mirror of https://github.com/fail2ban/fail2ban
file filter-backends: stability fix for sporadically errors - always close file handle, otherwise may be locked (prevent log-rotate, etc.)
parent
4404642fa3
commit
2108216d33
|
@ -799,49 +799,48 @@ class FileFilter(Filter):
|
||||||
if log is None:
|
if log is None:
|
||||||
logSys.error("Unable to get failures in " + filename)
|
logSys.error("Unable to get failures in " + filename)
|
||||||
return False
|
return False
|
||||||
# Try to open log file.
|
# We should always close log (file), otherwise may be locked (log-rotate, etc.)
|
||||||
try:
|
try:
|
||||||
has_content = log.open()
|
# Try to open log file.
|
||||||
# see http://python.org/dev/peps/pep-3151/
|
|
||||||
except IOError as e:
|
|
||||||
logSys.error("Unable to open %s" % filename)
|
|
||||||
logSys.exception(e)
|
|
||||||
return False
|
|
||||||
except OSError as e: # pragma: no cover - requires race condition to tigger this
|
|
||||||
logSys.error("Error opening %s" % filename)
|
|
||||||
logSys.exception(e)
|
|
||||||
return False
|
|
||||||
except Exception as e: # pragma: no cover - Requires implemention error in FileContainer to generate
|
|
||||||
logSys.error("Internal error in FileContainer open method - please report as a bug to https://github.com/fail2ban/fail2ban/issues")
|
|
||||||
logSys.exception(e)
|
|
||||||
return False
|
|
||||||
|
|
||||||
# seek to find time for first usage only (prevent performance decline with polling of big files)
|
|
||||||
if self.__autoSeek.get(filename):
|
|
||||||
startTime = self.__autoSeek[filename]
|
|
||||||
del self.__autoSeek[filename]
|
|
||||||
# prevent completely read of big files first time (after start of service),
|
|
||||||
# initial seek to start time using half-interval search algorithm:
|
|
||||||
try:
|
try:
|
||||||
self.seekToTime(log, startTime)
|
has_content = log.open()
|
||||||
except Exception as e: # pragma: no cover
|
# see http://python.org/dev/peps/pep-3151/
|
||||||
logSys.error("Error during seek to start time in \"%s\"", filename)
|
except IOError as e:
|
||||||
raise
|
logSys.error("Unable to open %s" % filename)
|
||||||
|
logSys.exception(e)
|
||||||
|
return False
|
||||||
|
except OSError as e: # pragma: no cover - requires race condition to tigger this
|
||||||
|
logSys.error("Error opening %s" % filename)
|
||||||
|
logSys.exception(e)
|
||||||
|
return False
|
||||||
|
except Exception as e: # pragma: no cover - Requires implemention error in FileContainer to generate
|
||||||
|
logSys.error("Internal error in FileContainer open method - please report as a bug to https://github.com/fail2ban/fail2ban/issues")
|
||||||
logSys.exception(e)
|
logSys.exception(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# yoh: has_content is just a bool, so do not expect it to
|
# seek to find time for first usage only (prevent performance decline with polling of big files)
|
||||||
# change -- loop is exited upon break, and is not entered at
|
if self.__autoSeek.get(filename):
|
||||||
# all if upon container opening that one was empty. If we
|
startTime = self.__autoSeek[filename]
|
||||||
# start reading tested to be empty container -- race condition
|
del self.__autoSeek[filename]
|
||||||
# might occur leading at least to tests failures.
|
# prevent completely read of big files first time (after start of service),
|
||||||
while has_content:
|
# initial seek to start time using half-interval search algorithm:
|
||||||
line = log.readline()
|
try:
|
||||||
if not line or not self.active:
|
self.seekToTime(log, startTime)
|
||||||
# The jail reached the bottom or has been stopped
|
except Exception as e: # pragma: no cover
|
||||||
break
|
logSys.error("Error during seek to start time in \"%s\"", filename)
|
||||||
self.processLineAndAdd(line)
|
raise
|
||||||
log.close()
|
logSys.exception(e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if has_content:
|
||||||
|
while not self.idle:
|
||||||
|
line = log.readline()
|
||||||
|
if not line or not self.active:
|
||||||
|
# The jail reached the bottom or has been stopped
|
||||||
|
break
|
||||||
|
self.processLineAndAdd(line)
|
||||||
|
finally:
|
||||||
|
log.close()
|
||||||
db = self.jail.database
|
db = self.jail.database
|
||||||
if db is not None:
|
if db is not None:
|
||||||
db.updateLog(self.jail, log)
|
db.updateLog(self.jail, log)
|
||||||
|
|
Loading…
Reference in New Issue