diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:29:28 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-10 10:29:28 (GMT) |
commit | 2116b12da59f77358cc539b90f58a3cdea43c2fd (patch) | |
tree | 693d26652b1dbf8b2ac058c5725f9726c3d14a0b /Lib/logging | |
parent | fcbf8f3e3d46eb023dd9a3954c6f9ed9a533d427 (diff) | |
parent | 7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd (diff) | |
download | cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.zip cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.gz cpython-2116b12da59f77358cc539b90f58a3cdea43c2fd.tar.bz2 |
Issue #23865: close() methods in multiple modules now are idempotent and more
robust at shutdown. If needs to release multiple resources, they are released
even if errors are occured.
Diffstat (limited to 'Lib/logging')
-rw-r--r-- | Lib/logging/__init__.py | 21 | ||||
-rw-r--r-- | Lib/logging/handlers.py | 23 |
2 files changed, 27 insertions, 17 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 4942147..104b0be 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1013,14 +1013,19 @@ class FileHandler(StreamHandler): """ self.acquire() try: - if self.stream: - self.flush() - if hasattr(self.stream, "close"): - self.stream.close() - self.stream = None - # Issue #19523: call unconditionally to - # prevent a handler leak when delay is set - StreamHandler.close(self) + try: + if self.stream: + try: + self.flush() + finally: + stream = self.stream + self.stream = None + if hasattr(stream, "close"): + stream.close() + finally: + # Issue #19523: call unconditionally to + # prevent a handler leak when delay is set + StreamHandler.close(self) finally: self.release() diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index d4f8aef..02a5fc1 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -627,9 +627,10 @@ class SocketHandler(logging.Handler): """ self.acquire() try: - if self.sock: - self.sock.close() + sock = self.sock + if sock: self.sock = None + sock.close() logging.Handler.close(self) finally: self.release() @@ -1213,8 +1214,10 @@ class BufferingHandler(logging.Handler): This version just flushes and chains to the parent class' close(). """ - self.flush() - logging.Handler.close(self) + try: + self.flush() + finally: + logging.Handler.close(self) class MemoryHandler(BufferingHandler): """ @@ -1268,13 +1271,15 @@ class MemoryHandler(BufferingHandler): """ Flush, set the target to None and lose the buffer. """ - self.flush() - self.acquire() try: - self.target = None - BufferingHandler.close(self) + self.flush() finally: - self.release() + self.acquire() + try: + self.target = None + BufferingHandler.close(self) + finally: + self.release() class QueueHandler(logging.Handler): |