Skip to content

Commit 6e67bfd

Browse files
committed
Merge pull request #97 from minrk/shutdown
prevent IOThread from zombifying kernel
2 parents 60f5811 + 26f8bad commit 6e67bfd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

ipykernel/iostream.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Distributed under the terms of the Modified BSD License.
66

77
from __future__ import print_function
8+
import atexit
89
import os
910
import threading
1011
import sys
@@ -116,9 +117,14 @@ def _check_mp_mode(self):
116117
def start(self):
117118
"""Start the IOPub thread"""
118119
self.thread.start()
120+
# make sure we don't prevent process exit
121+
# I'm not sure why setting daemon=True above isn't enough, but it doesn't appear to be.
122+
atexit.register(self.stop)
119123

120124
def stop(self):
121125
"""Stop the IOPub thread"""
126+
if not self.thread.is_alive():
127+
return
122128
self.io_loop.add_callback(self.io_loop.stop)
123129
self.thread.join()
124130

ipykernel/tests/test_kernel.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io
88
import os.path
99
import sys
10+
import time
1011

1112
import nose.tools as nt
1213

@@ -242,3 +243,19 @@ def test_complete():
242243
nt.assert_greater(len(matches), 0)
243244
for match in matches:
244245
nt.assert_equal(match[:2], 'a.')
246+
247+
248+
def test_shutdown():
249+
"""Kernel exits after polite shutdown_request"""
250+
with new_kernel() as kc:
251+
km = kc.parent
252+
execute(u'a = 1', kc=kc)
253+
wait_for_idle(kc)
254+
kc.shutdown()
255+
for i in range(100): # 10s timeout
256+
if km.is_alive():
257+
time.sleep(.1)
258+
else:
259+
break
260+
nt.assert_false(km.is_alive())
261+

0 commit comments

Comments
 (0)