From de6e9d615ddada40fb0ee4e005777eb73a147058 Mon Sep 17 00:00:00 2001
From: Vinay Sajip <vinay_sajip@yahoo.co.uk>
Date: Mon, 23 Aug 2010 17:50:30 +0000
Subject: [PATCH] Issue #9501: Fixed logging regressions in cleanup code.

---
 Lib/logging/__init__.py | 28 ++++++++++++++++++++--------
 Misc/NEWS               |  2 ++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index dca00179ed..b4eaab6275 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -609,12 +609,16 @@ def _removeHandlerRef(wr):
     """
     Remove a handler reference from the internal cleanup list.
     """
-    _acquireLock()
-    try:
-        if wr in _handlerList:
-            _handlerList.remove(wr)
-    finally:
-        _releaseLock()
+    # 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:
+        _acquireLock()
+        try:
+            if wr in _handlerList:
+                _handlerList.remove(wr)
+        finally:
+            _releaseLock()
 
 def _addHandlerRef(handler):
     """
@@ -1604,8 +1608,16 @@ def shutdown(handlerList=_handlerList):
         #we just ignore them if raiseExceptions is not set
         try:
             h = wr()
-            h.flush()
-            h.close()
+            if h:
+                try:
+                    h.flush()
+                    h.close()
+                except (IOError, ValueError):
+                    # Ignore errors which might be caused
+                    # because handlers have been closed but
+                    # references to them are still around at
+                    # application exit.
+                    pass
         except:
             if raiseExceptions:
                 raise
diff --git a/Misc/NEWS b/Misc/NEWS
index 6e49fac5b0..75375abe39 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,8 @@ Extensions
 Library
 -------
 
+- Issue #9501: Fixed logging regressions in cleanup code.
+
 - Fix functools.total_ordering() to actually work.
 
 - Issue #9572: Importlib should not raise an exception if a directory it
-- 
2.40.0