diff -r 03df2c1c6892 Lib/logging/__init__.py --- a/Lib/logging/__init__.py Thu Apr 03 10:31:25 2014 -0400 +++ b/Lib/logging/__init__.py Thu Apr 03 13:16:04 2014 -0700 @@ -622,16 +622,20 @@ 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. + acquireLock = _acquireLock + releaseLock = _releaseLock + handlerList = _handlerList + if (acquireLock is not None and handlerList is not None and + releaseLock is not None): + acquireLock() try: - if wr in _handlerList: - _handlerList.remove(wr) + if wr in handlerList: + handlerList.remove(wr) finally: - _releaseLock() + releaseLock() def _addHandlerRef(handler): """