-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fs_events(darwin): fix broken double-checked locking on fsevents_default_loop #30111
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3d75e55
fs_events(darwin): drop unlocked fast path on fsevents_default_loop /…
robobun b267506
[autofix.ci] apply automated fixes
autofix-ci[bot] 91223d9
test: track worker completion via 'exit' so a silent worker death can…
robobun f5d25eb
test: drop vestigial parentPort.postMessage now that completion is tr…
robobun 9fea19c
test: update fs_events.zig path in comment after src/ restructure
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,97 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isMacOS, tempDir } from "harness"; | ||
|
|
||
| // Regression test for broken double-checked locking on `fsevents_default_loop` | ||
| // in src/bun.js/node/fs_events.zig. | ||
|
Check warning on line 5 in test/js/node/watch/fs.watch.worker-init-race.test.ts
|
||
| // | ||
| // `FSEvents.watch()` is called from `Darwin.addWatch` (path_watcher.zig) | ||
| // WITHOUT holding `manager.mutex` (it's released first to keep lock order | ||
| // one-way). Two Workers can therefore enter `FSEvents.watch()` concurrently. | ||
| // | ||
| // Before the fix the function read `fsevents_default_loop` with no lock and | ||
| // no acquire fence; only the else-branch took `fsevents_default_loop_mutex`. | ||
| // On ARM64 Worker A's store of the pointer could become visible to Worker B | ||
| // before the stores inside `FSEventsLoop.init()` (`this.* = fs_loop`), so | ||
| // Worker B would call `registerWatcher()` on a partially-visible loop and | ||
| // lock a garbage `loop.mutex` / read a garbage `loop.watchers` BabyList. | ||
| // `CoreFoundation.get()` / `CoreServices.get()` had the identical pattern. | ||
| // | ||
| // This is `path_watcher.zig`'s own `PathWatcherManager.get()` comment applied | ||
| // to `fs_events.zig`: drop the unlocked fast path; the mutex is uncontended | ||
| // after initialization. | ||
| // | ||
| // The race requires (a) the very first `fs.watch()` in the process to happen | ||
| // on two threads at once and (b) store reordering, so it is low-probability | ||
| // even on Apple Silicon. This test spawns a fresh process per iteration so | ||
| // the loop is uninitialized each time, and fires several Workers that all | ||
| // call `fs.watch()` as their first statement on distinct directories (so | ||
| // `PathWatcherManager` dedup doesn't serialize them). | ||
| // | ||
| // macOS-only: the FSEvents code path doesn't exist on other platforms. | ||
| test.skipIf(!isMacOS)( | ||
| "FSEvents: concurrent first fs.watch() from Workers does not observe a partially-initialized loop", | ||
| async () => { | ||
| const WORKERS = 8; | ||
| const files: Record<string, string> = {}; | ||
| for (let i = 0; i < WORKERS; i++) files[`d${i}/f.txt`] = "x"; | ||
| files["worker.js"] = ` | ||
| const fs = require("fs"); | ||
| const { workerData } = require("worker_threads"); | ||
| // First thing this thread does: hit FSEvents.watch() via Darwin.addWatch | ||
| // with manager.mutex released. Multiple Workers race here on a fresh | ||
| // process so fsevents_default_loop starts null. | ||
| const w = fs.watch(workerData.dir, () => {}); | ||
| w.close(); | ||
| `; | ||
| files["main.js"] = ` | ||
| const path = require("path"); | ||
| const { Worker } = require("worker_threads"); | ||
| const root = process.argv[2]; | ||
| const N = ${WORKERS}; | ||
| let done = 0; | ||
| let failed = false; | ||
| for (let i = 0; i < N; i++) { | ||
| const w = new Worker(path.join(root, "worker.js"), { | ||
| workerData: { dir: path.join(root, "d" + i) }, | ||
| }); | ||
| w.on("error", err => { | ||
| failed = true; | ||
| console.error("worker error:", err); | ||
| process.exit(1); | ||
| }); | ||
| // Track completion via 'exit' (fires exactly once per worker no matter | ||
| // how it ends) so a worker that dies without posting can't hang us. | ||
| w.on("exit", code => { | ||
| if (code !== 0 && !failed) { | ||
| failed = true; | ||
| console.error("worker exited with code", code); | ||
| process.exit(1); | ||
| } | ||
| if (++done === N && !failed) { | ||
| console.log("OK"); | ||
| process.exit(0); | ||
| } | ||
| }); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| `; | ||
|
|
||
| using dir = tempDir("fsevents-worker-init-race", files); | ||
|
|
||
| // Fresh process each iteration so the FSEvents loop global starts null | ||
| // and the DCLP race window exists every time. | ||
| for (let i = 0; i < 20; i++) { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "main.js", String(dir)], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout.trim()).toBe("OK"); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| expect(exitCode).toBe(0); | ||
| } | ||
| }, | ||
| 60_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.