Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Feb 25, 2025
1 parent f21dd29 commit acb0fad
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
17 changes: 9 additions & 8 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ async def shell_main(self, subshell_id: str | None):

async with create_task_group() as tg:
tg.start_soon(self.process_shell, socket)
if subshell_id is None:
# Main subshell.
await to_thread.run_sync(self.shell_stop.wait)

if not self._eventloop_set.is_set():
# Stop the async task that is waiting for the eventloop to be set.
self._eventloop_set.set()

tg.cancel_scope.cancel()

async def process_shell(self, socket=None):
# socket=None is valid if kernel subshells are not supported.
Expand Down Expand Up @@ -568,14 +577,6 @@ async def start(self, *, task_status: TaskStatus = TASK_STATUS_IGNORED) -> None:
if not self._is_test and self.shell_socket is not None:
tg.start_soon(self.shell_main, None)

await to_thread.run_sync(self.shell_stop.wait)

if not self._eventloop_set.is_set():
# Stop the async task that is waiting for the eventloop to be set.
self._eventloop_set.set()

tg.cancel_scope.cancel()

def stop(self):
self.shell_stop.set()
self.control_stop.set()
Expand Down
13 changes: 4 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
from ipykernel.kernelbase import Kernel
from ipykernel.zmqshell import ZMQInteractiveShell

# ensure we don't leak history managers
if os.name != "nt":
import tracemalloc

tracemalloc.start(20)
HistoryManager._max_inst = 1


@pytest.fixture(scope="session", autouse=True)
def _garbage_collection(request):
Expand All @@ -43,6 +36,10 @@ def _garbage_collection(request):
except ModuleNotFoundError:
tracemalloc = None

# ensure we don't leak history managers
if os.name != "nt":
HistoryManager._max_inst = 1


pytestmark = pytest.mark.anyio

Expand Down Expand Up @@ -225,8 +222,6 @@ async def ipkernel(anyio_backend):
yield kernel
finally:
kernel.destroy()
kernel.shell._atexit_once()
kernel.shell.configurables = []
ZMQInteractiveShell.clear_instance()


Expand Down
3 changes: 0 additions & 3 deletions tests/test_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from ipykernel.kernelbase import Kernel


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_comm(kernel: Kernel) -> None:
manager = CommManager(kernel=kernel)
kernel.comm_manager = manager # type:ignore
Expand Down Expand Up @@ -35,7 +34,6 @@ def on_message(msg):
assert c.target_name == "bar"


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_comm_manager(kernel: Kernel) -> None:
manager = CommManager(kernel=kernel)
msgs = []
Expand Down Expand Up @@ -101,7 +99,6 @@ def on_msg(msg):
assert comm._closed


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_comm_in_manager(ipkernel: IPythonKernel) -> None:
with pytest.deprecated_call():
comm = Comm()
Expand Down
4 changes: 0 additions & 4 deletions tests/test_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def _setup_env():

@windows_skip
@pytest.mark.skipif(sys.platform == "darwin", reason="hangs on macos")
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_tk_loop(kernel):
def do_thing():
time.sleep(1)
Expand Down Expand Up @@ -91,19 +90,16 @@ def do_thing():


@windows_skip
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_enable_gui(kernel):
enable_gui("tk", kernel)


@pytest.mark.skipif(sys.platform != "darwin", reason="MacOS-only")
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_cocoa_loop(kernel):
loop_cocoa(kernel)


@pytest.mark.parametrize("gui", qt_guis_avail)
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
def test_qt_enable_gui(gui, kernel, capsys):
if os.getenv("GITHUB_ACTIONS", None) == "true" and gui == "qt5":
pytest.skip("Qt5 and GitHub action crash CPython")
Expand Down
2 changes: 0 additions & 2 deletions tests/test_zmq_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ def test_magics(tmp_path):
socket.close()
context.destroy()
finally:
shell._atexit_once()
shell.configurables = []
InteractiveShell.clear_instance()

Expand All @@ -258,7 +257,6 @@ def test_zmq_interactive_shell(kernel):
shell.system_piped("dir")
shell.ask_exit()
finally:
shell._atexit_once()
shell.configurables = []
ZMQInteractiveShell.clear_instance()

Expand Down

0 comments on commit acb0fad

Please sign in to comment.