Skip to content

Commit 4ccc9ea

Browse files
authored
Bug Fix - fix CTRL-C behavior to correctly handle signals (#2369)
1 parent a885803 commit 4ccc9ea

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎docs/history/hatch.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2020

2121
- Fix environment creation crashing when a metadata hook exists that doesn’t happen to be installed in the `hatch` CLI’s environment.
2222

23+
- Fix commands run by `hatch run` no longer being interruptible with Ctrl-C, which happened because the ignored `SIGINT` of the parent process was inherited by the child process.
24+
2325

2426
## [1.17.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.17.1) - 2026-07-08 ## {: #hatch-v1.17.1 }
2527

‎src/hatch/cli/application.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ def run_shell_commands(self, context: ExecutionContext) -> None:
7575
# foreground process group; without this the parent raises KeyboardInterrupt
7676
# and aborts even though the child (e.g. a Python REPL) may choose to stay
7777
# alive. The child's exit code is still used to determine success/failure.
78+
# A handler that does nothing is used rather than `signal.SIG_IGN` because
79+
# handled signals are reset to their default action when the child is
80+
# executed whereas ignored signals stay ignored, which would otherwise make
81+
# the child itself unable to be interrupted.
7882
original_sigint = signal.getsignal(signal.SIGINT)
7983
try:
80-
signal.signal(signal.SIGINT, signal.SIG_IGN)
84+
signal.signal(signal.SIGINT, lambda *_: None)
8185
process = context.env.run_shell_command(command)
8286
finally:
8387
# Restore the original handler in finally so the parent stays

‎tests/cli/run/test_run.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import signal
23
import sys
34
import sysconfig
45

@@ -735,6 +736,43 @@ def test_scripts_no_environment(hatch, helpers, temp_dir, config_file):
735736
assert os.path.realpath(output_file.read_text().strip()).lower() == os.path.realpath(sys.executable).lower()
736737

737738

739+
def test_interrupt_signal_not_inherited(hatch, temp_dir, config_file):
740+
config_file.model.template.plugins["default"]["tests"] = False
741+
config_file.save()
742+
743+
project_name = "My.App"
744+
745+
with temp_dir.as_cwd():
746+
result = hatch("new", project_name)
747+
748+
assert result.exit_code == 0, result.output
749+
750+
project_path = temp_dir / "my-app"
751+
data_path = temp_dir / "data"
752+
data_path.mkdir()
753+
754+
project = Project(project_path)
755+
config = dict(project.raw_config)
756+
config["tool"]["hatch"]["scripts"] = {"py": "python -c {args}"}
757+
project.save_config(config)
758+
759+
original_handler = signal.getsignal(signal.SIGINT)
760+
761+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
762+
result = hatch(
763+
"run",
764+
":py",
765+
"import pathlib,signal;pathlib.Path('test.txt').write_text(str(signal.getsignal(signal.SIGINT)))",
766+
)
767+
768+
assert result.exit_code == 0, result.output
769+
770+
output_file = project_path / "test.txt"
771+
assert output_file.read_text() == str(signal.default_int_handler)
772+
773+
assert signal.getsignal(signal.SIGINT) is original_handler
774+
775+
738776
def test_error(hatch, helpers, temp_dir, config_file):
739777
config_file.model.template.plugins["default"]["tests"] = False
740778
config_file.save()

0 commit comments

Comments
 (0)