starting TcpServer in case of Address already in use

try several times, in case this is a restart. It seems that
allow_reuse_address = True is not always enough
This commit is contained in:
l_samenv 2021-04-08 10:19:03 +02:00
parent d768d84ea1
commit dec286460d

View File

@ -25,6 +25,7 @@ import socket
import socketserver import socketserver
import sys import sys
import threading import threading
import time
from secop.datatypes import BoolType, StringType from secop.datatypes import BoolType, StringType
from secop.errors import SECoPError from secop.errors import SECoPError
@ -184,8 +185,19 @@ class TCPServer(socketserver.ThreadingTCPServer):
port = int(options.pop('uri').split('://', 1)[-1]) port = int(options.pop('uri').split('://', 1)[-1])
self.detailed_errors = options.pop('detailed_errors', False) self.detailed_errors = options.pop('detailed_errors', False)
self.allow_reuse_address = True
self.log.info("TCPServer %s binding to port %d" % (name, port)) self.log.info("TCPServer %s binding to port %d" % (name, port))
socketserver.ThreadingTCPServer.__init__( for ntry in range(5):
self, ('0.0.0.0', port), TCPRequestHandler, bind_and_activate=True) try:
socketserver.ThreadingTCPServer.__init__(
self, ('0.0.0.0', port), TCPRequestHandler, bind_and_activate=True)
break
except OSError as e:
if e.args[0] == 98: # address already in use
# this may happen despite of allow_reuse_address
time.sleep(0.3 * (1 << ntry))
else:
self.log.error('could not initialize TCP Server: %r' % e)
raise
if ntry:
self.log.warning('tried again %d times after "Address already in use"' % ntry)
self.log.info("TCPServer initiated") self.log.info("TCPServer initiated")