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

Skip to content

Commit 3d1e2e4

Browse files
committed
Closes #21149: Improved thread-safety in logging cleanup during interpreter shutdown.
2 parents a5a294e + 156307b commit 3d1e2e4

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
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #21149: Improved thread-safety in logging cleanup during interpreter
33+
shutdown. Thanks to Devin Jeanpierre for the patch.
34+
3235
- Issue #21058: Fix a leak of file descriptor in
3336
:func:`tempfile.NamedTemporaryFile`, close the file descriptor if
3437
:func:`io.open` fails

0 commit comments

Comments
 (0)