Fix: Bind browser close output to creation context#14077
Conversation
|
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?) |
|
Good question. I manually traced the chain further with the help of Codex to dig up relevant past PRs. Background Our fix Some more digging More generally, I was debating whether this should be a MNE-Python fix or a One nice thing about this fix using Broader adoption? If @larsoner you agree with my investigations here, then I think we may want to mirror this use of |
I think we can tackle those when we hit them... the raw browser is the only one that immediately comes to mind as printing some "this is what the result was" message at the end, so it's at least the biggest case I think.
Okay can you add a code comment linking to the specific comment that recommends this? It'll help us follow the trail on this issue next time someone is modifying that code or if we otherwise have problems with it |
Add comments to clarify the rationale for updating the _close method to use Context.run()
|
Sounds good. I linked to the PEP 567 asyncio section that describes this pair of |
Reference issue (if any)
None
What does this implement/fix?
In Jupyter notebook, closing a second or more
Raw.plot()Qt browser appends itsChannels 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 currentsys.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:
3 passed).