Repository navigation
The JS event loop waits again when a signal interrupts select, as the C loop does - #1036
Conversation
There was a problem hiding this comment.
Thank you, the fix is right, and it holds on macOS too. On an Apple M4 merged onto main (ac0ddb7b), select_eintr wakes the idle park on main in 10 of 10 runs on the interpreted and JS lanes (never on C), and prints only slept on all three lanes with this branch. A C probe answers both of your macOS questions: select fails with EINTR even under SA_RESTART, and it leaves the fd sets untouched, so the retry with the same sets is sound. The tests/io checkup is byte-identical to main (the new test runs 91st in the shared process, with 24 after it), the C and JS lanes of tests/io match main, and gates/repo.ts passes 46/46.
Two changes.
1. The test's cost. It takes about 1.2 s per lane here (signal at 200 ms, IO.sleep(1000), and macOS restarts the full timeout after the interrupt), which is a lot against the gate's 30 s cap across ~1000 tests. The constants can't simply shrink, because of how the JS side sends the signal:
sh -c 'sleep …; kill …'needs ~30 ms or more just to start here, so with small constants the signal often lands after the process exited;- the child inherits the test's stderr, so a late signal prints
sh: line 0: kill: (…) - No such processinto the output, and the gate fails the run. At 5/30 this happened in about a third of the runs.
Spawning kill directly, with its output discarded, removes both: its own startup is the delay, and nothing can reach the test's output.
process.on("SIGWINCH", () => {});
Bun.spawn(["kill", "-s", "WINCH", String(process.pid)],
{ stdout: "ignore", stderr: "ignore" });With that, Idle.park(5) and IO.sleep(30), 20 runs per lane:
| main (spurious wake) | this branch (only slept) |
this branch, median | |
|---|---|---|---|
| interp | 20 / 20 | 20 / 20 | 108 ms |
| JS | 20 / 20 | 20 / 20 | 59 ms |
| C | 0 / 20 | 20 / 20 | 55 ms |
For reference, an empty program takes about 40 ms on JS and 18 ms on C here, so the test costs a few tens of ms instead of ~1.2 s per lane. The ms argument then only serves the C thread.
2. Retry with the time that's left. macOS leaves the timeout unchanged after EINTR (Linux updates it to the time left), so each retry waits the full timeout again. The PR's own test shows it: with the signal at 200 ms, IO.sleep(1000) ends at ~1.22 s on both lanes. The C loop already did this, but the retry is the code this PR adds, so please recompute the timeout from the deadline before each retry, in both loops; the sets can be reused as they are:
if (sys.errno() !== 4) {
throw "bend: the poller failed";
}
if (ms >= 0) {
const left = Math.max(0, Math.ceil(soon - performance.now()));
tv[0] = BigInt(left / 1000 | 0);
tv[1] = BigInt(left % 1000 * 1000);
} if (errno != EINTR) {
err_fail("the poller failed");
}
tick = io_tick();
ms = soon > tick ? (soon - tick) / 1000000 + 1 : 0;
tv = (struct timeval){ ms / 1000, ms % 1000 * 1000 };On Linux this changes nothing. It needs no test of its own: the error it removes is only as large as the signal's delay, 5 ms with the constants above.
Note: this review was written by an AI after it tested the PR for me and I reviewed the findings. If anything here is wrong, reply and I will review it myself.
5214d5a to
4ab883e
Compare
|
Thank you for testing it on the M4, and for the probe answers. Both changes are in, with one difference from the snippet, and the reason is measured. 1. The test's cost. The JS twin now asks the kernel instead:
An empty program takes 17 ms on JS and 2 ms on C here. The 2. Retry with the time that is left. Both loops recompute the timeout from the deadline before each retry, as in your snippets; the sets are reused. The The commit is amended and the description updated. |
… C loop does JS io_wait now does what the C one does: it selects again on EINTR, and on any other error fails with "bend: the poller failed" (a parked fd that is closed gives EBADF: the JS lanes used to wake every parked computation, and now print that line and exit 1, as the native lane did already). Both loops retry with the time that is left, recomputed from the deadline: macOS leaves select's timeout as it was after EINTR (Linux counts it down), so a retry there waited the whole timeout again. tests/io/select_eintr: Base has no signal API, so the test ships its own effect twins, each registered with io_eff(CID(Idle.park), ...). Idle.park(5) installs a no-op handler and has the signal land 5 ms later, while the loop waits in select (C: sigaction without SA_RESTART, and a thread that pthread_kills the loop's thread; JS: a process.on listener for SIGALRM, and setitimer, so the kernel sends it whatever the loop is doing then -- a kill spawned from the test lands about 0.5 ms later on Linux, most often before the loop reaches select: on upstream it caught the bug in 2 of 40 interpreted runs and 3 of 40 JS runs, the timer in 40 of 40), then parks on a pipe nobody writes; main sleeps 30 ms, prints "slept" and ends the process with IO.die. Twenty runs per lane on Linux: on upstream (95317d9) the interpreted and JS lanes print "the idle park woke" before "slept" 20 of 20 times; on this branch all three lanes print only "slept" 20 of 20 times, in 47 ms (JS) and 32 ms (C) a run, an empty program taking 18 ms and 1 ms. The tests/io checkup (118 modules, one process, as gates/test.ts runs it) is identical on this branch and on upstream with the test added but for the test's own "the idle park woke" line, gone on the branch; the nine tests of tests/io that wait on a timer or a poll pass on the JS and C lanes; gates/repo.ts passes 46 / 46; comp.ts is 62810 ttok (cap 64000). macOS was not run here. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: Claude Fable 5.1 <[email protected]>
4ab883e to
8c0ed1c
Compare
|
Rebased onto main (95317d9), since #1042 changed how an effect file names things: the test's twins now register with Re-run on the new base, Linux:
One correction to my last comment: re-measured on this base, a macOS is still not run here, so the one M4 run of |
|
Thank you @aldeni! Thorough work, and switching to a kernel timer was the right call. Here is the macOS run you asked for, on an Apple M4 with this branch merged onto current main (20 runs per lane):
The Right after the merge we simplified the fix in 3276efacc17af57d2db37c2d0c2a13f6ce4a4b5a. Both event loops already call We measured it with a small program that does nothing but go through Thanks again 🙏 Note: this comment was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself. |
Bug
The JS lane's
io_wait(bend2/comp.ts) ignores whatselectreturns. If a signal arrives while the loop sleeps inselect,selectfails withEINTR, and Linux leaves the fd sets unchanged. The loop then treats every bit it set as ready and calls themoreof every parked computation. Most effects just park again onEAGAIN.tcp_connectdoesn't: on wake it readsSO_ERROR, which is 0 while the connect is still in progress, so it answersDonewith a socket that never connected.Repro on Linux: a user effect installs a no-op SIGINT handler, then the program connects to TEST-NET-1, which drops its packets.
A build with
bend repro.bend -o repro.jsrun asbun repro.jsalso printsDone: connected. The native build (-o repro) stays pending, which is correct. Itsio_waitalready does this:Fix
The JS
io_waitnow works the same way. OnEINTR(4 on Linux and macOS) it callsselectagain with the same sets, and with the time that is left, recomputed from the deadline: macOS leaves the timeout as it was afterEINTR(Linux counts it down), so a retry there waited the whole timeout again. The C loop now recomputes it the same way. On any other error it throws"bend: the poller failed", which the JS lanes print before exiting with status 1, as the native lane does. After the fix the repro stays pending on the interpreted and JS lanes too: the process is still running 3 s after the SIGINT.The fix also changes what happens on errors other than
EINTR. When a parked fd is closed under the loop,selectfails withEBADF. Before, the JS lanes woke every parked computation. Now they printbend: the poller failedand exit 1, as the native binary already did. I checked this with a scratch effect that parks on a closed fd: before the fix the interpreted and JS lanes woke that computation; after it, all three lanes print the line and exit 1.Test
tests/io/select_eintr.bendneeds a signal to arrive while the loop waits inselect. Base has no signal API, so the test brings its own effect files. It follows the layout oftests/io/fifo_eof: a.cand a.jsnext to the.bend, which thetests/<ns>/<name>.(c|js)line ofgates/repo.tsallows, each registering the effect withio_eff(CID(Idle.park), ...).Idle.park(5)installs a no-op handler and has the signal land 5 ms later, while the loop waits inselect:sigactionwithoutSA_RESTART. A detached thread then callspthread_killon the loop's thread.process.on("SIGALRM", ...), which makes Bun install its handler, andsetitimer(ITIMER_REAL)throughbun:ffi, so the kernel sends the signal whatever the loop is doing then. A child process cannot promise that: akillspawned from the test lands about 0.5 ms later on Linux, most often before the loop reachesselect: on upstream it caught the bug in 2 of 40 interpreted runs and 3 of 40 JS runs, where the timer catches it in 40 of 40. AsetTimeoutwould never run here either, because the loop blocks in a synchronousselect.After that, the computation parks on the read end of a pipe that nobody writes to, so any wake is spurious.
mainsleeps 30 ms, printsslept, and ends the process withIO.die(U32, 0, ""), because the park is still live at that point.Two design choices:
Twenty runs per lane on Linux:
slept)An empty program takes 18 ms on JS and 1 ms on C here. On the native lane, a build with a print in its
EINTRbranch shows that the signal interruptsselectthere too, so the test exercises both loops.Regression
On Linux with Bun 1.3.11, mirroring what
gates/test.tsdoes:tests/iocheckup (118 modules, checked and interpreted in one process) is identical on this branch and on upstream 95317d9 with the test added, but for the test's ownthe idle park wokeline, gone on the branch.tests/iothat wait on a timer or a poll (fifo_eof,sleep_order,sleep_timer,spawn_outlives_main,spawn_sleep,tcp_poll,tcp_spawn,udp_poll, and this one) pass on the JS and C lanes.gates/repo.ts: 46 / 46.comp.tsis 62810 ttok (cap 64000).Not tested here
itimervalgoes through the sameBigInt64Arrayshapeio_waitalready uses fortv, so it should hold there as that does, but this branch was not run on a Mac.Related to #1034 (timeouts and cancellation) and to the review of #1011, where a wake that doesn't say why it happened came up. In this bug, the loop treated an interrupted wait as a ready fd.