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

serhiy-storchaka <[email protected]> Mon, 10 Aug 2026 02:10:57 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/c2946542871a4a1e1a96ced2fee16b08d4755352
commit: c2946542871a4a1e1a96ced2fee16b08d4755352
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-10T09:10:43+03:00
summary:

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.

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 c78a30763d9fa08..fb6b5f3b411b227 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -902,7 +902,11 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
         self.socktype = socktype
         self.timeout = timeout
         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 90c38557d2e21b4..7cd0df3ea0b62d2 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -2211,6 +2211,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]