Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further cleanup on shutdown #419

Merged
merged 2 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ipykernel/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,20 @@ def run(self):
zmq.device(zmq.QUEUE, self.socket, self.socket)
except zmq.ZMQError as e:
if e.errno == errno.EINTR:
# signal interrupt, resume heartbeat
continue
elif e.errno == zmq.ETERM:
# context terminated, close socket and exit
try:
self.socket.close()
except zmq.ZMQError:
# suppress further errors during cleanup
# this shouldn't happen, though
pass
break
elif e.errno == zmq.ENOTSOCK:
# socket closed elsewhere, exit
break
else:
raise
else:
Expand Down
12 changes: 11 additions & 1 deletion ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,11 @@ def init_heartbeat(self):

def close(self):
"""Close zmq sockets in an orderly fashion"""
# un-capture IO before we start closing channels
self.reset_io()
self.log.info("Cleaning up sockets")
if self.heartbeat:
self.log.debug("Closing heartbeat channel")
self.heartbeat.socket.close()
self.heartbeat.context.term()
if self.iopub_thread:
self.log.debug("Closing iopub channel")
Expand Down Expand Up @@ -391,6 +392,15 @@ def init_io(self):

self.patch_io()

def reset_io(self):
"""restore original io

restores state after init_io
"""
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
sys.displayhook = sys.__displayhook__

def patch_io(self):
"""Patch important libraries that can't handle sys.stdout forwarding"""
try:
Expand Down