Skip to content

Commit c34b0f9

Browse files
committed
Watch.zig: better account for resource cleanup
1 parent d558101 commit c34b0f9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/std/Build/Watch.zig

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,20 @@ const Os = switch (builtin.os.tag) {
266266
lpCompletionRoutine: windows.LPOVERLAPPED_COMPLETION_ROUTINE,
267267
) callconv(windows.WINAPI) windows.BOOL;
268268

269-
fn readChanges(self: *@This()) void {
269+
fn readChanges(self: *@This()) !void {
270270
const notify_filter =
271271
windows.FILE_NOTIFY_CHANGE_CREATION |
272272
windows.FILE_NOTIFY_CHANGE_DIR_NAME |
273273
windows.FILE_NOTIFY_CHANGE_FILE_NAME |
274274
windows.FILE_NOTIFY_CHANGE_LAST_WRITE |
275275
windows.FILE_NOTIFY_CHANGE_SIZE;
276276
const r = ReadDirectoryChangesW(self.handle, @ptrCast(&self.buffer), self.buffer.len, 0, notify_filter, null, &self.overlapped, null);
277-
assert(r != 0);
277+
if (r == 0) {
278+
switch (windows.GetLastError()) {
279+
.INVALID_FUNCTION => return error.ReadDirectoryChangesUnsupported,
280+
else => |err| return windows.unexpectedError(err),
281+
}
282+
}
278283
}
279284

280285
fn getWaitHandle(self: @This()) windows.HANDLE {
@@ -340,7 +345,7 @@ const Os = switch (builtin.os.tag) {
340345
offset += notify.NextEntryOffset;
341346
}
342347

343-
dir.readChanges();
348+
try dir.readChanges();
344349
return any_dirty;
345350
}
346351

@@ -408,18 +413,22 @@ const Os = switch (builtin.os.tag) {
408413
// directory on the file system.
409414
// In such case, we must revert adding this directory, but keep
410415
// the additions to the step set.
411-
const dh_gop = try w.os.handle_table.getOrPut(gpa, dir_handle);
416+
const dh_gop = w.os.handle_table.getOrPut(gpa, dir_handle) catch |err| {
417+
windows.CloseHandle(dir_handle);
418+
return err;
419+
};
412420
if (dh_gop.found_existing) {
413421
_ = w.dir_table.pop();
414422
} else {
415423
assert(dh_gop.index == gop.index);
416424
dh_gop.value_ptr.* = .{};
417425
const dir = try Os.Directory.init(gpa, dir_handle);
426+
errdefer dir.deinit(gpa);
427+
try dir.readChanges();
418428
try w.os.handle_extra.insert(gpa, dh_gop.index, .{
419429
.dir = dir,
420430
.wait_handle = dir.getWaitHandle(),
421431
});
422-
dir.readChanges();
423432
}
424433
break :rs &w.os.handle_table.values()[dh_gop.index];
425434
}

0 commit comments

Comments
 (0)