Skip to content

Commit 4a47b36

Browse files
Set the default SIGPIPE handler before starting bash. (#135)
1 parent 7885eda commit 4a47b36

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

bash_kernel/kernel.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ def _start_bash(self):
109109
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
110110
# message handlers, we need to temporarily reset the SIGINT handler here
111111
# so that bash and its children are interruptible.
112-
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
112+
old_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_DFL)
113+
# We need to temporarily reset the default signal handler for SIGPIPE so
114+
# that commands like `head` used in a pipe chain can signal to the data
115+
# producers.
116+
old_sigpipe_handler = signal.signal(signal.SIGPIPE, signal.SIG_DFL)
113117
try:
114118
# Note: the next few lines mirror functionality in the
115119
# bash() function of pexpect/replwrap.py. Look at the
@@ -131,7 +135,8 @@ def _start_bash(self):
131135
extra_init_cmd="export PAGER=cat",
132136
line_output_callback=self.process_output)
133137
finally:
134-
signal.signal(signal.SIGINT, sig)
138+
signal.signal(signal.SIGINT, old_sigint_handler)
139+
signal.signal(signal.SIGPIPE, old_sigpipe_handler)
135140

136141
# Disable bracketed paste (see <https://github.com/takluyver/bash_kernel/issues/117>)
137142
self.bashwrapper.run_command("bind 'set enable-bracketed-paste off' >/dev/null 2>&1 || true")

0 commit comments

Comments
 (0)