kj-rs: add LoopChecked and KjFuture::into_send() for Send futures on a KJ event loop #8857
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.42) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 353 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.351
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #8857"
general
weight: 0.35
nathanwhit
Why
In edgeworker, a KJ event loop migrates between OS threads together with its tasks, so
IoContext::await_ioandIoContext::await_io_and_runrequireSendfutures (and now callbacks) to rule out OS-thread-pinned state (MutexGuard,Rccloned from athread_local!, etc.).Bridged C++
async fnpromises andKjOwn<T>handles are!Sendby default so they cannot accidentally cross intotokio::spawnor JSG resources. This stack adds lightweight,kj::EventLoop::Id-checked opt-in wrappers soSendfutures and callbacks that stay on the same KJ event loop can hold KJ objects and await C++ promises without uncheckedunsafeat call sites.Stack
1.
kj-rs: add LoopChecked—Send + !Syncwrapper storing a value alongsidekj::EventLoop::Id::current()(8 bytes,Copy, zero atomics). Failsget()/get_mut()/get_pin_mut()off-loop (into_inner()returnsErr(self)), panics if polled off-loop, and aborts on drop off-loop (including after loop destruction) or insidekj::DisallowAsyncDestructorsScope. Intended to be wrapped inside a private field of a!Tracedbinding struct at construction.2.
io-context: require await_io_and_run's callback to be Send— Adds+ Sendtoawait_io_and_run/await_iocallbacks. WrapsSpanBuilderandTraceContextinLoopCheckedat construction (removingThreadBoundSpan'sunsafe impl Send) while keeping them!Tracedso JSG resources must still useIoContext::add_span().3.
kj-rs: add KjFuture::into_send()—extern "C++"async fnnow requires a current KJ event loop and returns a!Send,!Unpinsealedkj_rs::KjFuture..into_send()is a safe opt-in returningSendKjFuture<Self>(Send + !Sync + !Unpin, preserving input borrows) that returns an error if polled off-loop and aborts if dropped off-loop. UpdatesTokioEventPort::cancelSpawnedTasks()to re-enter its loop'sWaitScopeif needed so tasks drop on-loop.Overhead
Measured with cross-language LTO: ~1.5 ns per loop check, ~3.9 ns per
LoopCheckedcreate/read/drop lifecycle, and ~5.5 ns per awaitedSendKjFuture(zero atomics or allocations).Testing
!SendKjFuture,Send + !SyncSendKjFuture, borrow preservation acrossinto_send(),!Sync/unsafe new()onLoopChecked,Sendcallbacks onawait_io_and_run/await_io,SpanBuilderrejection in#[jsg_resource]/#[jsg_traced], and taken spans inawait_io_and_run.kj_testsuites (loop-checked-test.c++,send-kj-future-test.c++,tokio-event-port-test.c++) covering off-loop access/poll errors, off-loop drop aborts (unpolled, pending, armed, and post-loop destruction),DisallowAsyncDestructorsScope/AllowAsyncDestructorsScope, andTokioEventPortteardown afterWaitScope.