Skip to content

Commit

Permalink
use psutil to terminate properly - avoids os.kill issues on Windows s…
Browse files Browse the repository at this point in the history
…uch as missing signals and process group issues
  • Loading branch information
Laurin Schmidt committed Oct 28, 2024
1 parent 9d55a87 commit 7e56374
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions py4web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import importlib.machinery
import importlib.util
import inspect
import io
import json
import logging
import numbers
Expand All @@ -36,9 +35,9 @@
import uuid
import zipfile
from collections import OrderedDict
from contextlib import redirect_stderr, redirect_stdout

import portalocker
import psutil
from watchgod import awatch

# Optional web servers for speed
Expand Down Expand Up @@ -1710,7 +1709,15 @@ def start_server(kwargs):

# Catch interrupts like Ctrl-C if needed
def kill_all(sig, _):
os.kill(os.getpid(), signal.SIGKILL)
# better way to kill, adapted from: https://stackoverflow.com/a/70565806
main_process = psutil.Process()
children = main_process.children(recursive=True)
for child in children:
child.terminate() # friendly termination
_, still_alive = psutil.wait_procs(children, timeout=2)
for child in still_alive:
child.kill() # unfriendly termination
main_process.terminate()

signal.signal(signal.SIGINT, kill_all)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rocket3 >= 20241019.1
yatl >= 20230507.3
pydal >= 20241027.1
watchgod >= 0.6
psutil

# optional modules:
# gunicorn
Expand Down

0 comments on commit 7e56374

Please sign in to comment.