Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix stack overflow when logging system encounters an error
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Sep 7, 2020
1 parent 77b4711 commit cc38965
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/8268.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix stack overflow when stderr is redirected to the logging system, and the logging system encounters an error.
25 changes: 23 additions & 2 deletions synapse/config/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging.config
import os
import sys
import threading
from string import Template

import yaml
Expand All @@ -25,6 +26,7 @@
ILogObserver,
LogBeginner,
STDLibLogObserver,
eventAsText,
globalLogBeginner,
)

Expand Down Expand Up @@ -216,8 +218,9 @@ def factory(*args, **kwargs):
# system.
observer = STDLibLogObserver()

def _log(event):
threadlocal = threading.local()

def _log(event):
if "log_text" in event:
if event["log_text"].startswith("DNSDatagramProtocol starting on "):
return
Expand All @@ -228,7 +231,25 @@ def _log(event):
if event["log_text"].startswith("Timing out client"):
return

return observer(event)
# 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)
finally:
threadlocal.active = False

logBeginner.beginLoggingTo([_log], redirectStandardIO=not config.no_redirect_stdio)
if not config.no_redirect_stdio:
Expand Down

0 comments on commit cc38965

Please sign in to comment.