Skip to content

Commit 41e6430

Browse files
committed
Forward SIGINT to current pipe process instead of terminating it. This is consistent
with our do_shell() command, ppaged() command, and other shells.
1 parent b39ca95 commit 41e6430

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,8 @@ def sigint_handler(self, signum: int, frame) -> None:
16591659
:param frame
16601660
"""
16611661
if self.cur_pipe_proc_reader is not None:
1662-
# Terminate the current pipe process
1663-
self.cur_pipe_proc_reader.terminate()
1662+
# Pass the SIGINT to the current pipe process
1663+
self.cur_pipe_proc_reader.send_sigint()
16641664

16651665
# Check if we are allowed to re-raise the KeyboardInterrupt
16661666
if not self.sigint_protection:
@@ -1933,8 +1933,8 @@ def _redirect_output(self, statement: Statement) -> Tuple[bool, RedirectionSaved
19331933
# We want Popen to raise an exception if it fails to open the process. Thus we don't set shell to True.
19341934
try:
19351935
# Set options to not forward signals to the pipe process. If a Ctrl-C event occurs,
1936-
# our sigint handler terminate the most recent pipe process. This makes sure
1937-
# pipe processes close in the right order (most recent first).
1936+
# our sigint handler will forward it only to the most recent pipe process. This makes
1937+
# sure pipe processes close in the right order (most recent first).
19381938
if sys.platform == 'win32':
19391939
creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
19401940
start_new_session = False

cmd2/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ def __init__(self, proc: subprocess.Popen, stdout: Union[StdSim, BinaryIO, TextI
404404
if self._proc.stderr is not None:
405405
self._err_thread.start()
406406

407+
def send_sigint(self) -> None:
408+
"""Send a SIGINT to the process"""
409+
import signal
410+
self._proc.send_signal(signal.SIGINT)
411+
407412
def terminate(self) -> None:
408413
"""Terminate the process"""
409414
self._proc.terminate()

0 commit comments

Comments
 (0)