Skip to content

Commit

Permalink
fix: there can be only one comm_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Dec 7, 2022
1 parent 4dc3033 commit 82291d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import getpass
import signal
import sys
import threading
import typing as t
from contextlib import contextmanager
from functools import partial
Expand Down Expand Up @@ -46,9 +47,19 @@ def _create_comm(*args, **kwargs):
return BaseComm(*args, **kwargs)


# there can only be one comm manager in a ipykernel process
lock = threading.Lock()
comm_manager : t.Optional[CommManager] = None


def _get_comm_manager(*args, **kwargs):
"""Create a new CommManager."""
return CommManager(*args, **kwargs)
global comm_manager
if comm_manager is None:
with lock:
if comm_manager is None:
comm_manager = CommManager(*args, **kwargs)
return comm_manager


comm.create_comm = _create_comm
Expand Down
8 changes: 7 additions & 1 deletion ipykernel/tests/test_comm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from ipykernel.comm import Comm

from .utils import kernel
from ipykernel.ipkernel import IPythonKernel

async def test_comm(kernel):
c = Comm()
c.kernel = kernel # type:ignore
c.publish_msg("foo")


def test_comm_in_manager(ipkernel: IPythonKernel):
comm = Comm()
assert comm.comm_id in ipkernel.comm_manager.comms

0 comments on commit 82291d3

Please sign in to comment.