From 8dce90408dbd37139bd11b73cd25cb99fb8be019 Mon Sep 17 00:00:00 2001 From: Griatch Date: Sat, 27 Jan 2018 10:57:33 +0100 Subject: [PATCH] Remove Unix-centric Zombie-process removal that eats Windows --- evennia/server/evennia_launcher.py | 31 +++++++++++++++++++---------- evennia/server/portal/amp_server.py | 17 +++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/evennia/server/evennia_launcher.py b/evennia/server/evennia_launcher.py index 48c2bd3839..f3e6f1c28d 100644 --- a/evennia/server/evennia_launcher.py +++ b/evennia/server/evennia_launcher.py @@ -481,6 +481,11 @@ Others, like migrate, test and shell is passed on to Django.""" # # ------------------------------------------------------------ + +def _is_windows(): + return os.name == 'nt' + + def _print_info(portal_info_dict, server_info_dict): """ Format info dicts from the Portal/Server for display @@ -811,7 +816,7 @@ def start_evennia(pprofiler=False, sprofiler=False): def _portal_not_running(fail): print("Portal starting {}...".format("(under cProfile)" if pprofiler else "")) try: - if os.name == 'nt': + if _is_windows(): # Windows requires special care create_no_window = 0x08000000 Popen(portal_cmd, env=getenv(), bufsize=-1, @@ -1349,7 +1354,7 @@ def getenv(): env (dict): Environment global dict. """ - sep = ";" if os.name == 'nt' else ":" + sep = ";" if _is_windows() else ":" env = os.environ.copy() env['PYTHONPATH'] = sep.join(sys.path) return env @@ -1409,7 +1414,7 @@ def kill(pidfile, component='Server', callback=None, errback=None, killsignal=SI Ignored on Windows. """ - if os.name == 'nt': + if _is_windows(): # Windows signal sending is very limited. from win32api import GenerateConsoleCtrlEvent, SetConsoleCtrlHandler try: @@ -1427,7 +1432,7 @@ def kill(pidfile, component='Server', callback=None, errback=None, killsignal=SI # Linux/Unix/Mac can send kill signal directly to specific PIDs. pid = get_pid(pidfile) if pid: - if os.name == 'nt': + if _is_windows(): os.remove(pidfile) try: os.kill(int(pid), killsignal) @@ -1607,7 +1612,7 @@ def init_game_directory(path, check_db=True): print(ERROR_LOGDIR_MISSING.format(logfiles=errstr)) sys.exit() - if os.name == 'nt': + if _is_windows(): # We need to handle Windows twisted separately. We create a # batchfile in game/server, linking to the actual binary @@ -1754,12 +1759,12 @@ def run_menu(): elif inp == 6: stop_server_only() elif inp == 7: - if os.name == 'nt': + if _is_windows(): print("Windows can't send kill signals by PID. Use option 8 instead.") else: kill(SERVER_PIDFILE, 'Server') elif inp == 8: - if os.name == 'nt': + if _is_windows(): kill(None) else: kill(SERVER_PIDFILE, 'Server') @@ -1937,10 +1942,16 @@ def main(): elif option == 'sstop': stop_server_only() elif option == 'kill': - kill(SERVER_PIDFILE, 'Server') - kill(PORTAL_PIDFILE, 'Portal') + if _is_windows(): + kill(None) + else: + kill(SERVER_PIDFILE, 'Server') + kill(PORTAL_PIDFILE, 'Portal') elif option == 'skill': - kill(SERVER_PIDFILE, 'Server') + if _is_windows(): + print("This is not supported on Windows. Use 'evennia kill' instead.") + else: + kill(SERVER_PIDFILE, 'Server') elif option != "noop": # pass-through to django manager check_db = False diff --git a/evennia/server/portal/amp_server.py b/evennia/server/portal/amp_server.py index 9baa7ba86f..c550a648c3 100644 --- a/evennia/server/portal/amp_server.py +++ b/evennia/server/portal/amp_server.py @@ -9,10 +9,14 @@ import sys from twisted.internet import protocol from evennia.server.portal import amp from django.conf import settings -from subprocess import Popen, STDOUT, PIPE +from subprocess import Popen, STDOUT from evennia.utils import logger +def _is_windows(): + return os.name == 'nt' + + def getenv(): """ Get current environment and add PYTHONPATH. @@ -21,7 +25,7 @@ def getenv(): env (dict): Environment global dict. """ - sep = ";" if os.name == 'nt' else ":" + sep = ";" if _is_windows() else ":" env = os.environ.copy() env['PYTHONPATH'] = sep.join(sys.path) return env @@ -156,7 +160,7 @@ class AMPServerProtocol(amp.AMPMultiConnectionProtocol): # eventual errors happening before the Server has # opened its logger. try: - if os.name == 'nt': + if _is_windows(): # Windows requires special care create_no_window = 0x08000000 process = Popen(server_twistd_cmd, env=getenv(), bufsize=-1, @@ -171,11 +175,10 @@ class AMPServerProtocol(amp.AMPMultiConnectionProtocol): self.factory.portal.server_twistd_cmd = server_twistd_cmd logfile.flush() - if process: - # avoid zombie-process + if process and not _is_windows(): + # avoid zombie-process on Unix/BSD process.wait() - return process.pid - return 0 + return def wait_for_disconnect(self, callback, *args, **kwargs): """