Skip to content

Keep subshell requests from reparenting main-shell thread output - #1609

Open
Mystery01092000 wants to merge 1 commit into
ipython:mainfrom
Mystery01092000:fix/subshell-thread-output-parent
Open

Mystery01092000 wants to merge 1 commit into
ipython:mainfrom
Mystery01092000:fix/subshell-thread-output-parent

Conversation

@Mystery01092000

Copy link
Copy Markdown

Summary

Output from a thread started by a main-shell cell was parented to whichever shell request the kernel handled most recently, including a request handled on a subshell. Any tool that uses subshells to stay responsive (variable inspectors and similar) could then take over the output of the main shell's background threads, and frontends dropped that output because they do not route that parent to the cell.

After this change, subshell requests no longer move that fallback. Threads with no parent of their own still go to the most recent main-shell request, as discussed in #1289.

Root cause

A plain threading.Thread starts with an empty contextvars context, so it never finds a parent set for its own thread and uses the shared fallback:

  • Kernel._get_shell_context_var falls back to Kernel._shell_context.
  • The shell, display publisher, displayhook and OutStreams fall back to their _parent_header_global.

Kernel.set_parent replaced _shell_context with copy_context(), and IPythonKernel.set_parent called shell.set_parent(), which sets every global parent. Both ran on every request, on whichever thread handled it, so a request handled on a subshell thread overwrote the fallback used by the main shell's threads.

Fix

When set_parent runs on a SubshellThread:

  • Kernel.set_parent still sets the _shell_parent/_shell_parent_ident context variables for that thread, but leaves _shell_context alone.
  • IPythonKernel.set_parent calls shell.set_thread_parent(parent) (an existing API that sets only the calling thread's parent on the shell, displayhook, display publisher and streams) instead of shell.set_parent(parent).

The subshell's own output still goes to its request, because each subshell has its own thread. Nothing changes on the main shell thread. One side effect: a thread started by a subshell cell now also falls back to the most recent main-shell request, not to the most recent request on any shell.

Tests

  • New tests/test_subshells.py::test_subshell_request_does_not_reparent_main_shell_thread_output. A main-shell cell starts a thread that waits on an event. A subshell request then sets the event and waits for the thread. The thread prints kernel.get_parent()["header"]["msg_id"]. The test checks that the stream message is parented to the main-shell cell and that its text is the main-shell cell's msg_id. This covers both the stream fallback and the Kernel fallback. On main the output is parented to the subshell request and the test fails. With this change it passes.
  • I ran the full test suite on Linux (python:3.12): 230 passed, 20 skipped.

Fixes #1603

This change was prepared with help from an AI assistant. I reviewed it and ran the tests locally.

Threads started by a main-shell cell have no parent of their own, so their
output falls back to the parent of the most recent shell request. That
fallback (Kernel._shell_context and the global parent of the shell, display
publisher, displayhook and output streams) was updated by set_parent on
every thread, so a request handled on a subshell redirected later output of
main-shell threads to the subshell request.

On a subshell thread, set_parent now only sets the parent for that thread
and leaves the fallback to the main shell.

Fixes ipython#1603

This branch has not been deployed

No deployments
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.

A subshell request becomes the parent of later output from threads started on the main shell

1 participant