diff --git a/fsmonitor/win32.py b/fsmonitor/win32.py index 13f819b..645ab72 100644 --- a/fsmonitor/win32.py +++ b/fsmonitor/win32.py @@ -119,6 +119,7 @@ def __del__(self): self.close() def close(self): + self.remove_all_watches() if self.__cphandle is not None: win32file.CloseHandle(self.__cphandle) self.__cphandle = None @@ -129,11 +130,11 @@ def add_dir_watch(self, path, flags=FSEvent.All, user=None, recursive=False): watch = FSMonitorWatch(path, flags, user, recursive) with self.__lock: key = self.__last_key - win32file.CreateIoCompletionPort(watch._hDir, self.__cphandle, key, 0) self.__last_key += 1 - read_changes(watch) watch._key = key self.__key_to_watch[key] = watch + win32file.CreateIoCompletionPort(watch._hDir, self.__cphandle, key, 0) + read_changes(watch) return watch except pywintypes.error as e: raise FSMonitorWindowsError(*e.args) @@ -141,24 +142,28 @@ def add_dir_watch(self, path, flags=FSEvent.All, user=None, recursive=False): def add_file_watch(self, path, flags=FSEvent.All, user=None): raise NotImplementedError() - def __remove_watch(self, watch): + def remove_watch(self, watch): if not watch._removed: + watch._removed = True try: - watch._removed = True close_watch(watch) return True except pywintypes.error: pass return False - def remove_watch(self, watch): - with self.__lock: - return self.__remove_watch(watch) - def remove_all_watches(self): with self.__lock: - for watch in self.__key_to_watch.itervalues(): - self.__remove_watch(watch) + watches_to_close = [] + for watch in self.__key_to_watch.values(): + if not watch._removed: + watch._removed = True + watches_to_close.append(watch) + for watch in watches_to_close: + try: + close_watch(watch) + except pywintypes.error: + pass def enable_watch(self, watch, enable=True): watch.enabled = enable