https://github.com/python/cpython/commit/f61d36036ce8ac7dcb26bc1a94f1a43346056b07
commit: f61d36036ce8ac7dcb26bc1a94f1a43346056b07
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: Yhg1s <[email protected]>
date: 2026-09-29T23:15:26+02:00
summary:

[3.13] gh-82535: Ignore address resolution failure in SysLogHandler constructor 
(GH-154504) (#155459)

gh-82535: Ignore address resolution failure in SysLogHandler constructor 
(GH-154504)

The address may be temporarily unresolvable when the handler is created.
It is resolved again when a record is emitted.
(cherry picked from commit c2946542871a4a1e1a96ced2fee16b08d4755352)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-22-19-21-20.gh-issue-82535.METFqd.rst
M Lib/logging/handlers.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 130318063837024..ae977efd9ed9a06 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -882,7 +882,11 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
         self.facility = facility
         self.socktype = socktype
         self.socket = None
-        self.createSocket()
+        # The address is resolved again when emitting an event.
+        try:
+            self.createSocket()
+        except socket.gaierror:
+            pass
 
     def _connect_unixsocket(self, address):
         use_socktype = self.socktype
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 70dd3d7867ab963..7cda54796a503f0 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -2147,6 +2147,24 @@ def tearDown(self):
         self.server_class.address_family = socket.AF_INET
         super(IPv6SysLogHandlerTest, self).tearDown()
 
[email protected]_working_socket()
+class UnresolvableSysLogAddressTest(BaseTest):
+
+    """Test for SysLogHandler with a temporarily unresolvable address."""
+
+    @patch('socket.getaddrinfo')
+    def test_unresolvable_address(self, mock_getaddrinfo):
+        # The address can be unresolvable when the handler is created.
+        mock_getaddrinfo.side_effect = socket.gaierror
+        hdlr = logging.handlers.SysLogHandler(('localhost', 514))
+        self.addCleanup(hdlr.close)
+        self.assertIsNone(hdlr.socket)
+        # It is resolved again when a record is emitted.
+        calls = mock_getaddrinfo.call_count
+        with support.captured_stderr():
+            hdlr.emit(logging.makeLogRecord({'msg': 'sp\xe4m'}))
+        self.assertGreater(mock_getaddrinfo.call_count, calls)
+
 @support.requires_working_socket()
 @threading_helper.requires_working_threading()
 class HTTPHandlerTest(BaseTest):
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-22-19-21-20.gh-issue-82535.METFqd.rst 
b/Misc/NEWS.d/next/Library/2026-07-22-19-21-20.gh-issue-82535.METFqd.rst
new file mode 100644
index 000000000000000..e027f81256b703e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-22-19-21-20.gh-issue-82535.METFqd.rst
@@ -0,0 +1,3 @@
+:class:`logging.handlers.SysLogHandler` no longer fails
+if the address cannot be resolved when the handler is created.
+The address is resolved again when a record is emitted.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to