Skip to content

Fix Ctrl+C handling on Windows subprocesses #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/juv/_run_managed.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def run(
encoding="utf-8",
) as f:
script_path = Path(f.name)
atexit.register(lambda: script_path.unlink(missing_ok=True))

lockfile = Path(f"{f.name}.lock")
f.write(script)
f.flush()
Expand Down Expand Up @@ -161,8 +163,5 @@ def run(
lockfile.unlink(missing_ok=True)
output_queue.put(None)
output_thread.join()

# ensure the process is fully cleaned up before deleting script
process.wait()
# unlink after process has exited
atexit.register(lambda: script_path.unlink(missing_ok=True))
# ensure the process is fully cleaned up before deleting script
process.wait()
23 changes: 13 additions & 10 deletions src/juv/_run_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def run(script: str, args: list[str], lockfile_contents: str | None, dir: Path)
encoding="utf-8",
) as f:
script_path = Path(f.name)
atexit.register(lambda: script_path.unlink(missing_ok=True))

lockfile = Path(f"{f.name}.lock")
f.write(script)
f.flush()
Expand All @@ -36,33 +38,34 @@ def run(script: str, args: list[str], lockfile_contents: str | None, dir: Path)
# We rewrite the lockfile entry (if necessary) within that process.
env["JUV_LOCKFILE_PATH"] = str(lockfile)

if not IS_WINDOWS:
if IS_WINDOWS:
process = subprocess.Popen( # noqa: S603
[os.fsdecode(find_uv_bin()), *args, f.name],
stdout=sys.stdout,
stderr=sys.stderr,
preexec_fn=os.setsid, # noqa: PLW1509
# Required so the subprocess is attached to our console;
# needed for CTRL_BREAK_EVENT to propagate to the process group
stdin=sys.stdin,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
env=env,
)
else:
process = subprocess.Popen( # noqa: S603
[os.fsdecode(find_uv_bin()), *args, f.name],
stdout=sys.stdout,
stderr=sys.stderr,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
preexec_fn=os.setsid, # noqa: PLW1509
env=env,
)

try:
process.wait()
except KeyboardInterrupt:
if not IS_WINDOWS:
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
else:
if IS_WINDOWS:
os.kill(process.pid, signal.SIGTERM)
else:
# Send CTRL_BREAK_EVENT to the process group on Windows
process.send_signal(signal.CTRL_BREAK_EVENT)
finally:
lockfile.unlink(missing_ok=True)

# ensure the process is fully cleaned up before deleting script
process.wait()
atexit.register(lambda: script_path.unlink(missing_ok=True))
process.wait()