-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(watcher): handle vim atomic save race on macOS #23566
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ecc9dbd
WIP
Jarred-Sumner 8bc9628
fix(watcher): handle vim atomic save race on macOS
Jarred-Sumner b54f24d
cleanup
Jarred-Sumner b0de2bd
Update hot_reloader.zig
Jarred-Sumner c41cc8c
fix: address CodeRabbit review feedback
922afd0
fix: pass address of path array to faccessat on macOS
150dc37
fix: prevent file descriptor leak on macOS in addFileByPathSlow
0cb8d87
docs: add documentation for addFileDescriptorToKQueueWithoutChecks an…
d6e5223
docs: add documentation for addFileByPathSlow and validate file_path
011f2e4
Update src/Watcher.zig
Jarred-Sumner a758ab2
Update src/bun.js/VirtualMachine.zig
Jarred-Sumner 1ff1b6a
Update src/bun.js/hot_reloader.zig
Jarred-Sumner adb0b83
Update src/bun.js/hot_reloader.zig
Jarred-Sumner d7532e1
Update src/bun.js/hot_reloader.zig
Jarred-Sumner 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 |
|---|---|---|
|
|
@@ -312,6 +312,48 @@ fn watchLoop(this: *Watcher) bun.sys.Maybe(void) { | |
| return .success; | ||
| } | ||
|
|
||
| /// Register a file descriptor with kqueue on macOS without validation. | ||
| /// | ||
| /// Preconditions (caller must ensure): | ||
| /// - `fd` is a valid, open file descriptor | ||
| /// - `fd` is not already registered with this kqueue | ||
| /// - `watchlist_id` matches the entry's index in the watchlist | ||
| /// | ||
| /// Note: This function does not propagate kevent registration errors. | ||
| /// If registration fails, the file will not be watched but no error is returned. | ||
| pub fn addFileDescriptorToKQueueWithoutChecks(this: *Watcher, fd: bun.FileDescriptor, watchlist_id: usize) void { | ||
| const KEvent = std.c.Kevent; | ||
|
|
||
| // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/kqueue.2.html | ||
| var event = std.mem.zeroes(KEvent); | ||
|
|
||
| event.flags = std.c.EV.ADD | std.c.EV.CLEAR | std.c.EV.ENABLE; | ||
| // we want to know about the vnode | ||
| event.filter = std.c.EVFILT.VNODE; | ||
|
|
||
| event.fflags = std.c.NOTE.WRITE | std.c.NOTE.RENAME | std.c.NOTE.DELETE; | ||
|
|
||
| // id | ||
| event.ident = @intCast(fd.native()); | ||
|
|
||
| // Store the index for fast filtering later | ||
| event.udata = @as(usize, @intCast(watchlist_id)); | ||
| var events: [1]KEvent = .{event}; | ||
|
|
||
| // This took a lot of work to figure out the right permutation | ||
| // Basically: | ||
| // - We register the event here. | ||
| // our while(true) loop above receives notification of changes to any of the events created here. | ||
| _ = std.posix.system.kevent( | ||
| this.platform.fd.unwrap().?.native(), | ||
| @as([]KEvent, events[0..1]).ptr, | ||
| 1, | ||
| @as([]KEvent, events[0..1]).ptr, | ||
| 0, | ||
| null, | ||
| ); | ||
| } | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| fn appendFileAssumeCapacity( | ||
| this: *Watcher, | ||
| fd: bun.FileDescriptor, | ||
|
|
@@ -350,36 +392,7 @@ fn appendFileAssumeCapacity( | |
| }; | ||
|
|
||
| if (comptime Environment.isMac) { | ||
| const KEvent = std.c.Kevent; | ||
|
|
||
| // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/kqueue.2.html | ||
| var event = std.mem.zeroes(KEvent); | ||
|
|
||
| event.flags = std.c.EV.ADD | std.c.EV.CLEAR | std.c.EV.ENABLE; | ||
| // we want to know about the vnode | ||
| event.filter = std.c.EVFILT.VNODE; | ||
|
|
||
| event.fflags = std.c.NOTE.WRITE | std.c.NOTE.RENAME | std.c.NOTE.DELETE; | ||
|
|
||
| // id | ||
| event.ident = @intCast(fd.native()); | ||
|
|
||
| // Store the hash for fast filtering later | ||
| event.udata = @as(usize, @intCast(watchlist_id)); | ||
| var events: [1]KEvent = .{event}; | ||
|
|
||
| // This took a lot of work to figure out the right permutation | ||
| // Basically: | ||
| // - We register the event here. | ||
| // our while(true) loop above receives notification of changes to any of the events created here. | ||
| _ = std.posix.system.kevent( | ||
| this.platform.fd.unwrap().?.native(), | ||
| @as([]KEvent, events[0..1]).ptr, | ||
| 1, | ||
| @as([]KEvent, events[0..1]).ptr, | ||
| 0, | ||
| null, | ||
| ); | ||
| this.addFileDescriptorToKQueueWithoutChecks(fd, watchlist_id); | ||
| } else if (comptime Environment.isLinux) { | ||
| // var file_path_to_use_ = std.mem.trimRight(u8, file_path_, "/"); | ||
| // var buf: [bun.MAX_PATH_BYTES+1]u8 = undefined; | ||
|
|
@@ -612,6 +625,68 @@ pub fn addDirectory( | |
| return this.appendDirectoryAssumeCapacity(fd, file_path, hash, clone_file_path); | ||
| } | ||
|
|
||
| pub fn addFileByPathSlow( | ||
| this: *Watcher, | ||
| file_path: string, | ||
| loader: options.Loader, | ||
| ) bool { | ||
| const hash = getHash(file_path); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Check if already watched (with lock to avoid race with removal) | ||
| { | ||
| this.mutex.lock(); | ||
| const already_watched = this.indexOf(hash) != null; | ||
| this.mutex.unlock(); | ||
|
|
||
| if (already_watched) { | ||
| return true; | ||
| } | ||
| } | ||
|
Comment on lines
+648
to
+656
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 is this a nested block? |
||
|
|
||
| // Only open fd if we might need it | ||
| var fd: bun.FileDescriptor = bun.invalid_fd; | ||
| if (Environment.isMac) { | ||
| const path_z = std.posix.toPosixPath(file_path) catch return false; | ||
| switch (bun.sys.open(&path_z, bun.c.O_EVTONLY, 0)) { | ||
| .result => |opened| fd = opened, | ||
| .err => return false, | ||
| } | ||
| } | ||
|
|
||
| const res = this.addFile(fd, file_path, hash, loader, bun.invalid_fd, null, true); | ||
| switch (res) { | ||
| .result => { | ||
| // On macOS, addFile may have found the file already watched (race) | ||
| // and returned success without using our fd. Close it if unused. | ||
| if (comptime Environment.isMac) { | ||
| if (fd.isValid()) { | ||
| this.mutex.lock(); | ||
| const maybe_idx = this.indexOf(hash); | ||
| const stored_fd = if (maybe_idx) |idx| | ||
| this.watchlist.items(.fd)[idx] | ||
| else | ||
| bun.invalid_fd; | ||
| this.mutex.unlock(); | ||
|
|
||
| // Only close if entry exists and stored fd differs from ours. | ||
| // Race scenarios: | ||
| // 1. Entry removed (maybe_idx == null): our fd was stored then closed by flushEvictions → don't close | ||
| // 2. Entry exists with different fd: another thread added entry, addFile didn't use our fd → close ours | ||
| // 3. Entry exists with same fd: our fd was stored → don't close | ||
| if (maybe_idx != null and stored_fd.native() != fd.native()) { | ||
| fd.close(); | ||
| } | ||
| } | ||
| } | ||
|
Jarred-Sumner marked this conversation as resolved.
Outdated
|
||
| return true; | ||
| }, | ||
| .err => { | ||
| if (fd.isValid()) fd.close(); | ||
| return false; | ||
| }, | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| pub fn addFile( | ||
| this: *Watcher, | ||
| fd: bun.FileDescriptor, | ||
|
|
||
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -677,12 +677,28 @@ pub fn uncaughtException(this: *jsc.VirtualMachine, globalObject: *JSGlobalObjec | |||||||||||||||||||
| return handled; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| pub fn handlePendingInternalPromiseRejection(this: *jsc.VirtualMachine) void { | ||||||||||||||||||||
| var promise = this.pending_internal_promise.?; | ||||||||||||||||||||
| pub fn reportExceptionInHotReloadedModuleIfNeeded(this: *jsc.VirtualMachine) void { | ||||||||||||||||||||
| const promise_opt = this.pending_internal_promise; | ||||||||||||||||||||
| if (promise_opt == null) { | ||||||||||||||||||||
| this.addMainToWatcherIfNeeded(); | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| var promise = promise_opt.?; | ||||||||||||||||||||
|
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. edit: see followup suggestion; this can be even simpler
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| if (promise.status(this.global.vm()) == .rejected and !promise.isHandled(this.global.vm())) { | ||||||||||||||||||||
| this.unhandledRejection(this.global, promise.result(this.global.vm()), promise.asValue()); | ||||||||||||||||||||
| promise.setHandled(this.global.vm()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| this.addMainToWatcherIfNeeded(); | ||||||||||||||||||||
|
Jarred-Sumner marked this conversation as resolved.
Outdated
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||
| pub fn addMainToWatcherIfNeeded(this: *jsc.VirtualMachine) void { | ||||||||||||||||||||
| if (this.isWatcherEnabled()) { | ||||||||||||||||||||
| const main = this.main; | ||||||||||||||||||||
| if (main.len == 0) return; | ||||||||||||||||||||
| _ = this.bun_watcher.addFileByPathSlow(main, this.transpiler.options.loader(std.fs.path.extension(main))); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| pub fn defaultOnUnhandledRejection(this: *jsc.VirtualMachine, _: *JSGlobalObject, value: JSValue) void { | ||||||||||||||||||||
|
|
||||||||||||||||||||
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.