Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 156307b

Browse files
committed
Issue #21149: Improved thread-safety in logging cleanup during interpreter shutdown.
1 parent b30b34c commit 156307b

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

Lib/logging/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,16 +711,17 @@ def _removeHandlerRef(wr):
711711
Remove a handler reference from the internal cleanup list.
712712
"""
713713
# This function can be called during module teardown, when globals are
714-
# set to None. If _acquireLock is None, assume this is the case and do
715-
# nothing.
716-
if (_acquireLock is not None and _handlerList is not None and
717-
_releaseLock is not None):
718-
_acquireLock()
714+
# set to None. It can also be called from another thread. So we need to
715+
# pre-emptively grab the necessary globals and check if they're None,
716+
# to prevent race conditions and failures during interpreter shutdown.
717+
acquire, release, handlers = _acquireLock, _releaseLock, _handlerList
718+
if acquire and release and handlers:
719+
acquire()
719720
try:
720-
if wr in _handlerList:
721-
_handlerList.remove(wr)
721+
if wr in handlers:
722+
handlers.remove(wr)
722723
finally:
723-
_releaseLock()
724+
release()
724725

725726
def _addHandlerRef(handler):
726727
"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #21149: Improved thread-safety in logging cleanup during interpreter
31+
shutdown. Thanks to Devin Jeanpierre for the patch.
32+
3033
- Issue #20145: `assertRaisesRegex` and `assertWarnsRegex` now raise a
3134
TypeError if the second argument is not a string or compiled regex.
3235

0 commit comments

Comments
 (0)