-
-
Notifications
You must be signed in to change notification settings - Fork 825
fix(watcher): serialize watch and close on the native watcher #14757
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -470,8 +470,8 @@ export declare class NativeWatcher { | |
| * # Safety | ||
| * | ||
| * This function is unsafe because it uses `&mut self` to call the watcher asynchronously. | ||
| * It's important to ensure that the watcher is not used in any other places before this function is finished. | ||
| * You must ensure that the watcher not call watch, close or pause in the same time, otherwise it may lead to undefined behavior. | ||
| * `watch` and `close` are serialized internally, but calling `pause` while this function is | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this api's semantic is not same as js version |
||
| * in flight is still undefined behavior. | ||
| */ | ||
| close(): Promise<void> | ||
| pause(): void | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why need spawn_handle here and what does spawn_handle mean here? |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| const fs = require("node:fs"); | ||
| const os = require("node:os"); | ||
| const path = require("node:path"); | ||
| const binding = require("@rspack/binding"); | ||
|
|
||
| const ITERATIONS = 30; | ||
| const WATCHES_PER_ITERATION = 5; | ||
| const FILE_COUNT = 400; | ||
|
|
||
| let dir; | ||
| let files; | ||
| let dirs; | ||
|
|
||
| const startWatch = watcher => { | ||
| watcher.watch( | ||
| [files, []], | ||
| [dirs, []], | ||
| [[], []], | ||
| BigInt(Date.now()), | ||
| () => {}, | ||
| () => {} | ||
| ); | ||
| }; | ||
|
|
||
| describe("NativeWatcher lifecycle", () => { | ||
| beforeAll(() => { | ||
| dir = fs.mkdtempSync(path.join(os.tmpdir(), "rspack-native-watcher-")); | ||
| files = []; | ||
| dirs = [dir]; | ||
| for (let i = 0; i < FILE_COUNT; i++) { | ||
| const sub = path.join(dir, `sub${i % 20}`); | ||
| fs.mkdirSync(sub, { recursive: true }); | ||
| const file = path.join(sub, `f${i}.js`); | ||
| fs.writeFileSync(file, `module.exports = ${i};`); | ||
| files.push(file); | ||
| if (!dirs.includes(sub)) { | ||
| dirs.push(sub); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("does not crash when watch and close race on the same watcher", async () => { | ||
| for (let i = 0; i < ITERATIONS; i++) { | ||
| const watcher = new binding.NativeWatcher({ aggregateTimeout: 1 }); | ||
|
|
||
| for (let j = 0; j < WATCHES_PER_ITERATION; j++) { | ||
| startWatch(watcher); | ||
| } | ||
|
|
||
| await watcher.close(); | ||
|
|
||
| expect(() => startWatch(watcher)).toThrow(/has been closed/); | ||
| } | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so calling pause while watching still can cause panic | abort issue?