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
Great, numba got installed. Now let's write a repro script that mimics marimo module patching and check whether Numba can compile a function referencing print, before and after the print override patch.
Assistant
There's a proper venv. Let's install numba into that venv and run repro there.
Assistant
Reproduced the issue: with print_override installed, Numba fails to compile. Let's confirm that without the override it works fine.
Assistant
Now let's update threads.py to add the dynamic install/uninstall logic.
Assistant
Now let's update the
runmethod to use these helpers.Assistant
All passing. Now let's re-run our numba repro with actual kernel-based flow to validate the fix end-to-end (via Kernel, similar to test_thread_print but also checking print identity for non-thread cells).
Assistant
Now let's write a more thorough reproduction/verification test using the actual Kernel class (mirroring the test harness) to check: (1) print identity preserved without Threads, (2) Numba compiles fine without Threads, (3) mo.Thread output routing still works with isolation between concurrent threads.
Assistant
All 4 tests pass. Let's remove the temp test file and now run the full relevant test suites to make sure nothing else broke.
Assistant
Pre-existing failure unrelated to our change (environment permission issue with
os.setsid()), confirmed by testing on the original codebase without our changes. Now let's run broader test suites (messaging, print_override, patches) to be thorough.Assistant
All pass. Let's also run the broader test suite for pyodide session and script/kernel_runner to make sure nothing broke there (since they pass print_override=None already, unaffected), and check patches.py tests specifically.
Assistant
Now let's also test concurrent thread output isolation more rigorously, since the issue emphasizes "output isolation between concurrent threads."
Assistant
Concurrent thread output isolation confirmed. Let's remove this temp test file.
Assistant
Now let's check
git difffor a final review, and also double check other places usingpatch_main_module_context/scripts for consistency.Assistant
Let's also run mypy on these files if available.
Assistant
Good, no other dependents. Now let's run the full test suite one more time for messaging + runtime + a broader sanity check (excluding the known pre-existing failing test), and also check
_pyodidetests briefly.Assistant
Pre-existing failure unrelated to our change (missing static build artifact). Let's confirm.
Assistant
Confirmed pre-existing, unrelated to our change. Now let's clean up temp files and remove numba if it was only installed for testing purposes (not necessary to uninstall, but let's leave it since it's a venv and doesn't affect repo files). Let's check no repo files changed unexpectedly.
Assistant
Clean. Let's do one final full re-run of the numba repro script from the beginning to make sure everything works end to end with the actual patch flow (not just my temp pytest tests), and clean up temp files.
Assistant
Both scenarios now work correctly — with
print_overridepassed but no active thread,printremains true identity, and Numba compiles successfully. Let's do one more sanity check with the full test suite for_messagingand_runtimecombined to ensure nothing regressed, then finalize.Assistant
Only the known pre-existing environment-related failure remains. Now let's uninstall numba from the venv since it was only for testing (not required, but to keep environment clean — actually not necessary since it's not part of repo). I'll leave it as-is since it doesn't affect repo files.
Assistant
Checklist review
-
If an error was reported in the issue description, I have successfully reproduced it. Yes. I reproduced the core problem with a script that patched
__main__viapatches.patch_main_module(..., print_override=print_override)(mirroring whatruntime.pydoes for every kernel) and tried tonumba.njita function callingprint. This failed withnumba.core.errors.TypingError: Untyped global name 'print': Cannot determine Numba type of <class 'function'>because the module's globalprintwas a Python closure (print_override), not the true builtinprint, so Numba couldn't recognize it. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. After the fix, the same script compiles and runs correctly (
3/4printed as expected), matching the baseline behavior with no override at all. I also added and ran temporary pytest-based tests (removed afterward) using the realKernelfixtures confirming: (1)print is builtins.printholds for notebooks that never usemo.Thread; (2) a Numba-jitted function callingprintcompiles fine inside a real kernel-run cell; (3)mo.Threadoutput is still correctly routed to the spawning cell's stream; (4) after a thread completes,printreverts to builtin identity again. -
I have thought about edge cases and made sure my fix handles them as well. Yes:
- Reference-counting keyed by
id(globals_dict)so multiple concurrentmo.Threads (even nested ones) sharing the same kernel correctly install/uninstall the override without one thread's completion prematurely restoring identity while another thread still needs routed output. - Each kernel's globals dict is patched independently (not a process-wide
builtins.printpatch), so concurrent sessions in run mode don't leak the override across sessions. - Output isolation between concurrent threads was explicitly verified with a test spawning two
mo.Threads printing interleaved output — each thread's output stream only contained its own messages. - Wrapped
super().run()intry/finallyso the override (andTHREADS/context teardown) is properly reverted even if the thread target raises an exception. ScriptRuntimeContextthreads are left untouched (as before), since standard streams aren't redirected in script mode andprint_overridewas never installed there.
- Reference-counting keyed by
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. Yes. Ran
tests/_runtime(1444 passed) andtests/_messaging(266 passed) fully, plustests/_pyodide,tests/_runtime/test_threads.py, andtests/_messaging/test_print_override.pyspecifically. The only failures present (test_non_windows_does_not_change_event_loop_policyandtest_pyodide_bridge_export_html) were confirmed viagit stashto pre-exist on the unmodified repository (due to sandbox environment limitations:os.setsid()permission error and a missing static build artifact), unrelated to this change.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →