Skip to content

Commit

Permalink
Trap exceptions during shutdown of comm port, fix process proxy leak
Browse files Browse the repository at this point in the history
PR jupyter-server#279 introduced some fixes to address file descriptor leaks - one of
which was to shutdown the communication port.  Since the kernel launcher
listening on the other side may have already terminated, the shutdown
method could throw an exception.  This change catches, logs, then ignores
such exceptions.

While looking into other leaks (in this case memory), it was discovered
that the process proxy instance was being leaked across kernel cycles.
This change addresses that particular leak.

Note: Other PRs have also been submitted to address leaks in
`jupyter_client` and `notebook`.  These are:

[PR 360 - Fix memory leak of kernel Popen object](jupyter/jupyter_client#360)
[PR 361 - Fix memory leak of IOLoopKernelManager object](jupyter/jupyter_client#361)
[PR 3424 - Fix memory leak of iopub object in activity monitoring](jupyter/notebook#3424)
  • Loading branch information
kevin-bates committed Mar 14, 2018
1 parent 43ce2ed commit ffc613e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions enterprise_gateway/services/kernels/remotemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def cleanup(self, connection_file=True):
# which then prevents process proxy cleanup.
if self.process_proxy:
self.process_proxy.cleanup()
self.process_proxy = None
return super(RemoteKernelManager, self).cleanup(connection_file)

def get_connection_info(self, session=False):
Expand Down
8 changes: 6 additions & 2 deletions enterprise_gateway/services/processproxies/processproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from Crypto.Cipher import AES



# Default logging level of paramiko produces too much noise - raise to warning only.
logging.getLogger('paramiko').setLevel(os.getenv('EG_SSH_LOG_LEVEL', logging.WARNING))

Expand Down Expand Up @@ -701,7 +700,12 @@ def shutdown_listener(self):
"(using remote kill): {}".format(self.comm_ip, self.comm_port,
self.kernel_id, str(e)))
finally:
sock.shutdown(SHUT_WR)
try:
sock.shutdown(SHUT_WR)
except Exception as e2:
self.log.warning("Exception occurred attempting to shutdown communication socket to {}:{} "
"for KernelID '{}' (ignored): {}".format(self.comm_ip, self.comm_port,
self.kernel_id, str(e2)))
sock.close()

# Also terminate the tunnel process for the communication port - if in play. Failure to terminate
Expand Down

0 comments on commit ffc613e

Please sign in to comment.