Skip to content

Conversation

ptosco
Copy link

@ptosco ptosco commented Sep 20, 2025

Currently, a long-running C Python module executed on Windows in a Jupyter kernel can only be interrupted by sending twice the "Interrupt kernel" signal.
This happens because the first interrupt signal is intercepted by ipykernel/parentpoller.py and as it is set to auto-reset, it will not reach a C Python module listening for the same signal. At the time, the interrupt_main() call scheduled by ipykernel/parentpoller.py will not be executed as the main Python thread is blocked by the long-running C Python module.
Only when the "Interrupt kernel" is issued again the C Python module will be interrupted.

This PR sets the CreateEvent bManualReset Boolean flag to true, thus requiring the interrupt event to be manually reset by the receiver. This makes it possible for a listening C Python module to be reached by the signal and hence be interrupted, without requiring the interrupt signal to be issued twice.

This can be conveniently tested through the attached waitloop test Python module, which can be installed on all platforms supported by Jupyter by unzipping waitloop_module.zip and then running pip install . in the waitloop_module directory:

Python interpreter - all platforms

>>> import waitloop
>>> waitloop.run()

[Press CTRL+C on your keyboard to interrupt the loop]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt

Jupyter notebook - Windows platform

[1]: import waitloop

[2]: waitloop.run()

[Issue Kernel->Interrupt kernel once - waitloop keeps running]
[Issue Kernel->Interrupt kernel again - waitloop stops running]

Jupyter notebook - non-Windows platforms

[1]: import waitloop

[2]: waitloop.run()

[Issue Kernel->Interrupt kernel once - waitloop stops running]

WIth the current Jupyter version, on Windows it is necessary to issue "Interrupt kernel" twice to interrupt waitloop, whereas on non-Windows platforms it is sufficient to issue the command once,

After merging this PR and the sister PR #1434 in the ipykernel repo, a single "Interrupt kernel" will stop waitloop also on Windows, as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant