Problem: When a callback that EventLoop::sync() runs on the event loop thread for another thread throws, the error is only logged ("EventLoop: m_sync_fn threw") and the calling thread carries on as if the callback succeeded. Depending on the caller:
- An IPC client call hangs forever if building the request throws, for example when a string argument is larger than the Cap'n Proto text size limit.
- If wrapping a socket fails,
MakeStreamreturns a null stream andListenConnectionsreturns as if it were listening. If creating theConnectionfails,ConnectStreambuilds a client on a nullConnectionand crashes. - An async server method runs without its cancellation callback if installing the callback fails.
Separately, a socket leaks if allocating the KJ object that wraps it fails.
None of this happens in normal operation. The triggers are allocation failures, epoll or kqueue failing to register a socket, and oversized arguments. The problems were reported privately in security reviews: the request build hang twice (first report, second report), the socket setup failures and leak (report), and the cancellation callback (report).
Solution: sync() now rethrows the callback's exception in the calling thread, as it already did when called on the event loop thread, so every caller sees the failure. Two call sites need more than that:
- The async method setup in
PassFieldcatches the exception inside its callback and rethrows it after its cleanup is in place, so the cancellation callback and self reference are still released on the event loop thread. MakeStreamandListenConnectionsclose the socket if allocating its wrapper fails. They close it only onstd::bad_alloc, since KJ closes the socket itself when a later step of constructing the wrapper fails.
Tests cover each case, using the existing testing_hook_misc hook, which is now called from three more places.
For reviewers: callbacks passed to sync() from destructors and from the server thread's result delivery in ProxyServer<Thread>::post() now terminate the process if they throw, instead of logging. Nothing in those callbacks is expected to throw.