Skip to content

Commit 3c69d12

Browse files
committed
Merge #260: event loop: tolerate unexpected exceptions in post() callbacks
b8a48c6 event loop: tolerate unexpected exceptions in `post()` callbacks (hulxv) Pull request description: If a function posted via `EventLoop::post()` threw an exception, the event loop would exit without resetting `m_post_fn` or notifying the condition variable, permanently deadlocking the calling thread in `post()`. This change catches the exception instead, logs it, and keeps the event loop running so the caller is unblocked and other I/O events continue to be processed. Fix #259 ACKs for top commit: ryanofsky: Code review ACK b8a48c6. Since last review just squashed commits and added missing thread safety annotation. This change is a probably an improvement from a debugging perspective since it will now always log an error when an unexpected exception happens. It is not a clear improvement in behavior since continuing after an unexpected failure could potentially be worse than hanging, but the difference is unlikely to be significant. Probably the best thing would be to [catch and rethrow](#260 (comment)) the exception. Tree-SHA512: 5cf5481227b810ce09801eebf6bcde44ee3152545738cbd8068365a2d988ddd78486cc08906e0df3125be83f06acb0ae40e718c3b1d28c602e821ae1989c317e
2 parents f787863 + b8a48c6 commit 3c69d12

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/mp/proxy.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <kj/async-prelude.h>
2323
#include <kj/common.h>
2424
#include <kj/debug.h>
25+
#include <kj/exception.h>
2526
#include <kj/function.h>
2627
#include <kj/memory.h>
2728
#include <kj/string.h>
@@ -245,7 +246,12 @@ void EventLoop::loop()
245246
if (read_bytes != 1) throw std::logic_error("EventLoop wait_stream closed unexpectedly");
246247
Lock lock(m_mutex);
247248
if (m_post_fn) {
248-
Unlock(lock, *m_post_fn);
249+
// m_post_fn throwing is never expected. If it does happen, the caller
250+
// of EventLoop::post() will return without any indication of failure,
251+
// which will likely cause other bugs. Log the error and continue.
252+
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() MP_REQUIRES(m_mutex) { Unlock(lock, *m_post_fn); })) {
253+
MP_LOG(*this, Log::Error) << "EventLoop: m_post_fn threw: " << kj::str(*exception).cStr();
254+
}
249255
m_post_fn = nullptr;
250256
m_cv.notify_all();
251257
} else if (done()) {

0 commit comments

Comments
 (0)