Corrected SO_REUSEPORT behavior on unsupported platforms

This commit is contained in:
Simonmicro 2021-12-09 18:01:23 +01:00
parent 28faacdf1d
commit 11e8b4d2fc
No known key found for this signature in database
GPG key ID: 033A4D4CE4E063D6
2 changed files with 7 additions and 6 deletions

View file

@ -4,6 +4,9 @@ import os
import socket import socket
import selectors import selectors
import ipaddress import ipaddress
import logging
from pykms_Format import pretty_printer
loggersrv = logging.getLogger('logsrv')
# https://github.com/python/cpython/blob/master/Lib/socket.py # https://github.com/python/cpython/blob/master/Lib/socket.py
def has_dualstack_ipv6(): def has_dualstack_ipv6():
@ -27,12 +30,13 @@ def create_server_sock(address, *, family = socket.AF_INET, backlog = None, reus
*family* should be either AF_INET or AF_INET6. *family* should be either AF_INET or AF_INET6.
*backlog* is the queue size passed to socket.listen(). *backlog* is the queue size passed to socket.listen().
*reuse_port* dictates whether to use the SO_REUSEPORT socket option. *reuse_port* if True and the platform supports it, we will use the SO_REUSEPORT socket option.
*dualstack_ipv6* if True and the platform supports it, it will create an AF_INET6 socket able to accept both IPv4 or IPv6 connections; *dualstack_ipv6* if True and the platform supports it, it will create an AF_INET6 socket able to accept both IPv4 or IPv6 connections;
when False it will explicitly disable this option on platforms that enable it by default (e.g. Linux). when False it will explicitly disable this option on platforms that enable it by default (e.g. Linux).
""" """
if reuse_port and not hasattr(socket._socket, "SO_REUSEPORT"): if reuse_port and not hasattr(socket._socket, "SO_REUSEPORT"):
raise ValueError("SO_REUSEPORT not supported on this platform") pretty_printer(log_obj = loggersrv.warning, put_text = "{reverse}{yellow}{bold}SO_REUSEPORT not supported on this platform - ignoring socket option.{end}")
reuse_port = False
if dualstack_ipv6: if dualstack_ipv6:
if not has_dualstack_ipv6(): if not has_dualstack_ipv6():

View file

@ -13,7 +13,6 @@ import pickle
import socketserver import socketserver
import queue as Queue import queue as Queue
import selectors import selectors
from getpass import getuser
from tempfile import gettempdir from tempfile import gettempdir
from time import monotonic as time from time import monotonic as time
@ -495,14 +494,12 @@ def server_create():
all_address = [( all_address = [(
srv_config['ip'], srv_config['port'], srv_config['ip'], srv_config['port'],
(srv_config['backlog_main'] if 'backlog_main' in srv_config else srv_options['backlog']['def']), (srv_config['backlog_main'] if 'backlog_main' in srv_config else srv_options['backlog']['def']),
(srv_config['reuse_main'] if 'reuse_main' in srv_config else False if getuser() == 'WDAGUtilityAccount' \ (srv_config['reuse_main'] if 'reuse_main' in srv_config else srv_options['reuse']['def'])
else srv_options['reuse']['def'])
)] )]
log_address = "TCP server listening at %s on port %d" %(srv_config['ip'], srv_config['port']) log_address = "TCP server listening at %s on port %d" %(srv_config['ip'], srv_config['port'])
if 'listen' in srv_config: if 'listen' in srv_config:
for l, b, r in zip(srv_config['listen'], srv_config['backlog'], srv_config['reuse']): for l, b, r in zip(srv_config['listen'], srv_config['backlog'], srv_config['reuse']):
r = (False if getuser() == 'WDAGUtilityAccount' else r)
all_address.append(l + (b,) + (r,)) all_address.append(l + (b,) + (r,))
log_address += justify("at %s on port %d" %(l[0], l[1]), indent = 56) log_address += justify("at %s on port %d" %(l[0], l[1]), indent = 56)