]> granicus.if.org Git - python/commitdiff
Issue #21149: Improved thread-safety in logging cleanup during interpreter shutdown.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 4 Apr 2014 09:51:49 +0000 (10:51 +0100)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 4 Apr 2014 09:51:49 +0000 (10:51 +0100)
Lib/logging/__init__.py
Misc/NEWS

index 478c5afdae701d14787d05096a571baf05fdbbf4..181bc150d0f3a24be9ef75f072f510b7565519b6 100644 (file)
@@ -711,16 +711,17 @@ def _removeHandlerRef(wr):
     Remove a handler reference from the internal cleanup list.
     """
     # This function can be called during module teardown, when globals are
-    # set to None. If _acquireLock is None, assume this is the case and do
-    # nothing.
-    if (_acquireLock is not None and _handlerList is not None and
-        _releaseLock is not None):
-        _acquireLock()
+    # set to None. It can also be called from another thread. So we need to
+    # pre-emptively grab the necessary globals and check if they're None,
+    # to prevent race conditions and failures during interpreter shutdown.
+    acquire, release, handlers = _acquireLock, _releaseLock, _handlerList
+    if acquire and release and handlers:
+        acquire()
         try:
-            if wr in _handlerList:
-                _handlerList.remove(wr)
+            if wr in handlers:
+                handlers.remove(wr)
         finally:
-            _releaseLock()
+            release()
 
 def _addHandlerRef(handler):
     """
index 9e59154011eb2865a1186477e8f48711e154634f..4f27a991e94d0260fc3dd84a0661c87ccfd7f2e4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21149: Improved thread-safety in logging cleanup during interpreter
+  shutdown. Thanks to Devin Jeanpierre for the patch.
+
 - Issue #20145: `assertRaisesRegex` and `assertWarnsRegex` now raise a
   TypeError if the second argument is not a string or compiled regex.