Make bash subprocess interruptible

Prevent inheriting SIG_IGN

Closes gh-1
main
Thomas Kluyver 11 years ago
parent af97cb0146
commit d7f79edb8c

@ -1,5 +1,7 @@
from IPython.kernel.zmq.kernelbase import Kernel from IPython.kernel.zmq.kernelbase import Kernel
from pexpect import replwrap from pexpect import replwrap
import signal
from subprocess import check_output from subprocess import check_output
import re import re
@ -25,14 +27,22 @@ class BashKernel(Kernel):
def __init__(self, **kwargs): def __init__(self, **kwargs):
Kernel.__init__(self, **kwargs) Kernel.__init__(self, **kwargs)
self.bashwrapper = replwrap.bash() # Signal handlers are inherited by forked processes, and we can't easily
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
# message handlers, we need to temporarily reset the SIGINT handler here
# so that bash and its children are interruptible.
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
self.bashwrapper = replwrap.bash()
finally:
signal.signal(signal.SIGINT, sig)
def do_execute(self, code, silent, store_history=True, user_expressions=None, def do_execute(self, code, silent, store_history=True, user_expressions=None,
allow_stdin=False): allow_stdin=False):
if not code.strip(): if not code.strip():
return {'status': 'ok', 'execution_count': self.execution_count, return {'status': 'ok', 'execution_count': self.execution_count,
'payloads': [], 'user_expressions': {}} 'payloads': [], 'user_expressions': {}}
interrupted = False interrupted = False
try: try:
output = self.bashwrapper.run_command(code.rstrip(), timeout=None) output = self.bashwrapper.run_command(code.rstrip(), timeout=None)

Loading…
Cancel
Save