[Python-checkins] [2.7] bpo-21149: Workaround a GC finalization bug in logging. (#4368)

Gregory P. Smith webhook-mailer at python.org
Sat Nov 11 17:48:53 EST 2017


https://github.com/python/cpython/commit/e84f6d3817ad3d18fdfbb5223b8078606166cea0
commit: e84f6d3817ad3d18fdfbb5223b8078606166cea0
branch: 2.7
author: Gregory P. Smith <greg at krypto.org>
committer: GitHub <noreply at github.com>
date: 2017-11-11T14:48:49-08:00
summary:

[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.

files:
A Misc/NEWS.d/next/Library/2017-11-10-17-19-24.bpo-21149.8UVfeT.rst
M Lib/logging/__init__.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 22205825f3f..cc24b309009 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -636,12 +636,19 @@ def _removeHandlerRef(wr):
     # 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 handlers:
-                handlers.remove(wr)
-        finally:
-            release()
+            acquire()
+            try:
+                if wr in handlers:
+                    handlers.remove(wr)
+            finally:
+                release()
+        except TypeError:
+            # https://bugs.python.org/issue21149 - If the RLock object behind
+            # acquire() and release() has been partially finalized you may see
+            # an error about NoneType not being callable.  Absolutely nothing
+            # we can do in this GC during process shutdown situation.  Eat it.
+            pass
 
 def _addHandlerRef(handler):
     """
diff --git a/Misc/NEWS.d/next/Library/2017-11-10-17-19-24.bpo-21149.8UVfeT.rst b/Misc/NEWS.d/next/Library/2017-11-10-17-19-24.bpo-21149.8UVfeT.rst
new file mode 100644
index 00000000000..3c582fcfa89
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-11-10-17-19-24.bpo-21149.8UVfeT.rst
@@ -0,0 +1,3 @@
+Silence a `'NoneType' object is not callable` in `_removeHandlerRef` error
+that could happen when a logging Handler is destroyed as part of cyclic
+garbage collection during process shutdown.



More information about the Python-checkins mailing list