Skip to content

Commit 76bcda3

Browse files
fix: kill myst processes as a group
1 parent 26b7b8b commit 76bcda3

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

  • src/afterpython/cli/commands

src/afterpython/cli/commands/dev.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
if TYPE_CHECKING:
66
from afterpython._typing import NodeEnv
77

8+
import contextlib
9+
import os
10+
import signal
811
import subprocess
912
import time
1013

@@ -116,15 +119,26 @@ def dev(
116119
paths = ctx.obj["paths"]
117120

118121
def cleanup_processes():
119-
"""Clean up all MyST server processes"""
122+
"""Clean up all MyST server processes.
123+
124+
Each spawned 'ap {content_type}' is its own session leader (start_new_session=True
125+
below), so its grandchild `myst start` shares the same process group. proc.terminate()
126+
would only SIGTERM the outer Python wrapper — which is blocked in subprocess.run and
127+
doesn't forward the signal — leaving myst orphaned. Signal the whole group instead.
128+
"""
120129
click.echo("\nShutting down MyST servers...")
121130
for proc in myst_processes:
122131
try:
123-
proc.terminate()
132+
pgid = os.getpgid(proc.pid)
133+
except ProcessLookupError:
134+
continue
135+
try:
136+
os.killpg(pgid, signal.SIGTERM)
124137
proc.wait(timeout=5)
125138
except subprocess.TimeoutExpired:
126-
proc.kill()
127-
except Exception:
139+
with contextlib.suppress(ProcessLookupError):
140+
os.killpg(pgid, signal.SIGKILL)
141+
except ProcessLookupError:
128142
pass
129143

130144
# Determine which content types to run
@@ -184,6 +198,9 @@ def cleanup_processes():
184198
*(["--execute"] if execute else []),
185199
*ctx.args,
186200
],
201+
# New session so cleanup_processes can SIGTERM the whole group and
202+
# take the grandchild `myst start` down with the wrapper.
203+
start_new_session=True,
187204
# stdout=subprocess.DEVNULL, # Suppress output (optional)
188205
# stderr=subprocess.DEVNULL, # Suppress errors (optional)
189206
)

0 commit comments

Comments
 (0)