From 4404642fa32d4626500d784e1569a8e94c949833 Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 9 Sep 2016 10:56:35 +0200 Subject: [PATCH] pyinotify-backend: stability fix for sporadically errors in multi-threaded environment (without lock) --- fail2ban/server/filterpyinotify.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/fail2ban/server/filterpyinotify.py b/fail2ban/server/filterpyinotify.py index c5b5680b..1e08f250 100644 --- a/fail2ban/server/filterpyinotify.py +++ b/fail2ban/server/filterpyinotify.py @@ -119,14 +119,15 @@ class FilterPyinotify(FileFilter): logSys.debug("Added file watcher for %s", path) def _delFileWatcher(self, path): - wdInt = self.__watches[path] - wd = self.__monitor.rm_watch(wdInt) - if wd[wdInt]: - del self.__watches[path] - logSys.debug("Removed file watcher for %s", path) - return True - else: - return False + try: + wdInt = self.__watches.pop(path) + wd = self.__monitor.rm_watch(wdInt) + if wd[wdInt]: + logSys.debug("Removed file watcher for %s", path) + return True + except KeyError: # pragma: no cover + pass + return False ## # Add a log file path @@ -158,8 +159,11 @@ class FilterPyinotify(FileFilter): if k.startswith(path_dir + pathsep)]): # Remove watches for the directory # since there is no other monitored file under this directory - wdInt = self.__watches.pop(path_dir) - self.__monitor.rm_watch(wdInt) + try: + wdInt = self.__watches.pop(path_dir) + self.__monitor.rm_watch(wdInt) + except KeyError: # pragma: no cover + pass logSys.debug("Removed monitor for the parent directory %s", path_dir) # pyinotify.ProcessEvent default handler: