This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Commit cc38965 1 parent 77b4711 commit cc38965 Copy full SHA for cc38965
File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change
1
+ Fix stack overflow when stderr is redirected to the logging system, and the logging system encounters an error.
Original file line number Diff line number Diff line change 17
17
import logging .config
18
18
import os
19
19
import sys
20
+ import threading
20
21
from string import Template
21
22
22
23
import yaml
25
26
ILogObserver ,
26
27
LogBeginner ,
27
28
STDLibLogObserver ,
29
+ eventAsText ,
28
30
globalLogBeginner ,
29
31
)
30
32
@@ -216,8 +218,9 @@ def factory(*args, **kwargs):
216
218
# system.
217
219
observer = STDLibLogObserver ()
218
220
219
- def _log ( event ):
221
+ threadlocal = threading . local ()
220
222
223
+ def _log (event ):
221
224
if "log_text" in event :
222
225
if event ["log_text" ].startswith ("DNSDatagramProtocol starting on " ):
223
226
return
@@ -228,7 +231,25 @@ def _log(event):
228
231
if event ["log_text" ].startswith ("Timing out client" ):
229
232
return
230
233
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
232
253
233
254
logBeginner .beginLoggingTo ([_log ], redirectStandardIO = not config .no_redirect_stdio )
234
255
if not config .no_redirect_stdio :
You can’t perform that action at this time.
0 commit comments