From 53bb8eeeadb037a450df7db1ef4e0adb2f239af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 14 Mar 2018 16:39:57 +0100 Subject: [PATCH] Fix cleanup code for directory watchers in the WinAPI driver. - Fixes an assertion triggered by accessing a file field in releaseRef - Fixed a potential leak of file system watcher handles when calling releaseRef from inside the supplied callback - this was the reason why the fixed assertion error wasn't triggered by the test code --- source/eventcore/drivers/winapi/watchers.d | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/eventcore/drivers/winapi/watchers.d b/source/eventcore/drivers/winapi/watchers.d index 9606148..702ba0c 100644 --- a/source/eventcore/drivers/winapi/watchers.d +++ b/source/eventcore/drivers/winapi/watchers.d @@ -82,7 +82,7 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { catch (Exception e) assert(false, "Freeing directory watcher buffer failed."); } (); slot.watcher.buffer = null; - core.discardEvents(&slot.watcher.overlapped, &slot.file.write.overlapped); + core.discardEvents(&slot.watcher.overlapped); core.freeSlot(handle); })) { @@ -159,10 +159,13 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { catch (Exception e) {} // FIXME: can happen if the base path is relative and the CWD has changed if (ch.kind != FileChangeKind.modified || !ch.isDirectory) slot.callback(id, ch); - if (fni.NextEntryOffset == 0) break; + if (fni.NextEntryOffset == 0 || !slot.callback) break; } - triggerRead(handle, *slot); + if (slot.callback) + triggerRead(handle, *slot); + else if (gslot.refCount == 1) + doReleaseRef(handle); } private static bool triggerRead(HANDLE handle, ref WatcherSlot slot)