Skip to content

Commit e84f6d3

Browse files
authored
[2.7] bpo-21149: Workaround a GC finalization bug in logging. (#4368)
* Work around a GC process finalization bug. The logging RLock instances may exist but the threading.RLock class itself has already been emptied causing a Exception TypeError: "'NoneType' object is not callable" in <function _removeHandlerRef ..." to be printed to stderr on process termination. This catches that exception and ignores it because there is absolutely nothing we can or should do about it from the context of a weakref handler called from the gc context.
1 parent 6401e56 commit e84f6d3

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

‎Lib/logging/__init__.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,19 @@ def _removeHandlerRef(wr):
636636
# to prevent race conditions and failures during interpreter shutdown.
637637
acquire, release, handlers = _acquireLock, _releaseLock, _handlerList
638638
if acquire and release and handlers:
639-
acquire()
640639
try:
641-
if wr in handlers:
642-
handlers.remove(wr)
643-
finally:
644-
release()
640+
acquire()
641+
try:
642+
if wr in handlers:
643+
handlers.remove(wr)
644+
finally:
645+
release()
646+
except TypeError:
647+
# https://bugs.python.org/issue21149 - If the RLock object behind
648+
# acquire() and release() has been partially finalized you may see
649+
# an error about NoneType not being callable. Absolutely nothing
650+
# we can do in this GC during process shutdown situation. Eat it.
651+
pass
645652

646653
def _addHandlerRef(handler):
647654
"""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Silence a `'NoneType' object is not callable` in `_removeHandlerRef` error
2+
that could happen when a logging Handler is destroyed as part of cyclic
3+
garbage collection during process shutdown.

0 commit comments

Comments
 (0)