-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Watcher: flush evict_list in remove() when full to prevent overflow #29939
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,76 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isWindows, tempDir } from "harness"; | ||
|
|
||
| // Regression test for `Watcher.evict_list` overflow. | ||
| // | ||
| // `Watcher.remove()` appends the watchlist index to a fixed-size | ||
| // `evict_list[max_eviction_count = 8096]` buffer; the actual removal | ||
| // happens in `flushEvictions()`, which was only driven by `onFileUpdate()` | ||
| // on the watcher thread — i.e. only when a filesystem event arrives. | ||
| // | ||
| // `fs.watch(path).close()` reaches `_decrementPathRefNoLock()` → | ||
| // `main_watcher.remove(hash)` for the watched path. Repeating that | ||
| // without ever modifying the filesystem means `flushEvictions()` never | ||
| // runs, and once the cumulative remove count passes 8096, | ||
| // `removeAtIndex()` writes past the end of `evict_list`: | ||
| // | ||
| // panic(main thread): index out of bounds: index 8096, len 8096 | ||
| // Watcher.removeAtIndex src/Watcher.zig | ||
| // Watcher.remove | ||
| // PathWatcherManager._decrementPathRefNoLock | ||
| // | ||
| // The fix drains `evict_list` inside `remove()` when it's full (the | ||
| // mutex is already held there, matching how `flushEvictions()` is | ||
| // invoked from the platform watch loops). | ||
| // | ||
| // Watching a single file (not a directory) keeps the test deterministic: | ||
| // file watches don't schedule a `DirectoryRegisterTask`, so `deinit()` | ||
| // runs to completion on every `close()` and exactly one `remove()` is | ||
| // issued per iteration; and the inotify file mask doesn't include | ||
| // IN_OPEN / IN_CLOSE so re-opening the fd each cycle doesn't generate | ||
| // events that would opportunistically flush. | ||
| // | ||
| // Windows uses win_watcher.zig (no evict_list). macOS/FreeBSD file | ||
| // watches go through KEventWatcher and hit the same `Watcher.remove()` | ||
| // → `evict_list` path, so they're covered too — only directory watches | ||
| // on macOS bypass it via FSEvents. | ||
| test.skipIf(isWindows)( | ||
| "Watcher.remove() does not overflow evict_list when no fs events fire", | ||
| async () => { | ||
| using dir = tempDir("fswatch-evict-overflow", { "f.txt": "x" }); | ||
|
|
||
| const fixture = /* js */ ` | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const target = path.join(process.argv[1], "f.txt"); | ||
| // > max_eviction_count (8096): one remove() per cycle, no fs events, | ||
| // so without the fix evict_list_i hits 8096 and removeAtIndex panics. | ||
| const ITERS = 8200; | ||
| for (let i = 0; i < ITERS; i++) { | ||
| const w = fs.watch(target, { persistent: false }, () => {}); | ||
| w.close(); | ||
| } | ||
| console.log("ok " + ITERS); | ||
| `; | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", fixture, String(dir)], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| const filteredStderr = stderr | ||
| .split("\n") | ||
| .filter(l => l && !l.startsWith("WARNING: ASAN interferes")) | ||
| .join("\n"); | ||
|
|
||
| expect({ stdout: stdout.trim(), stderr: filteredStderr, exitCode }).toEqual({ | ||
| stdout: "ok 8200", | ||
| stderr: "", | ||
| exitCode: 0, | ||
| }); | ||
| }, | ||
| 30000, | ||
| ); |
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.