fix: fix the fixes and add more error logging

This commit is contained in:
CJ van den Berg 2026-03-15 00:17:52 +01:00
parent eb42d23fc8
commit f60f9e6a21
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 30 additions and 8 deletions

View file

@ -93,7 +93,7 @@ pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
self.file_watches.deinit(allocator);
var sit = self.snapshots.iterator();
while (sit.next()) |entry| {
// Keys are borrowed from self.watches and freed in the watches loop above.
allocator.free(entry.key_ptr.*); // independently owned; see take_snapshot/scan_dir
var names = entry.value_ptr.*;
var nit = names.iterator();
while (nit.next()) |ne| allocator.free(ne.key_ptr.*);
@ -436,7 +436,15 @@ fn take_snapshot(self: *@This(), allocator: std.mem.Allocator, dir_path: []const
self.snapshots_mutex.lock();
errdefer self.snapshots_mutex.unlock();
const gop = try self.snapshots.getOrPut(allocator, dir_path);
if (!gop.found_existing) gop.value_ptr.* = .empty;
if (!gop.found_existing) {
// Snapshot outer keys are independently owned so they can be safely
// freed in deinit/remove_watch regardless of how the entry was created.
gop.key_ptr.* = allocator.dupe(u8, dir_path) catch |e| {
_ = self.snapshots.remove(dir_path);
return e;
};
gop.value_ptr.* = .empty;
}
var snapshot = gop.value_ptr;
for (names.items) |name| {
if (snapshot.contains(name)) continue;
@ -464,6 +472,7 @@ pub fn remove_watch(self: *@This(), allocator: std.mem.Allocator, path: []const
const snap_entry = self.snapshots.fetchRemove(path);
self.snapshots_mutex.unlock();
if (snap_entry) |entry| {
allocator.free(entry.key); // independently owned; see take_snapshot/scan_dir
var names = entry.value;
var it = names.iterator();
while (it.next()) |ne| {