diff --git a/Lib/smtpd.py b/Lib/smtpd.py index 17b74270b2f..d7c5c938140 100755 --- a/Lib/smtpd.py +++ b/Lib/smtpd.py @@ -274,14 +274,19 @@ def __init__(self, localaddr, remoteaddr): self._localaddr = localaddr self._remoteaddr = remoteaddr asyncore.dispatcher.__init__(self) - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - # try to re-use a server port if possible - self.set_reuse_addr() - self.bind(localaddr) - self.listen(5) - print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( - self.__class__.__name__, time.ctime(time.time()), - localaddr, remoteaddr), file=DEBUGSTREAM) + try: + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + # try to re-use a server port if possible + self.set_reuse_addr() + self.bind(localaddr) + self.listen(5) + except: + self.close() + raise + else: + print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( + self.__class__.__name__, time.ctime(time.time()), + localaddr, remoteaddr), file=DEBUGSTREAM) def handle_accept(self): conn, addr = self.accept() diff --git a/Misc/NEWS b/Misc/NEWS index 5d3c14e1e57..78fe3fc1a33 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -460,6 +460,9 @@ C-API Library ------- +- Issue #6589: cleanup asyncore.socket_map in case smtpd.SMTPServer constructor + raises an exception. + - Issue #9110: Addition of ContextDecorator to contextlib, for creating APIs that act as both context managers and decorators. contextmanager changes to use ContextDecorator.