Skip to content

Commit

Permalink
Exit gracefully when socket fails to open
Browse files Browse the repository at this point in the history
This fixes prevents a traceback that was shown when trying to listen to
an address on which 'bind' fails.
  • Loading branch information
samhed committed Jul 21, 2024
1 parent 99f83ca commit f05cd20
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions websockify/websockifyserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,20 +718,25 @@ def start_server(self):
be overridden) for each new client connection.
"""

if self.listen_fd != None:
lsock = socket.fromfd(self.listen_fd, socket.AF_INET, socket.SOCK_STREAM)
elif self.unix_listen != None:
lsock = self.socket(host=None,
unix_socket=self.unix_listen,
unix_socket_mode=self.unix_listen_mode,
unix_socket_listen=True)
else:
lsock = self.socket(self.listen_host, self.listen_port, False,
self.prefer_ipv6,
tcp_keepalive=self.tcp_keepalive,
tcp_keepcnt=self.tcp_keepcnt,
tcp_keepidle=self.tcp_keepidle,
tcp_keepintvl=self.tcp_keepintvl)
try:
if self.listen_fd != None:
lsock = socket.fromfd(self.listen_fd, socket.AF_INET, socket.SOCK_STREAM)
elif self.unix_listen != None:
lsock = self.socket(host=None,
unix_socket=self.unix_listen,
unix_socket_mode=self.unix_listen_mode,
unix_socket_listen=True)
else:
lsock = self.socket(self.listen_host, self.listen_port, False,
self.prefer_ipv6,
tcp_keepalive=self.tcp_keepalive,
tcp_keepcnt=self.tcp_keepcnt,
tcp_keepidle=self.tcp_keepidle,
tcp_keepintvl=self.tcp_keepintvl)
except OSError as e:
self.msg("Openening socket failed: %s", str(e))
self.vmsg("exception", exc_info=True)
sys.exit()

if self.daemon:
keepfd = self.get_log_fd()
Expand Down

0 comments on commit f05cd20

Please sign in to comment.