Skip to content

Commit 40a92dc

Browse files
committed
Treat interruption in the script magic (blocking case)
This change prevents leaving untrucked subprocesses alive.
1 parent 012d228 commit 40a92dc

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

IPython/core/magics/script.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import os
1616
import re
1717
import sys
18+
import signal
19+
import time
1820
from subprocess import Popen, PIPE
1921

2022
# Our own packages
@@ -203,7 +205,22 @@ def shebang(self, line, cell):
203205
self.shell.user_ns[args.proc] = p
204206
return
205207

206-
out, err = p.communicate(cell)
208+
try:
209+
out, err = p.communicate(cell)
210+
except KeyboardInterrupt:
211+
p.send_signal(signal.SIGINT)
212+
time.sleep(0.1)
213+
if p.poll() is not None:
214+
print "Process is interrupted."
215+
return
216+
p.terminate()
217+
time.sleep(0.1)
218+
if p.poll() is not None:
219+
print "Process is terminated."
220+
return
221+
p.kill()
222+
print "Process is killed."
223+
return
207224
out = py3compat.bytes_to_str(out)
208225
err = py3compat.bytes_to_str(err)
209226
if args.out:

0 commit comments

Comments
 (0)