Skip to content

Use thread lock to support comms via subshells #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ianthomas23
Copy link
Member

The upcoming ipykernel 7.0.0 release will support subshells which are separate threads of execution within a kernel process. JupyterLab has already been updated to deal with subshells on kernels that support them and this includes options for running comms over subshells (jupyterlab/jupyterlab#17363). This PR contains changes that are needed in ipympl to support subshells, specifically use of a threading.Lock in the python code that accesses global state such at Matplotlib's Gcf and ipython.display. Without this we see problems such as plots not being displayed as one thread can be, for example, modifying a collection in Gcf whilst another is reading it. Strictly speaking we only need to lock when one thread is writing to a global as we can tolerate multiple non-modifying reads at the same time, but I have preferred to keep the changes as simple and understandable as possible but putting the locks at a high level in each of the functions that needs it.

Screenshot 2025-08-13 at 11 29 40

Above is a screenshot reproducing the problem using latest commit 5e068ed, showing that the third plot is not displayed. To reproduce this use the latest jupyterlab and precisely ipykernel==7.0.0a2, and in Lab's Settings Editor select "Kernel comms over subshells" to be "One subshell per comm-target". It does not occur every time, but this is the setting that I have found most likely to produce the problem. After this PR, all plots display correctly.

Copy link
Contributor

Binder 👈 Launch a binder notebook on branch ianthomas23/ipympl/lock-for-subshells-2

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

Successfully merging this pull request may close these issues.

1 participant