Bug 298878 - linux(4): epoll_pwait(2)/epoll_pwait2(2) with a sigmask leave a stale signal mask, deadlocking Bun apps (Claude Code, opencode)
Summary: linux(4): epoll_pwait(2)/epoll_pwait2(2) with a sigmask leave a stale signal ...
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 15.1-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-emulation (Nobody)
URL:
Keywords:
Depends on:
Blocks: 247219 299200
  Show dependency tree / graph
 
Reported: 2026-09-26 12:09 UTC by Grzegorz Junka
Modified: 2026-10-23 08:57 UTC (History)
14 users (show)

See Also:


Attachments
Tested patch fixing this issue with proposed commit message (3.31 KB, patch)
2026-09-26 12:09 UTC, Grzegorz Junka
no flags Details | Diff
standalone Linux-ABI test, amd64 and i386, no libc (6.04 KB, text/plain)
2026-09-26 12:12 UTC, Grzegorz Junka
no flags Details
re-uploaded the patch without CRLF (3.23 KB, patch)
2026-09-26 13:46 UTC, Grzegorz Junka
no flags Details | Diff
Tested patch fixing this issue with proposed commit message (3.23 KB, patch)
2026-09-26 13:46 UTC, Grzegorz Junka
no flags Details | Diff
Standalone Linux-ABI test, amd64 and i386, no libc (5.86 KB, text/plain)
2026-09-26 13:47 UTC, Grzegorz Junka
no flags Details
Reproducer (glibc, builds for x86_64 and aarch64) (2.61 KB, text/plain)
2026-09-28 14:23 UTC, Yuki Tachi
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Grzegorz Junka 2026-09-26 12:09:13 UTC
Created attachment 275141 [details]
Tested patch fixing this issue with proposed commit message

linux_epoll_wait_ts() in sys/compat/linux/linux_event.c saves the
caller's signal mask in a local variable, but still sets TDP_OLDMASK and
schedules the TDA_SIGSUSPEND AST:

	if (uset != NULL) {
		error = kern_sigprocmask(td, SIG_SETMASK, uset,
		    &omask, 0);
		...
		td->td_pflags |= TDP_OLDMASK;
		ast_sched(td, TDA_SIGSUSPEND);
	}
	...
	if (uset != NULL)
		error = kern_sigprocmask(td, SIG_SETMASK, &omask,
		    NULL, 0);

On return to user mode, ast_sigsuspend() (or postsig(), if a signal is
being delivered) installs td->td_oldsigmask. This function never writes
it, so it still holds the mask the thread had at its last sigsuspend(2)
(or pselect/ppoll with a mask). After any epoll_pwait(2)/epoll_pwait2(2)
call with a non-NULL sigmask the thread can silently end up with an old,
unrelated signal mask. In addition, the final kern_sigprocmask()
overwrites "error", so EINTR (and any other error) is returned to user
space as 0.

This goes back to 80c7315d17ce ("Restore signal mask in epoll_pwait.",
r314311, 2017), which changed &td->td_oldsigmask to &omask but kept
TDP_OLDMASK. main and stable/15 are identical to 15.1-RELEASE here;
stable/14 has the same code.


Real-world impact: Bun-compiled Linux binaries deadlock
-------------------------------------------------------
Bun's event loop calls epoll_pwait2() with an empty sigmask on every
iteration. JavaScriptCore suspends threads for conservative GC stack
scanning by sending a signal (Bun uses SIGPWR) and waiting on a
semaphore until the target thread's handler acknowledges. That handler
runs with SIGPWR blocked and calls sigsuspend(2), so after the first GC
suspend of the main thread its td_oldsigmask is {SIGPWR}. The next
epoll_pwait2() blocks SIGPWR on the main thread, and the next GC
suspend request never completes. State of a hung process:

  - procstat -kk: every thread sleeps in linux_sys_futex -> umtxq_sleep
    (plus the idle poll/epoll threads)
  - procstat -j: the main thread has exactly one signal blocked, 98
    (LINUX_SIGPWREMU = Linux SIGPWR), and it is pending there ("PB")
  - user-space stacks: the main thread is in pthread_cond_wait() under
    JSC heap code (waiting for the collector); a JSC heap thread is in
    sem_wait() right after pthread_kill(), i.e. the thread-suspend
    handshake, waiting for the SIGPWR handler to run on the main thread

Seen with Claude Code (Anthropic's CLI, a Bun-compiled Linux binary run
via Linuxulator). Versions 2.1.269 and later (they embed Bun 1.4.3)
freeze shortly after start, after roughly the same delay every time,
whether idle, typing or working; kill -9 is the only way out. Versions
up to 2.1.268 (Bun 1.4.1) are not affected in practice: in a GC stress
test under Bun 1.4.1 the collector never signalled the main thread (no
tgkill or sigsuspend at all), so td_oldsigmask never gets poisoned.

misc/claude-code is currently at 2.1.268, so port users will run into
this with the next update. Users who install the newest Linux binary
from npm directly are affected now. We do that because the port lags
behind upstream, with a script along these lines:

  VERSION="${1:-latest}"
  npm install -g "@anthropic-ai/claude-code-linux-x64@${VERSION}" --force
  # /compat/linux/tmp must be 1777 for non-root users
  chmod 1777 /compat/linux/tmp /compat/linux/var/tmp
  cat > /usr/local/bin/claude << EOT
  #!/bin/sh
  exec /usr/local/lib/node_modules/@anthropic-ai/claude-code-linux-x64/claude "\$@"
  EOT

This is very likely also the cause of bug 296441 (opencode deadlocks
with all threads in umtxq_sleep, avoided with BUN_JSC_useJIT=0) and of
the Claude Code hangs reported in bug 298047 comment 7.


Test program
------------
Attached epoll_sigmask_test.c is freestanding (raw syscalls, no libc),
so it builds on FreeBSD with base clang and ld.lld; build instructions
are at the top of the file. It supports amd64 and i386 (the i386 build
goes through linux.ko, which compiles the same linux_event.c).

15.1-RELEASE-p3, stock code (i386 build, stock linux.ko):
  PASS  after sigsuspend() and sigprocmask(SIG_SETMASK, {}): mask 0x0000000000000000
  FAIL  after epoll_pwait(timeout=0, sigmask={}): mask 0x0000000020000200, expected 0x0000000000000000
  FAIL  after epoll_pwait2(timeout=0, sigmask={}): mask 0x0000000020000200, expected 0x0000000000000000
  FAIL  epoll_pwait2(timeout=NULL, sigmask={}) interrupted by SIGALRM: returned 0, expected -4 (EINTR); SIGALRM handler calls: 1
  FAIL  after the interrupted epoll_pwait2(), mask {SIGUSR1}: mask 0x0000000020000200, expected 0x0000000000000200
  FAILED

15.1-RELEASE-p3 with the attached patch (amd64 build, patched linux64.ko):
  PASS  after sigsuspend() and sigprocmask(SIG_SETMASK, {}): mask 0x0000000000000000
  PASS  after epoll_pwait(timeout=0, sigmask={}): mask 0x0000000000000000
  PASS  after epoll_pwait2(timeout=0, sigmask={}): mask 0x0000000000000000
  PASS  epoll_pwait2(timeout=NULL, sigmask={}) interrupted by SIGALRM: returned -4
  PASS  after the interrupted epoll_pwait2(), mask {SIGUSR1}: mask 0x0000000000000200
  OK

(An earlier version of the test showed the same stale mask,
0x20000200, with the amd64 build and the stock linux64.ko.)


Fix
---
The attached patch saves the old mask in td_oldsigmask and lets the AST
restore it, exactly as kern_pselect() and kern_poll_kfds() have done
since ccb973da1f1b ("kern: restore signal mask before ast() for
pselect/ppoll"): TDA_SIGSUSPEND if the wait was interrupted, so the
signal is delivered with the temporary mask and the handler frame saves
the correct mask, and TDA_PSELECT otherwise. This matches Linux, which
restores the saved mask unless epoll_pwait returns -EINTR. It also stops
EINTR from being turned into 0.

Tested on 15.1-RELEASE-p3 amd64: the test program passes, and Claude
Code 2.1.283 runs normally with the patched linux64.ko (with the stock
module it froze every time). The patch applies to main and stable/15.
stable/14 has no TDA_PSELECT; the equivalent change there would mirror
its kern_poll_kfds() (save into td_oldsigmask; on EINTR set TDP_OLDMASK
and schedule TDA_SIGSUSPEND, otherwise restore td_oldsigmask directly).

The analysis, patch and test program were prepared with the help of an
AI assistant (Claude by Anthropic); I reviewed them and tested them on
my system.


See Also:
---------
  296441 opencode deadlocks at startup
  298047 claude-code hangs, comment 7

Blocks
------
  https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=247219
Comment 1 Grzegorz Junka 2026-09-26 12:12:22 UTC
Created attachment 275142 [details]
standalone Linux-ABI test, amd64 and i386, no libc
Comment 2 Grzegorz Junka 2026-09-26 13:46:00 UTC
Created attachment 275144 [details]
re-uploaded the patch without CRLF

Tested patch fixing this issue with proposed commit message
Comment 3 Grzegorz Junka 2026-09-26 13:46:52 UTC
Created attachment 275145 [details]
Tested patch fixing this issue with proposed commit message
Comment 4 Grzegorz Junka 2026-09-26 13:47:51 UTC
Created attachment 275146 [details]
Standalone Linux-ABI test, amd64 and i386, no libc
Comment 5 Todd Smith 2026-09-27 18:54:11 UTC
Confirming the fix on amd64.

Setup: ThinkPad T14s Gen 2a (Ryzen 7 PRO 5850U), FreeBSD 15.1-RELEASE-p3 amd64, linux_base-c7, Claude Code native Linux binary (Bun).

 Before: every Claude Code session from 2.1.282 on froze within 0–90 s (11 of 11 sessions on 2026-09-24). The process stayed alive at ~0 % CPU and input was ignored. 2.1.281 was usable, though it presumably has the same exposure at a lower rate. Downstream report: https://github.com/anthropics/claude-code/issues/96931

 Patch: I applied the same change as attachment 275145 [details] (the code is identical, only the comment wording differs) to releng/15.1, then rebuilt and installed linux64.ko and linux.ko.

 After: with the patched modules loaded, Claude Code 2.1.283 has run for over 7 minutes of normal interactive use without freezing, with no Bun/JSC workaround environment variables set. Before the patch, 2.1.282 never lasted past 90 s.

Thanks to the reporter for the analysis and the patch.
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-09-27 19:27:33 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=16a284b1cdfd45ba99c2723e7f88497e76b235ad

commit 16a284b1cdfd45ba99c2723e7f88497e76b235ad
Author:     Grzegorz Junka <list1@gjunka.com>
AuthorDate: 2026-09-27 19:22:20 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-09-27 19:22:20 +0000

    linux(4): Fix signal mask restoration in epoll_pwait(2)/epoll_pwait2(2)

    Since 80c7315d17ce ("Restore signal mask in epoll_pwait.") the caller's
    signal mask is saved in a local variable, but TDP_OLDMASK is still set
    and the TDA_SIGSUSPEND AST is still scheduled.  On return to user mode
    that AST, or postsig() if a signal is delivered, then installs
    td_oldsigmask, which this code never writes and which holds whatever
    mask the thread had at its last sigsuspend(2), pselect(2) or ppoll(2).
    As a result every epoll_pwait(2)/epoll_pwait2(2) call with a non-NULL
    sigmask can leave the thread with a stale signal mask.  In addition, the
    explicit restore at the end overwrote the return value, so EINTR (and
    any other error) was reported to user space as 0.

    Save the old mask in td_oldsigmask and let the AST restore it, as
    kern_pselect() and kern_poll_kfds() do: schedule TDA_SIGSUSPEND if the
    wait was interrupted, so the signal is delivered with the temporary mask
    in place, and TDA_PSELECT otherwise.  This matches Linux, which restores
    the saved mask unless the syscall returns -EINTR.

    This deadlocks Bun-based programs such as Claude Code (>= 2.1.269) and
    opencode.  JavaScriptCore suspends threads for conservative GC stack
    scanning by sending SIGPWR and waiting for the target's handler, which
    calls sigsuspend(2) with SIGPWR blocked.  Afterwards td_oldsigmask
    contains SIGPWR, the event loop's next epoll_pwait2(2) (Bun always
    passes an empty sigmask) blocks SIGPWR on the main thread, and the next
    GC suspend request waits forever.

    PR:             298878
    Fixes:          80c7315d17ce ("Restore signal mask in epoll_pwait.")
    MFC after:      1 week

 sys/compat/linux/linux_event.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
Comment 7 Yuki Tachi 2026-09-28 14:21:14 UTC
Confirming the fix on arm64 as well.

Setup: FreeBSD 15.1-RELEASE-p2 arm64 (GENERIC), linux_base-rl9-9.7,
Claude Code native Linux binary (Bun).

Before: Claude Code 2.1.283 froze within 0-90 s every time; ktrace/procstat
showed the same state as described here (main thread in futex, signal 98
pending+blocked). Details: https://github.com/anthropics/claude-code/issues/96931

Patch: applied commit 16a284b1cdfd to releng/15.1, rebuilt linux64.ko
(with KERNBUILDDIR from the GENERIC config) and replaced the module.

After:
- Independent reproducer (static aarch64 glibc binary, attached) now matches Linux:
  A: epoll_pwait=-1 errno=Interrupted system call after 200 ms, handler ran 1, SIGUSR1 blocked after: yes
  B: epoll_pwait=0, SIGUSR2 (marker) blocked after: no
  (stock module: A returned 0 and left SIGUSR1 unblocked; B left SIGUSR2 blocked)
- Claude Code 2.1.283 has run for over an hour of normal interactive use without freezing.

Thanks for the analysis and the quick fix.
Comment 8 Yuki Tachi 2026-09-28 14:23:35 UTC
Created attachment 275207 [details]
Reproducer (glibc, builds for x86_64 and aarch64)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-10-04 00:34:58 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0143078248bb4caf39f23c9075b2afbede52e70d

commit 0143078248bb4caf39f23c9075b2afbede52e70d
Author:     Grzegorz Junka <list1@gjunka.com>
AuthorDate: 2026-09-27 19:22:20 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-10-04 00:33:40 +0000

    linux(4): Fix signal mask restoration in epoll_pwait(2)/epoll_pwait2(2)

    PR:             298878

    (cherry picked from commit 16a284b1cdfd45ba99c2723e7f88497e76b235ad)

 sys/compat/linux/linux_event.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
Comment 10 Vedran Miletic 2026-10-05 06:29:40 UTC
(In reply to commit-hook from comment #9)
Any possibility that we will also get it in a 15.1 patch release?
Comment 11 crest 2026-10-07 12:13:50 UTC
I would recommend downgrading the affected ports until an errata patch is available. Unless there are (known) exploitable security problems having a slightly older version is better than one that always hangs after a few seconds.
Comment 12 Dave Cottlehuber freebsd_committer freebsd_triage 2026-10-09 19:37:45 UTC
Setting BUN_JSC_useConcurrentGC=0 may be of use, as reported in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=299200 . If this fixes a specific port please report back your findings.
Comment 13 Vedran Miletic 2026-10-23 08:57:25 UTC
(In reply to Vedran Miletic from comment #10)
15.2 betas are't too far away now, but it would still be nice to get it in 15.1 as well.