From 4e9ed1a6da150df1a35229ce76f6e2869470a6d3 Mon Sep 17 00:00:00 2001 From: otorreno Date: Mon, 21 Apr 2025 11:05:46 +0000 Subject: [PATCH] fix: make logging handler close conditional to having the transport opened There was a recent release (3.12.0) that included the changes introduced in #917. The newly introduced close method seems to be called by AppEngine Python runtime at shutdown, so if you would call it explicitly before the runtime does it, then the close function throws an exception because transport is None. --- google/cloud/logging_v2/handlers/handlers.py | 7 ++++--- tests/unit/handlers/test_handlers.py | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/google/cloud/logging_v2/handlers/handlers.py b/google/cloud/logging_v2/handlers/handlers.py index 364246d5..233d9eab 100644 --- a/google/cloud/logging_v2/handlers/handlers.py +++ b/google/cloud/logging_v2/handlers/handlers.py @@ -245,9 +245,10 @@ def flush(self): def close(self): """Closes the log handler and cleans up all Transport objects used.""" - self.transport.close() - self.transport = None - self._transport_open = False + if self._transport_open: + self.transport.close() + self.transport = None + self._transport_open = False def _format_and_parse_message(record, formatter_handler): diff --git a/tests/unit/handlers/test_handlers.py b/tests/unit/handlers/test_handlers.py index 2e948493..3f25929e 100644 --- a/tests/unit/handlers/test_handlers.py +++ b/tests/unit/handlers/test_handlers.py @@ -901,6 +901,10 @@ def test_close(self): self.assertFalse(handler._transport_open) self.assertTrue(old_transport.close_called) + # second call to close shouldn't throw an exception + handler.close() + self.assertFalse(handler._transport_open) + class TestFormatAndParseMessage(unittest.TestCase): def test_none(self):