Repository navigation
[WTF] The suspend signal handler reads the interrupted stack pointer from the context - #817
Conversation
…from the context signalHandlerSuspendResume published the registers only when its own stack pointer was inside the thread's stack. That test fails on every try once the handler runs on an alternate signal stack: a runtime loaded into the process (the Go runtime, in runtime.setsigstack) adds SA_ONSTACK to every handler it finds, and a thread with a sigaltstack (the main thread of Bun) then never suspends. Thread::suspend() retries without end, and a collector that waits for the thread's stack roots hangs the process. The handler now tests the stack pointer that the kernel saved in the ucontext. It is the same on either stack. A signal that interrupted code on another stack (a nested handler on the alternate stack) still backs off, as before. Related to oven-sh/bun#44982, oven-sh/bun#29843.
de74034 to
f919998
Compare
|
Preview build of f252926: |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline findings, I also checked: the per-architecture field selection in interruptedStackPointer() matches JSC::MachineContext::stackPointerImpl(mcontext_t&) exactly for the six cases it covers; macOS is not affected because this whole handler lives under #if !OS(DARWIN) (Darwin suspends via Mach, not signals); and the REG_RSP/gregs use on musl needs _GNU_SOURCE, which MachineContext.h already requires in the same build, so the musl lanes do not pick up a new dependency.
Extended reasoning...
The change is confined to Source/WTF/wtf/posix/ThreadingPOSIX.cpp and alters the stack-pointer test in the SIGPWR suspend handler that feeds conservative GC stack scanning, which is a correctness-critical path for every suspend (collector, sampling profiler, Workers) on Linux and FreeBSD. It touches no auth, injection, or data-exposure surface. The inline findings already call for a human look; this note only records what else was examined and ruled out.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe POSIX signal handler now checks the interrupted thread’s saved stack pointer when machine-context support is available. It falls back to the handler’s current stack pointer when extraction is unavailable. If the selected pointer is outside the thread’s stack, the handler clears saved registers and signals the suspend caller to retry. ChangesPOSIX thread suspension
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
I reviewed the new commit and found no bugs; the platform-coverage gap raised earlier is closed, but since this is the signal handler on the GC's conservative-scan path and most of the new branches build in no CI lane, a human look is still worthwhile.
What was reviewed:
- Compared every branch of
interruptedStackPointer()againstJSC::MachineContext::stackPointerImpl(mcontext_t&)(MachineContext.h:148-206): field names and OS/CPU conditions match line for line for every non-Darwin platform; the handler is under#if !OS(DARWIN)so the Darwin case does not apply. - Checked that the published registers and the back-off test now read the same ucontext (
registersFromUContext()at ThreadingPOSIX.cpp:260), and that the!HAVE(MACHINE_CONTEXT)path is behaviorally unchanged. - Checked
REG_RSP/REG_SPavailability: they come from<sys/ucontext.h>via PlatformRegisters.h under_GNU_SOURCE, which C++ compilers predefine on Linux, so the glibc/musl/Android lanes cover it; FreeBSD-ARM32, NetBSD, Haiku, QNX, Hurd and Fuchsia branches are not compiled anywhere in CI.
Extended reasoning...
The PR changes only Source/WTF/wtf/posix/ThreadingPOSIX.cpp (+57/-10): the non-Darwin signal-based Thread::suspend handler now checks the interrupted thread's stack pointer read from the kernel-saved ucontext instead of the handler's own stack pointer, so a handler forced onto an alternate signal stack (Go runtime adding SA_ONSTACK) no longer makes suspend() retry forever. It touches no auth, crypto, injection or data-exposure surface, but it is on the GC conservative stack-scan path used by every non-Darwin POSIX build, and the per-platform field table duplicates MachineContext.h by hand. The second commit since my prior review addresses the inline comment about missing platforms; the field selection now matches MachineContext.h exactly. Deferring rather than approving because the code runs in a signal handler on a critical GC path and the FreeBSD-ARM32/NetBSD/Haiku/QNX/Hurd/Fuchsia branches are neither built nor tested in CI.
…al stack (#45427) Fixes #44982 > `WEBKIT_VERSION` is the preview build of oven-sh/WebKit#817. Move it to the merge commit's build before this lands. ### Problem - Bun 1.4.3 hangs on Linux after a Go c-shared library is loaded. The JS thread waits in `Heap::acquireAccessSlow` from `Bun__JSC_acquireHeapAccessAfterWait`. The collector thread loops in `WTF::Thread::suspend()`. - Since #43681 the JS thread releases heap access when it parks with an idle collection pending, so the collector suspends it with SIGPWR to copy its stack. The Go runtime adds `SA_ONSTACK` to every handler, and Bun's crash handler gives the main thread a `sigaltstack`. WTF's handler ran on the alternate stack, saw its own stack pointer outside the thread's stack, and backed off on every try. ### Fix - oven-sh/WebKit#817: the handler tests the stack pointer the kernel saved in the `ucontext`, which is the same on either stack. This PR pins its preview build. - Correct because the stack scan already takes its top of stack from those registers. A handler on the thread's own stack sees no change. - Verified: `test/regression/issue/44982.test.ts` (Linux only) hangs on a debug build of main and passes with this pin. The reporter's script passes 3 of 3. Four related suites give the same results with both pins (Notes). - Self-review of the WebKit change did not complete (two runs stalled). The diff is 45 lines in one function and the same code as oven-sh/WebKit#235. ### Background - On Linux, JSC's collector thread suspends a mutator with a signal to copy its stack for the conservative scan. Only Linux is affected. - oven-sh/WebKit#235 and #31161 are this fix for an earlier trigger (#31158). Considered a Bun-side workaround (keep heap access when SIGPWR has `SA_ONSTACK`): a `sigaction` call per wait, blind to a library loaded later, and the sampling profiler and Workers stay broken. ### Downsides - None found. The handler reads one field of the `ucontext` in place of the inline SP read: no syscall, no allocation. - The second report on the issue (no Go library, low CPU) is not verified to be this mechanism and may remain. <details><summary>Notes</summary> **Stacks at hang time** (1.4.3, gdb). Main thread: `Heap::acquireAccessSlow` <- `Bun__JSC_acquireHeapAccessAfterWait` <- `us_loop_run_bun_tick` (epoll_kqueue.c:556). Collector thread: `Thread::suspend` <- `MachineThreads::tryCopyOtherThreadStacks` <- `gatherConservativeRoots` <- `Collector::runFixpointPhase`. `/proc/<pid>/status` of the main thread shows `SigPnd` with SIGPWR over and over, `SigBlk: 0`. One GC thread and the main thread use about 20% and 10% CPU. **SIGPWR `sa_flags`**, read with `sigaction(30, 0, buf)`: `0x14000004` (`SA_RESTART|SA_SIGINFO|SA_RESTORER`) in a plain script, `0x1c000004` (plus `SA_ONSTACK`) after the Go library (`httpcloak@1.7.2` through koffi) loads. The same in 1.4.2 and 1.4.3. The main thread's `sigaltstack` is 512 KiB in both. **Two experiments on 1.4.3**: clearing `SA_ONSTACK` on SIGPWR after the library loads makes the reporter's script pass 6 of 6. `BUN_IDLE_GC_SECONDS=0` makes it pass 6 of 6. **The test**: `bun:ffi` adds `SA_ONSTACK` to SIGPWR the way the Go runtime does (`runtime.setsigstack`), `BUN_IDLE_GC_SECONDS=1` with a 250 ms GC timer requests the idle collection after 1 s of quiet, a 2.5 s timer proves the loop came back, and `BUN_JSC_logGC=1` proves the collection ran in that window. The child hangs 3 of 3 on 1.4.3 and on a debug build of main, and 1.4.2 passes 3 of 3. The gate cannot show the failing side: it resets `src/` and `packages/`, and the pin lives in `scripts/build/deps/webkit.ts`. **Other suites**, debug ASAN build, this container under load: `test/js/bun/jsc/bun-jsc.test.ts`, `test/js/bun/ffi/ffi.test.js`, `test/js/web/workers/worker.test.ts`, `test/cli/run/cpu-prof.test.ts`. With this pin 238 pass, 5 fail. With the base pin 237 pass, 6 fail. Every failure is a 1 s or 5 s timeout (`worker with event listeners doesn't close event loop`, the `terminate()` races, `ArrayBuffer cells through an FTL-compiled call site`, the int64 identities), and the set varies between runs of the same build. **Kernel semantics**, a C program with a `sigaltstack` and a SIGPWR handler: without `SA_ONSTACK` the handler's SP and the `ucontext` SP are both on the main stack. With `SA_ONSTACK` the handler's SP is on the alternate stack and the `ucontext` SP is unchanged. **Not covered**: the second comment on #44982 (NestJS, napi-rs, ffi-rs, argon2, sharp, no Go, CPU low). No stack trace yet. If a native library blocks SIGPWR on a thread that has run JS, the suspend never returns either, and the fix here does not reach that. </details> <!-- robobun:evidence:begin --> --- **no test proof** · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/regression/issue/44982.test.ts <!-- robobun:evidence:end --> --------- Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Bun: - fetch: a TLS session that breaks after its handshake is a lost connection, so requests are retried as for any lost connection, not failed as a refused handshake (oven-sh/bun#46409). - macOS: DNS lookups use DNSServiceQueryRecord, so split DNS (VPNs) can fail over (oven-sh/bun#44519). WebKit: - The GC's suspend signal handler reads the interrupted stack pointer from the signal context, so it is right on an alternate signal stack (oven-sh/WebKit#817, oven-sh/bun#44982). Appended to the patch series; the existing patches are unchanged.
Related to oven-sh/bun#44982
Problem
Heap::acquireAccessSlow, the collector thread loops inThread::suspend(), and SIGPWR goes back and forth between them at about 30% CPU.Thread::signalHandlerSuspendResume(Source/WTF/wtf/posix/ThreadingPOSIX.cpp) publishes the registers only whencurrentStackPointer()is insidem_stack. The Go runtime addsSA_ONSTACKto every handler it finds (runtime.setsigstack), and Bun gives its main thread asigaltstack. The handler then runs on the alternate stack, the test fails on every try, andsuspend()retries without end.Fix
ucontext(newinterruptedStackPointer(), the same field selection asJSC::MachineContext::stackPointerImpl, which WTF cannot include). That pointer is the same on either stack.MachineThreads::tryCopyOtherThreadStack) already reads its top of stack from those registers. A handler on the thread's own stack sees no change: both pointers are insidem_stack. A signal that interrupted code on another stack (a nested handler on the alternate stack) still backs off.HAVE(MACHINE_CONTEXT)are unchanged.SA_ONSTACKthe handler's SP is on the alternate stack and theucontextSP is on the main stack). The Bun side pins the preview build and carries the regression test, which hangs on a debug build of Bun main and passes with this change.Background
sigsuspend()untilresume().SIGPWRhasSA_ONSTACK): it costs asigactioncall per wait, misses a library loaded later, and leaves every other suspend (sampling profiler, Workers, the libpas scavenger) broken.Downsides
ucontextin place of the inline SP read. No syscall, no allocation, no change to whatsuspend()andresume()do.