-
Notifications
You must be signed in to change notification settings - Fork 5k
event_loop: don't debug-panic on late main-VM enqueue_task_concurrent during process.exit #36020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/b37fdd39/enqueue-task-concurrent-main-vm-exit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8fa685
event_loop: don't debug-panic on main-VM enqueue_task_concurrent afte…
robobun be3956b
test: gate on isASAN and poll for writer-in-kernel instead of sleeping
robobun 49f1c50
event_loop: shorten the worker-only assert comment
robobun 80e7d7e
test: fail loudly if the poll loop deadline expires without seeing wr…
robobun 332829f
test: forward-declare close/usleep instead of including <unistd.h>
robobun 4cffc9a
ci: retrigger
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isASAN, isLinux, tempDir } from "harness"; | ||
|
|
||
| // A thread-pool `fs.write` that is still blocked in the kernel when | ||
| // `process.exit()` runs must not crash the process when it later completes. | ||
| // `BUN_DESTRUCT_VM_ON_EXIT=1` (set by the CI runner) makes `global_exit` | ||
| // call `VirtualMachine::destroy()` on the main-thread VM before `libc::exit`; | ||
| // a work-pool completion that lands after that posts to the event loop of a | ||
| // VM whose `has_terminated` is already true. The main-thread VM box is never | ||
| // freed, so the push is harmless and the debug assert was a false positive. | ||
| // | ||
| // Deterministically reproducing the race requires something to unblock the | ||
| // kernel write *after* `destroy()` has returned. The child registers a libc | ||
| // atexit handler (via bun:ffi's tinycc) that closes the FIFO reader and | ||
| // then sleeps, so the blocked `write()` returns EPIPE while the main thread | ||
| // is still inside the atexit chain. | ||
| // | ||
| // Linux + ASAN only: relies on mkfifo and blocking-pipe write semantics, and | ||
| // on `Global::exit` taking the `libc_exit()` branch so `__cxa_atexit` handlers | ||
| // run (non-ASAN Linux uses `quick_exit()`, which skips them). The guard being | ||
| // tested is `cfg!(debug_assertions)`-only, which ASAN builds enable. | ||
| test.skipIf(!isLinux || !isASAN)( | ||
| "process.exit with a thread-pool fs.write still blocked in the kernel exits cleanly", | ||
| async () => { | ||
| using dir = tempDir("fs-write-exit-race", { | ||
| "helper.c": ` | ||
| extern int close(int); | ||
| extern int usleep(unsigned int); | ||
| extern int __cxa_atexit(void (*)(void *), void *, void *); | ||
| static int g_fd = -1; | ||
| static void do_close_and_sleep(void *unused) { | ||
| (void)unused; | ||
| if (g_fd >= 0) close(g_fd); | ||
| usleep(300000); | ||
| } | ||
| void schedule_close_at_exit(int fd) { | ||
| g_fd = fd; | ||
| __cxa_atexit(do_close_and_sleep, 0, 0); | ||
| } | ||
| `, | ||
| "child.js": ` | ||
| const fs = require("node:fs"); | ||
| const path = require("node:path"); | ||
| const cp = require("node:child_process"); | ||
| const { cc, FFIType } = require("bun:ffi"); | ||
|
|
||
| const fifo = path.join(__dirname, "pipe"); | ||
| cp.spawnSync("mkfifo", [fifo]); | ||
|
|
||
| const rd = fs.openSync(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK); | ||
| const wd = fs.openSync(fifo, fs.constants.O_WRONLY); | ||
|
|
||
| const { symbols } = cc({ | ||
| source: path.join(__dirname, "helper.c"), | ||
| symbols: { | ||
| schedule_close_at_exit: { args: [FFIType.i32], returns: FFIType.void }, | ||
| }, | ||
| }); | ||
| symbols.schedule_close_at_exit(rd); | ||
|
|
||
| // Larger than any pipe capacity so write() blocks in the kernel until | ||
| // the reader fd closes. | ||
| fs.write(wd, Buffer.alloc(1 << 21, 0x41), () => {}); | ||
|
|
||
| // Wait until the work-pool thread has entered the kernel write(): rd is | ||
| // O_NONBLOCK, so readSync returns >0 once bytes have landed in the pipe. | ||
| // Consuming one byte does not unblock the 2 MiB writer. | ||
| const buf = Buffer.alloc(1); | ||
| const deadline = Date.now() + 5000; | ||
| let sawBytes = false; | ||
| while (Date.now() < deadline) { | ||
| try { if (fs.readSync(rd, buf) > 0) { sawBytes = true; break; } } catch (e) { if (e.code !== "EAGAIN") throw e; } | ||
| Bun.sleepSync(1); | ||
| } | ||
|
robobun marked this conversation as resolved.
|
||
| if (!sawBytes) { | ||
| process.stderr.write("writer never entered kernel within 5s\\n"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| process.stdout.write("alive\\n"); | ||
| process.exit(0); | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "child.js"], | ||
| cwd: String(dir), | ||
| env: { | ||
| ...bunEnv, | ||
| BUN_DESTRUCT_VM_ON_EXIT: "1", | ||
| ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "detect_leaks=0"].filter(Boolean).join(":"), | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect({ stdout, stderr, exitCode }).toEqual({ stdout: "alive\n", stderr: "", exitCode: 0 }); | ||
| }, | ||
| // On an unfixed debug build the panic hook symbolizes the backtrace through | ||
| // llvm-symbolizer (~5s) before the process terminates. | ||
| 15_000, | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.