Skip to content

Commit 7008b07

Browse files
committed
Merge #11593: rpc: work-around an upstream libevent bug
97932cd rpc: further constrain the libevent workaround (Cory Fields) 6b58360 rpc: work-around an upstream libevent bug (Cory Fields) Pull request description: A rare race condition may trigger while awaiting the body of a message. This may fix some reported rpc hangs/crashes. This work-around mimics what libevent does internally once a write has started, which is what usually happens, but not always due to the processing happening on a different thread: https://github.com/libevent/libevent/blob/e7ff4ef2b4fc950a765008c18e74281cdb5e7668/http.c#L373 Fixed upstream at: libevent/libevent@5ff8eb2 Tree-SHA512: b9fa97cae9da2a44101c5faf1e3be0b9cbdf722982d35541cf224be31430779c75e519c8ed18d06ab7487bfb1211069b28f22739f126d6c28ca62d3f73b79a52
2 parents bfb270a + 97932cd commit 7008b07

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/httpserver.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <event2/thread.h>
2626
#include <event2/buffer.h>
27+
#include <event2/bufferevent.h>
2728
#include <event2/util.h>
2829
#include <event2/keyvalq_struct.h>
2930

@@ -239,6 +240,16 @@ static std::string RequestMethodString(HTTPRequest::RequestMethod m)
239240
/** HTTP request callback */
240241
static void http_request_cb(struct evhttp_request* req, void* arg)
241242
{
243+
// Disable reading to work around a libevent bug, fixed in 2.2.0.
244+
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) {
245+
evhttp_connection* conn = evhttp_request_get_connection(req);
246+
if (conn) {
247+
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
248+
if (bev) {
249+
bufferevent_disable(bev, EV_READ);
250+
}
251+
}
252+
}
242253
std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
243254

244255
LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n",
@@ -601,8 +612,21 @@ void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
601612
struct evbuffer* evb = evhttp_request_get_output_buffer(req);
602613
assert(evb);
603614
evbuffer_add(evb, strReply.data(), strReply.size());
604-
HTTPEvent* ev = new HTTPEvent(eventBase, true,
605-
std::bind(evhttp_send_reply, req, nStatus, (const char*)nullptr, (struct evbuffer *)nullptr));
615+
auto req_copy = req;
616+
HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{
617+
evhttp_send_reply(req_copy, nStatus, nullptr, nullptr);
618+
// Re-enable reading from the socket. This is the second part of the libevent
619+
// workaround above.
620+
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) {
621+
evhttp_connection* conn = evhttp_request_get_connection(req_copy);
622+
if (conn) {
623+
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
624+
if (bev) {
625+
bufferevent_enable(bev, EV_READ | EV_WRITE);
626+
}
627+
}
628+
}
629+
});
606630
ev->trigger(nullptr);
607631
replySent = true;
608632
req = nullptr; // transferred back to main thread

0 commit comments

Comments
 (0)