changeset: 90125:76689a706900 parent: 90122:d9337fe963cc parent: 90124:b5c91b61991a user: Vinay Sajip date: Fri Apr 04 10:57:25 2014 +0100 files: Misc/NEWS description: Closes #21149: Improved thread-safety in logging cleanup during interpreter shutdown. diff -r d9337fe963cc -r 76689a706900 Lib/logging/__init__.py --- a/Lib/logging/__init__.py Thu Apr 03 08:01:44 2014 -0700 +++ b/Lib/logging/__init__.py Fri Apr 04 10:57:25 2014 +0100 @@ -711,16 +711,17 @@ 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): """ diff -r d9337fe963cc -r 76689a706900 Misc/NEWS --- a/Misc/NEWS Thu Apr 03 08:01:44 2014 -0700 +++ b/Misc/NEWS Fri Apr 04 10:57:25 2014 +0100 @@ -29,6 +29,9 @@ Library ------- +- Issue #21149: Improved thread-safety in logging cleanup during interpreter + shutdown. Thanks to Devin Jeanpierre for the patch. + - Issue #21058: Fix a leak of file descriptor in :func:`tempfile.NamedTemporaryFile`, close the file descriptor if :func:`io.open` fails