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 Marimo’s print-routing and threading implementation, then inspect relevant tests and reproduce the builtin-identity problem before modifying code.
- Assistant
rgis unavailable, so I’ll use recursivegrepto find assignments or wrappers involvingbuiltins.print, along with themo.Threaddefinition. - Assistant
The likely mechanism is centralized in
print_override.py; I’ll inspect it,Thread, stream proxies, and their tests to understand lifecycle and concurrency constraints. - Assistant
The override is injected into every notebook’s
__main__globals at kernel creation, which explains why Numba sees a Python wrapper rather than the builtin. A likely compatible fix is lazy injection only whenmo.Threadis actually constructed. I’ll inspect module creation, runtime context/module references, and thread tests to find the safest insertion point and avoid circular imports. - Assistant
Lazy mutation of the shared notebook global would still affect Numba after a thread is constructed and could race across run-mode sessions. A better approach may be to temporarily replace only each thread target function’s global
print, but that globals dict is shared with the notebook and concurrent functions. I’ll examine runtime context fields and complete thread tests; this may support a per-thread stream-based solution that leavesprintbuiltin entirely. - Assistant
The requirement explicitly scopes builtin identity to notebooks that do not use
mo.Thread, so lazy installation is appropriate and avoids a much broader stdout redesign. I’ll locate how a thread can access its kernel’s global namespace and verify whetherThread.__init__can install the existing override into only that notebook module. - Assistant
KernelRuntimeContext.globalsprovides the correct per-notebook namespace. I’ll implement lazysetdefault("print", print_override)inThread.__init__for kernel contexts, and stop eager installation by having kernel creation retain the callback for thread use rather than passing it into module creation. Before choosing the exact shape, I’ll inspectKernelArgsand patch tests so internal callback behavior remains coherent. - Assistant
I’ll reproduce the issue directly against a test kernel: before the fix, the notebook global
printshould be the wrapper andprint is builtins.printshould be false. I’ll also check whether Numba is installed for an end-to-end compilation check. - Assistant
The plain interpreter lacks project dependencies (
msgspec). I’ll use the repository’s lockeduvenvironment to run the reproduction and tests rather than altering source or tests. - Assistant
The reproduction confirms the notebook receives a non-builtin
print. I’ll now disable eager injection during kernel creation, lazily install the override into the current kernel’s globals whenmo.Threadis constructed, and make thread registration/context cleanup exception-safe so failed concurrent targets cannot leave stale routing state. - Assistant
The implementation is in place. I’ll verify both halves of the behavior in one kernel: builtin identity before any
mo.Thread, then lazy override installation and routed output after constructing/running a thread. After that I’ll run the focused test suites. - Assistant
Focused tests pass. The ad hoc thread was created outside a cell execution context, so its expected missing-cell-id serialization warning does not reflect notebook execution; the official
test_thread_printpassed and validates routed output in a real cell. I’ll now inspect the full thread test names and add a temporary (non-repository) runtime scenario for concurrent threads spawned by different cells, since isolation is an explicit requirement. - Assistant
To confirm the reported Numba symptom end-to-end, I’ll use
uv’s temporary dependency mechanism (without changing project files) to compile a function in the fixed kernel namespace. This verifies builtin identity is not merely cosmetic. - Assistant
Numba now compiles and executes a function containing
print. I’ll inspect the kernel-construction mock assertion and run formatting/type/lint checks on the changed files; this catches any expectation that the callback still be forwarded eagerly. - Assistant
Lint and formatting pass; the filtered test command simply selected no tests (exit 5), not a failure. I’ll run the complete kernel lifecycle/runtime-adjacent suites and inspect
ExecReqProviderso I can exercise concurrent cell routing through the same public execution path as existing tests. - Assistant
All 48 relevant tests pass. I’ll perform the explicit isolation scenario with two overlapping
mo.Threads created in different cells, then inspect each console notification’scell_idto ensure lazy installation did not compromise routing. - Assistant
The concurrent notebook run itself completed without kernel errors; only my inspection assumed every recorded operation was a dictionary, while this mock also stores typed notifications. I’ll filter dictionary operations correctly and rerun the assertions.
- Assistant
The helper stream stores serialized/typed messages rather than the dictionary view used by thread tests. I’ll use the same
tests._messaging.mocks.MockStreamadapter as the official tests and inspect the resulting cell operations. - Assistant
This helper’s cloned thread streams do not share the parent mock’s in-memory message list, unlike the
kfixture used by official thread tests, so parent inspection cannot observe their messages. I’ll capture each thread’s context stream in notebook globals (as the official test does), then assert routing separately for each cloned stream. - Assistant
The concurrent scenario confirms isolation: thread A’s output is tagged with cell A and thread B’s with cell B. I’ll do a final diff review and run the project’s targeted type checker (if configured) before submission.
- Assistant
The final diff is minimal and clean. This repository uses mypy rather than pyright, so I’ll run mypy on the two changed modules, then submit if it passes. Checklist assessment:
- Reproduced: the kernel global
printwas a wrapper and failed builtin identity. - Fix confirmed: builtin lookup now equals
builtins.print, and a temporary Numba installation successfully compiled/executed a printed function. - Edge cases: verified lazy thread routing, two overlapping threads from distinct cells, and exception-safe stale thread-ID cleanup.
- Existing tests: 48 focused lifecycle/thread/print/proxy tests pass, plus the patches suite (30 pass, 10 optional-dependency skips); lint and formatting pass.
- Reproduced: the kernel global
- Assistant
Mypy is declared only in an optional development dependency group and is not installed in the current environment. I’ll run it via a temporary
uv --with mypyenvironment (without changing project files), then submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.