-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fs.watch: fully initialize ConcurrentTask in FSWatchTask.enqueue #29950
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3a7a119
fs.watch: fully initialize ConcurrentTask in FSWatchTask.enqueue
robobun 28eacb7
[autofix.ci] apply automated fixes
autofix-ci[bot] 84fd430
test: wait for first event instead of fixed rounds (macOS FSEvents la…
robobun ef660e0
event_loop: check raw bits in ConcurrentTask.next assertion
robobun bb13b1e
test: sync header comment with raw-bits assertion; add explicit timeout
robobun ebfa3ff
[autofix.ci] apply automated fixes
autofix-ci[bot] daa04db
test: correct comments — FSWatcher.close() emits synchronously on POSIX
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
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,89 @@ | ||
| // Regression test for FSWatchTaskPosix.enqueue() leaving `concurrent_task.next` | ||
| // undefined. `PackedNextPtr.setPtr` preserves the low `auto_delete` bit, so when | ||
| // `.next` is undefined the preserved bit is garbage; if it reads as 1 the event | ||
| // loop calls `bun.destroy` on the *embedded* ConcurrentTask (an interior pointer) | ||
| // and corrupts the heap. | ||
| // | ||
| // The fix uses `ConcurrentTask.from(that, .manual_deinit)` which fully initializes | ||
| // both `.task` and `.next`. A debug assertion in `enqueueTaskConcurrent` verifies | ||
| // the pointer bits of `.next` are zero before push, which fires on any regression | ||
| // of this pattern (Zig's debug `undefined` = 0xAA..AA → non-zero pointer bits). | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isWindows, tempDir } from "harness"; | ||
|
|
||
| // FSWatchTaskPosix is the POSIX-only code path. | ||
| test.skipIf(isWindows)( | ||
| "fs.watch: FSWatchTask enqueue fully initializes ConcurrentTask", | ||
| async () => { | ||
| using dir = tempDir("fswatch-concurrent-task", { | ||
| "a.txt": "a", | ||
| "b.txt": "b", | ||
| "c.txt": "c", | ||
| "d.txt": "d", | ||
| }); | ||
|
|
||
| // The regression signal here is the debug assertion / no heap corruption in | ||
| // FSWatchTask.enqueue(), not event-delivery count. On macOS, directory watches | ||
| // route through FSEvents which has ~50ms coalescing latency and async stream | ||
| // registration, so we wait for the first event before counting stress rounds | ||
| // rather than assuming a fixed number of setImmediate turns is "enough time". | ||
| const fixture = /* js */ ` | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const dir = ${JSON.stringify(String(dir))}; | ||
|
|
||
| let received = 0; | ||
| const watchers = []; | ||
| // Many watchers → many FSWatchTask.enqueue() calls per batch of fs events. | ||
| for (let i = 0; i < 64; i++) { | ||
| watchers.push(fs.watch(dir, () => { received++; })); | ||
| } | ||
|
|
||
| function done() { | ||
| for (const w of watchers) w.close(); | ||
| // Allow the close tasks (also routed via enqueue) to drain. | ||
| setImmediate(() => { | ||
| console.log("OK " + received); | ||
| process.exit(0); | ||
| }); | ||
| } | ||
|
|
||
| const files = ["a.txt", "b.txt", "c.txt", "d.txt"]; | ||
| function write() { | ||
| for (const f of files) fs.writeFileSync(path.join(dir, f), "v" + received); | ||
| } | ||
|
|
||
| // Phase 1: write until the first event arrives (condition, not time). | ||
| const started = Date.now(); | ||
| (async () => { | ||
| while (received === 0) { | ||
| write(); | ||
| await new Promise(r => setImmediate(r)); | ||
| // Give up waiting for delivery after a generous bound; the assertion / | ||
| // heap check is still exercised by close() even if no events arrived. | ||
| if (Date.now() - started > 10_000) return done(); | ||
|
Check warning on line 64 in test/js/node/watch/fs.watch.concurrent-task.test.ts
|
||
|
claude[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| // Phase 2: now that enqueue() is known to be firing, stress it. | ||
| for (let round = 0; round < 50; round++) { | ||
| write(); | ||
| await new Promise(r => setImmediate(r)); | ||
| } | ||
| done(); | ||
| })(); | ||
| `; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stderr).toBe(""); | ||
| expect(stdout).toStartWith("OK "); | ||
| expect(exitCode).toBe(0); | ||
| }, | ||
| 30_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.