Skip to content

Fix: Bind browser close output to creation context#14077

Open
mh105 wants to merge 1 commit into
mne-tools:mainfrom
mh105:agent/upstream-browser-close-context
Open

Fix: Bind browser close output to creation context#14077
mh105 wants to merge 1 commit into
mne-tools:mainfrom
mh105:agent/upstream-browser-close-context

Conversation

@mh105

@mh105 mh105 commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Reference issue (if any)

None

What does this implement/fix?

In Jupyter notebook, closing a second or more Raw.plot() Qt browser appends its Channels marked as bad: report to the output of the first browser cell rather than the cell that created that browser. This behavior is a bit counterintuitive, since I expect to see the "Channels marked as bad:" message to appear in the same cell I invoke the function.

I asked Codex to look into the issue, and it found that IPykernel associates output with a ContextVar. A persistent Qt event-loop task will retain the context of the cell that initialized it, so a later close callback runs with an older output parent even though MNE’s logger writes to the current sys.stdout.

Codex proposed a minimal change by capturing contextvars.copy_context() independently when each MNE browser is created, then runs the existing shared close implementation in that captured context. The change looks good to me.

Additional information

Codex-generated tests passed:

  • Focused browser-context unit tests passed (3 passed).
  • Matplotlib browser selection-child close smoke test passed.
  • A real IPykernel 7.3.0 plus Qt two-browser probe reproduced the stale parent before the fix and routed each close report to its own cell after the fix.

@mh105
mh105 requested a review from drammock as a code owner July 19, 2026 18:35
@mh105 mh105 changed the title Bind browser close output to creation context Fix: Bind browser close output to creation context Jul 19, 2026
@larsoner

Copy link
Copy Markdown
Member

I think we'll need a bit more info in the code for the next person to follow. Can you link to some ipython / jupyter docs that talk about this as a potential solution? A quick search turned up ipython/ipython#11565 but maybe there is something better.

Also, is this something we should special-case to the matplotlib inline backend? (Or maybe we don't need to because it's harmless on any others?)

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.

2 participants