Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-29001 Update handlers.py to address abnormal behavior when using RotatingFileHandler under gunicorn multiple workers #9616

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion Lib/logging/handlers.py
Expand Up @@ -183,8 +183,22 @@ def shouldRollover(self, record):
if self.maxBytes > 0: # are we rolling over?
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
if self.stream.tell() + len(msg) >= self.maxBytes:
"""
For gunicorn running with multiple workers, each worker will have
separate RotatingFileHandler instances. Due to this, when rollover occurs,
the effect is reflected only for the current worker. To propagate the rollover
for all workers, both stream_size and file_size has to be checked and take action
accordingly.
"""
file_size = os.stat(self.baseFilename).st_size + len(msg)
stream_size = self.stream.tell() + len(msg)
if file_size >= self.maxBytes:
#Rollover Yes!
return 1
if stream_size >= self.maxBytes:
#Rollover already done by previous worker; just update the stream.
self.stream.close()
self.stream = open(self.baseFilename, 'a')
return 0

class TimedRotatingFileHandler(BaseRotatingHandler):
Expand Down