Skip to content

[WTF] The suspend signal handler reads the interrupted stack pointer from the context - #817

Merged
dylan-conway merged 2 commits into
mainfrom
robobun/1ec48878/suspend-handler-on-altstack
Oct 21, 2026
Merged

dylan-conway merged 2 commits into
mainfrom
robobun/1ec48878/suspend-handler-on-altstack

Conversation

@robobun

@robobun robobun commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Related to oven-sh/bun#44982

Problem

  • Bun 1.4.3 hangs on Linux once a Go c-shared library is loaded ([Regression][Linux] Native HttpCloak/Koffi calls followed by GC hang Bun 1.4.3; 1.4.2 completes bun#44982). The JS thread waits in Heap::acquireAccessSlow, the collector thread loops in Thread::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 when currentStackPointer() is inside m_stack. The Go runtime adds SA_ONSTACK to every handler it finds (runtime.setsigstack), and Bun gives its main thread a sigaltstack. The handler then runs on the alternate stack, the test fails on every try, and suspend() retries without end.

Fix

  • The handler tests the stack pointer the kernel saved in the ucontext (new interruptedStackPointer(), the same field selection as JSC::MachineContext::stackPointerImpl, which WTF cannot include). That pointer is the same on either stack.
  • Correct because the stack scan (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 inside m_stack. A signal that interrupted code on another stack (a nested handler on the alternate stack) still backs off.
  • On an architecture without an accessor the handler keeps the old test. Platforms without HAVE(MACHINE_CONTEXT) are unchanged.
  • Verified: the kernel semantics with a C program (with SA_ONSTACK the handler's SP is on the alternate stack and the ucontext SP 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

Downsides

  • None found. The handler reads one field of the ucontext in place of the inline SP read. No syscall, no allocation, no change to what suspend() and resume() do.

…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.
@robobun
robobun force-pushed the robobun/1ec48878/suspend-handler-on-altstack branch from de74034 to f919998 Compare October 15, 2026 21:56
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Preview build of f252926: autobuild-preview-pr-817-f252926e

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread Source/WTF/wtf/posix/ThreadingPOSIX.cpp Resolved
Comment thread Source/WTF/wtf/posix/ThreadingPOSIX.cpp Resolved
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: c8b55fbf-bc7f-4bb7-b91f-8d37bf1840a3
📥 Commits

Reviewing files that changed from the base of the PR and between f919998 and f252926.

📒 Files selected for processing: 1
  • Source/WTF/wtf/posix/ThreadingPOSIX.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The 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.

Changes

POSIX thread suspension

Layer / File(s) Summary
Extract and validate the interrupted stack pointer
Source/WTF/wtf/posix/ThreadingPOSIX.cpp
The code extracts stack pointers for supported Linux, Fuchsia, Hurd, FreeBSD, NetBSD, Haiku, and QNX architecture combinations. The signal handler checks the extracted pointer, falls back to its current stack pointer when needed, and signals the suspend caller to retry if the selected pointer is outside the thread’s stack.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to f2529

The change aims to prevent suspension retries when a signal arrives on an alternate stack. No concrete merge-blocking failure is established by the available evidence.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description gives a detailed problem statement, fix, rationale, background, and verification results. However, it does not follow the repository template because it lacks a WebKit Bugzilla title a… Add the WebKit Bugzilla bug title and URL, include the required review-status line, and list the changed file and affected functions in the template format. Link the pull request from the associated Bugzilla issue and apply the required rep…
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly identifies the primary change: reading the interrupted stack pointer from the signal context in the suspend signal handler.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description gives a detailed problem statement, fix, rationale, background, and verification results. However, it does not follow the repository template because it lacks a WebKit Bugzilla title and URL, a "Reviewed by NOBODY (OOPS!)." line, and the required changed-file and function list.

Resolution

Add the WebKit Bugzilla bug title and URL, include the required review-status line, and list the changed file and affected functions in the template format. Link the pull request from the associated Bugzilla issue and apply the required repository labels and assignment.

  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() against JSC::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_SP availability: 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.

@dylan-conway
dylan-conway merged commit d6375c7 into main Oct 21, 2026
52 checks passed
dylan-conway added a commit to oven-sh/bun that referenced this pull request Oct 21, 2026
…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>
Kushalkhemka pushed a commit to opensec-git/Pi-Bolt that referenced this pull request Oct 22, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants