Repository navigation
Conversation
When a task blocked in blockInPlace is canceled, the cancel handler
waited for the worker only via the `done` flag. But completionFn sets
`done` *before* its trailing waiter.signal():
ctx.done.store(true, .release);
waiter.signal(); // still touches our stack frame
Since ctx/work/waiter all live on blockInPlace's stack, observing `done`
let blockInPlace return and reclaim that frame while the worker was still
inside signal(). The worker then read the freed Waiter: its union tag was
interpreted as .select, taking the cmpxchgStrong path on a garbage winner
pointer and segfaulting at address 0x0.
The window is only a couple of instructions on the worker (between the
store and the signal), so it needs the resend-backoff timer to fire in
exactly that gap - which happens regularly under load, e.g. on
powerpc64le under QEMU in CI.
Fix: keep resending SIGURG with backoff until the work finishes (`done`),
but drive the backoff sleeps on a *separate* waiter so our `waiter` is
signaled only by completionFn. Then do one final waiter.wait(1) to
synchronize with completionFn's trailing signal() - the worker's last
access to our stack frame - before returning. The fast (non-canceled)
path is unchanged: it already returns only after signal(), which is
completionFn's last action there.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughReworked the cancellation handling in blockInPlace's wait/resend logic within src/common.zig. Added clarifying comments about Context.done semantics and replaced the counter-driven waiter.timedWait loop with a sleeper-based exponential backoff resend loop followed by a final no_cancel wait on the main waiter. ChangesCancellation Wait/Resend Rework
Estimated code review effort: 3 (Moderate) | ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
not the right fix |
Problem
The
common.test.blockInPlace: cancellation interrupts a blocking syscall on the workertest segfaults on powerpc64le/epoll under QEMU in CI (reported crash:cmpxchgStrongat address0x0, under thecompletion_fncall inthread_pool.runTasks). It reproduces fairly regularly in CI.Root cause
blockInPlacekeeps everything on its own stack —ctx,waiter,token,work. The invariant that keeps this safe is: the task must not return (and reclaim that stack) until the worker'scompletionFnhas completely finished touching those objects.The cancel handler broke that invariant.
completionFnpublishesdonebefore its trailingwaiter.signal():The cancel loop returned as soon as it observed
done:So the sequence was:
ctx.done = true.timedWait) is woken by the backoff timer, seesdone == true, and returns fromblockInPlace— reclaiming its stack.waiter.signal()against the now-freedwaiter. The reused stack memory makes the tagged-unionmoderead as.select, which takes thecmpxchgStrongpath (common.zig,Waiter.signal) on a garbagewinnerpointer → segfault at0x0.The window between (1) and (2) is only a couple of instructions on the worker, so the crash requires the backoff timer to fire in exactly that gap. That is rare on fast native hardware but happens regularly under QEMU emulation and CI load. Nothing here is architecture-specific — it's a pure timing window.
The fast (non-canceled) path was never affected: it returns only after
signal(), which iscompletionFn's last action there.Fix
Keep resending
SIGURGwith backoff until the work finishes (done), but drive the backoff sleeps on a separate waiter, leavingblockInPlace'swaiterto be signaled only bycompletionFn. After the loop, do one finalwaiter.wait(1, .no_cancel)to synchronize withcompletionFn's trailingsignal()— the worker's last access to the stack frame — before returning.completionFnand the fast path are unchanged.