ISSUE DESCRIPTION Marimo's print routing breaks Numba compilation In notebooks that do…
- User
ISSUE DESCRIPTION
Marimo's print routing breaks Numba compilation
In notebooks that do not use
mo.Thread,printmust retain builtin identity so Numba can compile functions that call it.mo.Threadmust continue routing output to the spawning cell, with output isolation between concurrent threads.Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /marimo, current file: none) bash-$
- Assistant
I’ll first locate the print-routing and thread implementations, then inspect related tests and reproduce the identity/Numba failure before changing the smallest relevant surface.
- Assistant
rgis unavailable, so I’ll use repository-widegrepto identify the implementation and focused tests. - Assistant
The likely mechanism is a global proxy for
sys.stdoutrather than a directprintreplacement. I’ll inspect thread startup, stream proxy installation, runtime lifecycle, and print/thread tests to understand when builtin identity is altered. - Assistant
The stream proxy alone does not change
builtins.print, so I’ll search all assignments/import hooks involvingprint, then inspect complete thread tests and runtime context setup where cell builtins may be patched. - Assistant
marimo._messaging.print_overrideis the direct identity-changing mechanism. I’ll inspect it and every installation call to determine how to make routing opt-in only while anmo.Threadis active, preserving concurrent isolation. - Assistant
The kernel currently injects
print_overrideinto every notebook’s main-module globals at creation, which explains Numba rejecting even notebooks that never createmo.Thread. A safe direction is lazy installation when anmo.Threadis constructed: the override remains process/thread-aware viaTHREADS, while untouched notebooks retain the exact builtin. I’ll inspect module creation and context/global references to choose a robust lazy installation point rather than relying only on a target function shape. - Assistant
KernelRuntimeContext.globalsgivesmo.Threada reliable kernel namespace. I’ll confirm kernel-session construction and fixture behavior, then reproduce with a tiny kernel execution using Numba (if installed), checking both compilation andprint is builtins.print. - Assistant
The focused fixture confirms all test kernels currently receive the override eagerly. I’ll preserve the internal
KernelArgsfield for compatibility but stop eager insertion in kernel creation, and lazilysetdefault("print", print_override)inThread.__init__for kernel contexts. This also respects a user-definedprint. Before editing, I’ll inspect patch tests and static dependency constraints (especially the existing print_override↔threads import cycle) so the lazy import does not create a cycle. - Assistant
There are no tests depending on eager notebook injection, and the cycle is safe if
print_overrideis imported insideThread.__init__after module initialization. I’ll implement the lazy transition and add a short rationale in code. I’ll also make thread bookkeeping cleanup exception-safe because stale IDs could misroute output if a thread target raises and the OS later reuses its ID—directly relevant to output isolation. - Assistant
The core change is in place. I’ll now run the focused thread and print-override suites; these validate spawning-cell routing and existing formatting/fallback behavior. Then I’ll use an ad hoc script (outside the repository tests) to verify kernel-global builtin identity before thread use and lazy override afterward.
- Assistant
All 35 focused tests pass. For the explicit regression check, I’ll instantiate the same test kernel context directly and run cells that record
print is builtins.print, then construct anmo.Threadand confirm routing is installed only at that point. I’ll also test two concurrently active threads to ensure their messages remain tagged to their respective spawning cells (existing test only uses one thread). - Assistant
I’ll use
ExecuteCellCommanddirectly in an ad hoc Python snippet, avoiding any repository test changes. This regression probe will fail on the original behavior becauseprintwas present as the wrapper immediately. - Assistant
I’ll execute the probe now, including a function defined before the thread is created (the normal notebook pattern), and inspect emitted console operations.
- Assistant
The probe reached the output-inspection step successfully, so both identity assertions and lazy installation passed; only my ad hoc inspection assumed every stream entry was a dictionary, while this helper also stores typed notifications. I’ll rerun with the repository’s
MockStreamnormalization used bytest_thread_print. - Assistant
The explicit regression probe now passes: no
printglobal exists initially, bareprintresolves identically tobuiltins.print, and a subsequently constructed thread routes output to cellspawn.I’ll run formatting/lint checks on the modified files and inspect the final diff. I’ll also verify exceptional thread cleanup with a small direct probe, since that safeguards thread-ID isolation.
- Assistant
The code passes lint/format checks and exception cleanup. To directly cover the issue’s concurrency requirement, I’ll run one final ad hoc kernel scenario where two
mo.Threads are spawned by different cells, overlap behind a barrier, and print distinct labels; each cloned stream must report only its spawning cell. Then I’ll run the broader runtime lifecycle tests. - Assistant
Verification is complete. The broader run reached 302 passing tests (plus expected skips/xfail) before one environment-specific failure:
os.setsid()is forbidden in this container (PermissionError), unrelated to these changes. The targeted lifecycle suite had already passed, as did all focused routing tests.Checklist:
- Reproduced the reported error condition: the original kernel eagerly placed
print_overridein notebook globals, so bareprintwas notbuiltins.print; Numba itself is not installed, but this exact identity mismatch is the documented compilation trigger. - Confirmed the fix: an ad hoc live-kernel regression probe verified
print is builtins.printbefore anymo.Thread, then verified lazy override installation and routing to the spawning cell after thread creation.
- Reproduced the reported error condition: the original kernel eagerly placed
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.