Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
(evhtp_send_reply): Grab reference to bufferevent during write.
Browse files Browse the repository at this point in the history
If the data written is not very large, _evhtp_connection_writecb() may
be called directly from the bufferevent_write_buffer() call, and thus
free the evhtp connection and the underlying bufferevent.

This works fine us and for libevhtp, but unfortunately the SSL code in
libevent doesn't hold a reference to the bufferevent when calling the
write callback, and still it uses the bufferevent after the write
callback. By grabbing an extra reference to the bufferevent we become
independent of what libevent does.
  • Loading branch information
adamel committed Nov 20, 2015
1 parent bd70b5e commit a976a2f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,7 @@ void
evhtp_send_reply(evhtp_request_t * request, evhtp_res code) {
evhtp_connection_t * c;
evbuf_t * reply_buf;
struct bufferevent * bev;

c = evhtp_request_get_connection(request);
request->finished = 1;
Expand All @@ -3055,7 +3056,10 @@ evhtp_send_reply(evhtp_request_t * request, evhtp_res code) {
return;
}

bufferevent_write_buffer(evhtp_connection_get_bev(c), reply_buf);
bev = evhtp_connection_get_bev(c);
bufferevent_lock(bev);
bufferevent_write_buffer(bev, reply_buf);
bufferevent_unlock(bev);
evbuffer_drain(reply_buf, -1);
/* evbuffer_free(reply_buf); */
}
Expand Down Expand Up @@ -4378,4 +4382,4 @@ evhtp_make_request(evhtp_connection_t * c, evhtp_request_t * r,
unsigned int
evhtp_request_status(evhtp_request_t * r) {
return htparser_get_status(r->conn->parser);
}
}

0 comments on commit a976a2f

Please sign in to comment.