Repository navigation
Catch panics in the Rust futures that kj-rs hands to C++ - #8831
Conversation
C++ polls and drops a Rust future through the `extern "C"` callbacks in kj-rs/future.rs, which had no `catch_unwind`, so a panic while polling or dropping a future unwound into a function that can't unwind and aborted the process. A panic in a synchronous `extern "Rust"` function already becomes a `kj::Exception`, which left every caller that hands a future to C++ to wrap it in a panic-catching adapter of its own. The poll callbacks now catch a panic and reject the future's promise with a FAILED exception, described as `panic in Rust future: <message>`. This works for a future whose output is not a `Result` too: C++'s `FuturePoller` already has room for a `kj::Exception*` beside the output and accepts the error status for any future, so only the Rust side changes. The drop callbacks catch a panic in the future's destructor, which runs when C++ cancels the promise, and log it with `KJ_LOG(ERROR)` through a small C++ helper, since the cancellation has nowhere to report it. The error paths are cold; the path without a panic costs only the `catch_unwind` itself. The future and synchronous paths share the description through `cxx::private::panic_description()`, which now also reads a `&'static str` payload, the one `panic!()` with a literal message gives, so a synchronous panic with such a message carries it as well instead of only `panic in <function>`.
`io_future_wait()`, `wait_until_future_wait()` and `reentry_call_wait()` wrapped the future they wait for to catch a panic while polling it, which failed the wait, and to log a panic while dropping it, because kj-rs used to abort the process on either. kj-rs now does both for every Rust future it hands to C++, so the wrappers and `CatchDropPanic` go, and each function only awaits its future. `io_future_wait()` no longer needs a `Result` output either. A panic still fails the wait and a panic while cancelling is still logged; only the descriptions change, to kj-rs's `panic in Rust future: <message>` and `panic in Rust future destructor: <message>`, and the tests expect those. `catch_poll_panic()`, behind `IoContext::await_io()`, stays: it turns a panic into an output so that the JavaScript promise rejects instead of the task failing.
cba755d to
21a260b
Compare
There was a problem hiding this comment.
Superseded by the review of
ff8d0801.
This review, of aa4c94bc
✅ Approved — No issues found: poll panics become FAILED exceptions using correctly sized union storage, drop panics are logged, and the new tests cover each path.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Trust boundaries: the new
catch_unwindguards only turn panics intokj::Exceptions or log entries, and add no new trust boundary. - Logging bridge: it passes an explicit pointer and length, so it has no format-string problem.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
- Callbacks: every poll and drop callback goes through the shared
write_error,poll_panic_exceptionanddrop_catching_panichelpers. - C++ side:
FuturePolleralready handlesErrorfor anyT. - Build: the new
future.c++is compiled by the existing glob. io_context: removing the wrappers leaves no dead code, and the affected tests are updated.
⚡ Performance: ✅ LGTM
No performance concerns.
📝 Documentation: ✅ LGTM
The README, PATCHES.md and header comments accurately describe the new behaviour.
Reviewed commit: aa4c94bc
There was a problem hiding this comment.
Superseded by the review of
f52be830.
This review, of ff8d0801
✅ Approved — No issues found: poll panics become FAILED exceptions using correctly sized union storage, drop panics are logged, and the new tests cover each path.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Trust boundaries: the new
catch_unwindguards only turn panics intokj::Exceptions or log entries, and add no new trust boundary. - Logging bridge: it passes an explicit pointer and length, so it has no format-string problem.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
- Callbacks: every poll and drop callback goes through the shared
write_error,poll_panic_exceptionanddrop_catching_panichelpers. - C++ side:
FuturePolleralready handlesErrorfor anyT. - Build: the new
future.c++is compiled by the existing glob. io_context: removing the wrappers leaves no dead code, and the affected tests are updated.
⚡ Performance: ✅ LGTM
No performance concerns.
📝 Documentation: ✅ LGTM
The README, PATCHES.md and header comments accurately describe the new behaviour.
Reviewed commit: ff8d0801 — same changes as aa4c94bc, so this is that commit's review (no new review was run; line numbers are as of aa4c94bc)
…-future-panics # Conflicts: # src/rust/cxx/src/unwind.rs
There was a problem hiding this comment.
✅ Approved — No issues found. The merge resolution in unwind.rs keeps the shared panic-message helpers and the canceled-result handling intact, and the rest of the change is the same as at the last review, which also found no issues.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The new catch_unwind guards only turn panics into kj::Exceptions or log entries, and they add no new trust boundary. The logging bridge passes an explicit pointer and length, so it has no format-string problem.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md updates needed.
📋 Code Quality: ✅ LGTM
- Merge resolution in
unwind.rs:catch_unwindandtry_unwindboth go throughpanic_result. It maps aCanceledExceptionpayload toResult::canceled(). Every other payload becomes a FAILED error whose text comes frompanic_description, so a literalpanic!("...")keeps its message. - Shared helpers:
panic_descriptionandpanic_messageare public and take&(dyn Any + Send). That lets the poll and drop callbacks infuture.rsreuse them. - Rest of the change: the poll and drop callbacks and the C++ side are the same as at the last review.
io_context: removing the wrappers leaves no dead code, and the affected tests are updated.
⚡ Performance: ✅ LGTM
No performance concerns.
📝 Documentation: ✅ LGTM
The README, PATCHES.md and header comments accurately describe the new behaviour.
Reviewed commit: f52be830
main's #8831 catches panics in the futures kj-rs hands to C++, as this branch did, but logs a panic in a future's destructor rather than aborting. The merge takes main's helpers and policy, keeps this branch's lent poll waker, and drops this branch's own panic helper, its panic tests (main's cover the same cases) and the death test that asserted the abort. main's #8761 gives PromiseAwaiter the same PhantomPinned field this branch added; the merge keeps one. ThreadLogger, new on main, crosses threads in a KjOwn, so it opts into DisposableOnAnyThread like the other kj::heap() types. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kj-rs lets C++ poll and drop a Rust future through
extern "C"callbacks that had no
catch_unwind, so a panic in either aborted theprocess, unlike a panic in a synchronous
extern "Rust"function,which already becomes a
kj::Exception. Callers such asio_contextworked around this by wrapping each future themselves.
kj-rs now catches both. A panic while polling rejects the promise with
a FAILED exception,
panic in Rust future: <message>, for any outputtype, not only
Result. A panic while dropping, which is how C++cancels a future, has nowhere to go, so it is logged with
KJ_LOG(ERROR). The message extraction is shared with the synchronouspath.
The second commit removes the wrappers in
io_contextthat this makesredundant. Panics in those futures still fail the wait or get logged,
now with kj-rs's descriptions.
catch_poll_panic()stays, since itturns a panic into a rejected JavaScript promise rather than a failed
task.
New tests in
awaitables-cc-test.c++cover panics on the first poll,on a later poll, in a future with a non-
Resultoutput, and oncancellation. Without the catch, the first of them aborts the test
binary.