diff --git a/src/backend/KQueue.zig b/src/backend/KQueue.zig index a8f6d4d..77e8aed 100644 --- a/src/backend/KQueue.zig +++ b/src/backend/KQueue.zig @@ -274,6 +274,12 @@ fn register_file_watch(self: *@This(), allocator: std.mem.Allocator, path: []con return; }; self.file_watches_mutex.lock(); + if (self.file_watches.contains(path)) { + self.file_watches_mutex.unlock(); + std.posix.close(fd); + allocator.free(owned); + return; + } self.file_watches.put(allocator, owned, fd) catch { self.file_watches_mutex.unlock(); std.posix.close(fd); @@ -350,6 +356,12 @@ pub fn add_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) }; const owned_path = try allocator.dupe(u8, path); self.watches_mutex.lock(); + if (self.watches.contains(path)) { + self.watches_mutex.unlock(); + std.posix.close(path_fd); + allocator.free(owned_path); + return; + } self.watches.put(allocator, owned_path, path_fd) catch |e| { self.watches_mutex.unlock(); allocator.free(owned_path); diff --git a/src/backend/KQueueDir.zig b/src/backend/KQueueDir.zig index 8908ec0..6bbf770 100644 --- a/src/backend/KQueueDir.zig +++ b/src/backend/KQueueDir.zig @@ -315,6 +315,12 @@ pub fn add_watch(self: *@This(), allocator: std.mem.Allocator, path: []const u8) const is_file = if (stat) |s| std.posix.S.ISREG(s.mode) else false; const owned_path = try allocator.dupe(u8, path); self.watches_mutex.lock(); + if (self.watches.contains(path)) { + self.watches_mutex.unlock(); + std.posix.close(path_fd); + allocator.free(owned_path); + return; + } self.watches.put(allocator, owned_path, .{ .fd = path_fd, .is_file = is_file }) catch |e| { self.watches_mutex.unlock(); allocator.free(owned_path);