Skip to content

Commit 2fa22e2

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-82535: Ignore address resolution failure in SysLogHandler constructor (GH-154504) (GH-155457)
The address may be temporarily unresolvable when the handler is created. It is resolved again when a record is emitted. (cherry picked from commit c294654) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 6c577a5 commit 2fa22e2

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎Lib/logging/handlers.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,11 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
878878
self.socktype = socktype
879879
self.timeout = timeout
880880
self.socket = None
881-
self.createSocket()
881+
# The address is resolved again when emitting an event.
882+
try:
883+
self.createSocket()
884+
except socket.gaierror:
885+
pass
882886

883887
def _connect_unixsocket(self, address):
884888
use_socktype = self.socktype

‎Lib/test/test_logging.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,24 @@ def tearDown(self):
21722172
self.server_class.address_family = socket.AF_INET
21732173
super(IPv6SysLogHandlerTest, self).tearDown()
21742174

2175+
@support.requires_working_socket()
2176+
class UnresolvableSysLogAddressTest(BaseTest):
2177+
2178+
"""Test for SysLogHandler with a temporarily unresolvable address."""
2179+
2180+
@patch('socket.getaddrinfo')
2181+
def test_unresolvable_address(self, mock_getaddrinfo):
2182+
# The address can be unresolvable when the handler is created.
2183+
mock_getaddrinfo.side_effect = socket.gaierror
2184+
hdlr = logging.handlers.SysLogHandler(('localhost', 514))
2185+
self.addCleanup(hdlr.close)
2186+
self.assertIsNone(hdlr.socket)
2187+
# It is resolved again when a record is emitted.
2188+
calls = mock_getaddrinfo.call_count
2189+
with support.captured_stderr():
2190+
hdlr.emit(logging.makeLogRecord({'msg': 'sp\xe4m'}))
2191+
self.assertGreater(mock_getaddrinfo.call_count, calls)
2192+
21752193
@support.requires_working_socket()
21762194
@threading_helper.requires_working_threading()
21772195
class HTTPHandlerTest(BaseTest):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`logging.handlers.SysLogHandler` no longer fails
2+
if the address cannot be resolved when the handler is created.
3+
The address is resolved again when a record is emitted.

0 commit comments

Comments
 (0)