Fix stack overflow when logging system encounters an error (#8268)

This commit is contained in:
Richard van der Hoff 2020-09-07 16:54:30 +01:00 committed by GitHub
parent 7586fdf1e8
commit 77794ebc77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

1
changelog.d/8268.bugfix Normal file
View file

@ -0,0 +1 @@
Fix stack overflow when stderr is redirected to the logging system, and the logging system encounters an error.

View file

@ -17,6 +17,7 @@ import logging
import logging.config import logging.config
import os import os
import sys import sys
import threading
from string import Template from string import Template
import yaml import yaml
@ -25,6 +26,7 @@ from twisted.logger import (
ILogObserver, ILogObserver,
LogBeginner, LogBeginner,
STDLibLogObserver, STDLibLogObserver,
eventAsText,
globalLogBeginner, globalLogBeginner,
) )
@ -216,8 +218,9 @@ def _setup_stdlib_logging(config, log_config, logBeginner: LogBeginner):
# system. # system.
observer = STDLibLogObserver() observer = STDLibLogObserver()
def _log(event): threadlocal = threading.local()
def _log(event):
if "log_text" in event: if "log_text" in event:
if event["log_text"].startswith("DNSDatagramProtocol starting on "): if event["log_text"].startswith("DNSDatagramProtocol starting on "):
return return
@ -228,7 +231,25 @@ def _setup_stdlib_logging(config, log_config, logBeginner: LogBeginner):
if event["log_text"].startswith("Timing out client"): if event["log_text"].startswith("Timing out client"):
return return
# this is a workaround to make sure we don't get stack overflows when the
# logging system raises an error which is written to stderr which is redirected
# to the logging system, etc.
if getattr(threadlocal, "active", False):
# write the text of the event, if any, to the *real* stderr (which may
# be redirected to /dev/null, but there's not much we can do)
try:
event_text = eventAsText(event)
print("logging during logging: %s" % event_text, file=sys.__stderr__)
except Exception:
# gah.
pass
return
try:
threadlocal.active = True
return observer(event) return observer(event)
finally:
threadlocal.active = False
logBeginner.beginLoggingTo([_log], redirectStandardIO=not config.no_redirect_stdio) logBeginner.beginLoggingTo([_log], redirectStandardIO=not config.no_redirect_stdio)
if not config.no_redirect_stdio: if not config.no_redirect_stdio: