Skip to content

Commit

Permalink
Add option to use global ZMQ context
Browse files Browse the repository at this point in the history
We have found that reverting the context in jupyter_client to its
global form (from a year ago) doesn't lead to FD leaks.  The change
away from the global context fixed an issue with multi-threaded
kernel client applications, which EG probably doesn't need.

This change adds a configurable option (use_global_zmq_context=True)
to enable use of a global (singleton) context.  By default, the
current behavior in jupter_client is used.
  • Loading branch information
kevin-bates authored and lresende committed Jun 8, 2020
1 parent 80e1a9f commit f63148e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion enterprise_gateway/services/kernels/remotemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import signal
import re
import uuid
import zmq

from tornado import gen, web
from ipython_genutils.py3compat import unicode_type
from ipython_genutils.importstring import import_item
from notebook.services.kernels.kernelmanager import MappingKernelManager
from notebook.utils import maybe_future
from jupyter_client.ioloop.manager import IOLoopKernelManager
from traitlets import directional_link, log as traitlets_log
from traitlets import directional_link, default, Bool, log as traitlets_log

from ..processproxies.processproxy import LocalProcessProxy, RemoteProcessProxy
from ..sessions.kernelsessionmanager import KernelSessionManager
Expand Down Expand Up @@ -217,6 +218,23 @@ class RemoteKernelManager(EnterpriseGatewayConfigMixin, IOLoopKernelManager):
returned - upon which methods of poll(), wait(), send_signal(), and kill() can be called.
"""

use_global_zmq_context_env = 'EG_USE_GLOBAL_ZMQ_CONTEXT'
use_global_zmq_context = Bool(False, config=True,
help="""Indicates whether a global ZMQ context should be used.
(EG_USE_GLOBAL_ZMQ_CONTEXT env var)""")

@default('use_global_zmq_context')
def use_global_zmq_context_default(self):
return bool(os.getenv(self.use_global_zmq_context_env, 'false').lower() == 'true')

# Override _context_default and create a GLOBAL ZMQ context. This prevents leaks, but could break
# down the road, thus it doesn't occur by default.
def _context_default(self):
if self.use_global_zmq_context:
self.log.info("Using global ZMQ context via configuration override (use_global_zmq_context=True).")
return zmq.Context.instance()
return super(RemoteKernelManager, self)._context_default()

def __init__(self, **kwargs):
super(RemoteKernelManager, self).__init__(**kwargs)
self.process_proxy = None
Expand Down

0 comments on commit f63148e

Please sign in to comment.