Skip to content

Commit 12e19e3

Browse files
committed
attempt with locks
1 parent 9965bbd commit 12e19e3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ipykernel/kernelbase.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import zmq
2424
from tornado import ioloop
2525
from tornado import gen
26+
from tornado.locks import Semaphore
2627
from zmq.eventloop.zmqstream import ZMQStream
2728

2829
from traitlets.config.configurable import SingletonConfigurable
@@ -38,6 +39,13 @@
3839

3940
from ._version import kernel_protocol_version
4041

42+
from collections import defaultdict
43+
44+
# shell handlers are now coroutine (for async await code),
45+
# so we may not want to consume all the message and block while it yields
46+
_msg_locks = defaultdict(Semaphore)
47+
48+
4149
class Kernel(SingletonConfigurable):
4250

4351
#---------------------------------------------------------------------------
@@ -233,7 +241,9 @@ def dispatch_shell(self, stream, msg):
233241
self.log.debug("%s: %s", msg_type, msg)
234242
self.pre_handler_hook()
235243
try:
236-
yield gen.maybe_future(handler(stream, idents, msg))
244+
lock = _msg_locks[msg_type]
245+
with (yield lock.acquire()):
246+
yield gen.maybe_future(handler(stream, idents, msg))
237247
except Exception:
238248
self.log.error("Exception in message handler:", exc_info=True)
239249
finally:
@@ -376,7 +386,6 @@ def finish_metadata(self, parent, metadata, reply_content):
376386
@gen.coroutine
377387
def execute_request(self, stream, ident, parent):
378388
"""handle an execute_request"""
379-
380389
try:
381390
content = parent[u'content']
382391
code = py3compat.cast_unicode_py2(content[u'code'])

0 commit comments

Comments
 (0)