Skip to content

Commit

Permalink
Only set the 'fork' multiprocessing start method on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
WinterSnowfall committed Oct 22, 2023
1 parent 66ff636 commit 39b883a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions wookiee_unicaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
SERVER_RELAY_BASE_PORT_DEFAULT = 23000
CLIENT_RELAY_BASE_PORT_DEFAULT = 23100

# 'spawn' will be the default starting with Python 3.14
# (since it is more thread-safe), but since we want to ensure
# compatibility with Nuitka, set it to 'fork' manually;
# change it back to 'spawn' if you plan to run the Wookiee Unicaster
# Python script directly as is on Windows (not using binaries)
MULTIPROCESS_START_METHOD = 'fork'

def sigterm_handler(signum, frame):
# exceptions may happen here as well due to logger syncronization mayhem on shutdown
try:
Expand All @@ -62,6 +55,8 @@ def sigint_handler(signum, frame):
raise SystemExit(0)

class WookieeConstants:
'''Shared static and runtime constants'''

############################ WOOKIEE MODE ############################
WOOKIEE_MODE_CLIENT = b'0'
WOOKIEE_MODE_SERVER = b'1'
Expand Down Expand Up @@ -90,6 +85,7 @@ def __init__ (self, LOGGING_LEVEL, RECEIVE_BUFFER_SIZE, CLIENT_CONNECTION_TIMEOU
######################################################################

class ServerHandler:
'''Handles inbound connections for all remote peers'''

def __init__(self, peers, intf, local_ip, source_port,
remote_peer_event_list, source_queue_list,
Expand Down Expand Up @@ -284,13 +280,14 @@ def wookiee_server_worker(self, peer, peers, isocket, remote_peer_event_list, so
logger.info(f'WU P{peer} {wookiee_name} *** Server worker stopped.')

class RemotePeerHandler:
# keep alive packet content (featuring bowcaster ASCII art guards)
KEEP_ALIVE_PACKET = b'-=|-WU-KEEP-ALIVE-|=-'
KEEP_ALIVE_HALT_PACKET = b'-=|-WU-HALT-KEEP-ALIVE-|=-'

'''Handles remote peer channel workers for full duplex communication'''

# allows processes to end gracefully when no data is sent
# or received, based on the value of a shared exit event
DEFAULT_TIMEOUT = 2 #seconds
# keep alive packet content (featuring bowcaster ASCII art guards)
KEEP_ALIVE_PACKET = b'-=|-WU-KEEP-ALIVE-|=-'
KEEP_ALIVE_HALT_PACKET = b'-=|-WU-HALT-KEEP-ALIVE-|=-'

def __init__(self, peer, wookiee_mode, intf, local_ip, source_ip, destination_ip,
source_port, destination_port, relay_port, source_queue,
Expand Down Expand Up @@ -744,7 +741,12 @@ def wookiee_relay_worker(self, peer, wookiee_mode, osocket, oaddr,
# catch SIGINT and exit gracefully
signal.signal(signal.SIGINT, sigint_handler)

multiprocessing.set_start_method(MULTIPROCESS_START_METHOD)
if platform.system() == 'Linux':
# 'spawn' will be the default for Linux starting with Python 3.14
# (since it is more thread-safe), but since we want to ensure
# compatibility with Nuitka, set it to 'fork' manually;
# 'spawn' is already the default on Windows and macOS
multiprocessing.set_start_method('fork')

configParser = ConfigParser()
no_config_file = False
Expand Down

0 comments on commit 39b883a

Please sign in to comment.