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

Commit 77794eb

Browse files
authored
Fix stack overflow when logging system encounters an error (#8268)
1 parent 7586fdf commit 77794eb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

changelog.d/8268.bugfix

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

synapse/config/logger.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging.config
1818
import os
1919
import sys
20+
import threading
2021
from string import Template
2122

2223
import yaml
@@ -25,6 +26,7 @@
2526
ILogObserver,
2627
LogBeginner,
2728
STDLibLogObserver,
29+
eventAsText,
2830
globalLogBeginner,
2931
)
3032

@@ -216,8 +218,9 @@ def factory(*args, **kwargs):
216218
# system.
217219
observer = STDLibLogObserver()
218220

219-
def _log(event):
221+
threadlocal = threading.local()
220222

223+
def _log(event):
221224
if "log_text" in event:
222225
if event["log_text"].startswith("DNSDatagramProtocol starting on "):
223226
return
@@ -228,7 +231,25 @@ def _log(event):
228231
if event["log_text"].startswith("Timing out client"):
229232
return
230233

231-
return observer(event)
234+
# this is a workaround to make sure we don't get stack overflows when the
235+
# logging system raises an error which is written to stderr which is redirected
236+
# to the logging system, etc.
237+
if getattr(threadlocal, "active", False):
238+
# write the text of the event, if any, to the *real* stderr (which may
239+
# be redirected to /dev/null, but there's not much we can do)
240+
try:
241+
event_text = eventAsText(event)
242+
print("logging during logging: %s" % event_text, file=sys.__stderr__)
243+
except Exception:
244+
# gah.
245+
pass
246+
return
247+
248+
try:
249+
threadlocal.active = True
250+
return observer(event)
251+
finally:
252+
threadlocal.active = False
232253

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

0 commit comments

Comments
 (0)