Skip to content

Support socket timeout in SysLogHandler to prevent indefinite blocking on socket creation #126400

Closed
@hodamarr

Description

@hodamarr

Feature or enhancement

Proposal:

Hi,
In the current implementation of the SysLogHandler class from the logging.handlers module, if the specified address does not respond or is unreachable, the createSocket method may block indefinitely. This is problematic because there is no way to set a socket timeout during the connection's creation without overriding the entire createSocket method.

The only way to add timeout is to override the whole createSocket method (if I use super().createSocket() it will get stuck on this line)

my suggestion:

class SysLogHandler(logging.Handler):
    def __init__(self, address=('localhost', SYSLOG_UDP_PORT), 
                 facility=LOG_USER, socktype=None, timeout=None):
        # Existing initialization
        self.timeout = timeout  # New timeout parameter
        self.createSocket()
    
    def createSocket(self):
        # Existing code ....
        try:
            sock = socket.socket(af, socktype, proto)
            **if self.timeout:
                     sock.setttimeout(self.timeout)**
            # Existing code ........

Thanks

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions