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
Good,
ctx.globalsgives access to the module dict. Now let's view patches.py fully and runtime.py around print_override usage, and kernel_lifecycle.py args.Assistant
Now let's update threads.py to install the override lazily.
Assistant
Good, "Thread-local context for the runtime" — confirms
get_context()is thread-local already, so eachmo.Threadhas isolatedExecutionContext/stream viainitialize_context(self._marimo_ctx)insideThread.run(). That satisfies output isolation. Now let's verify no other place patchesprintunconditionally, and run tests.Assistant
Now let's write a reproduce script simulating: (1) numba compiling a function using print without any mo.Thread usage should work with
printidentity intact; (2) mo.Thread routing still works and isolates output.Assistant
Now
printis no longer patched at module creation. Let's run the existing test suite for print_override, threads, patches, and runtime kernel tests to confirm nothing's broken.Assistant
Let's check other tests referencing patches.py, kernel_lifecycle.py, and run broader relevant test dirs.
Assistant
Confirmed pre-existing environment failure unrelated to our changes. Now let's run additional relevant test suites and the asgi/session_manager tests referencing print_override/create_main_module.
Assistant
Pre-existing environment failures, unrelated to our change (same count before/after). Now let's write a reproduction script simulating a numba-like identity check plus actual mo.Thread usage end-to-end, using the test helper session infra.
Assistant
This uses a
kfixture (Kernel) from conftest. Let's use pytest with a small custom test script (not added to repo, just run inline) to verify both requirements end-to-end using the real kernel fixture infra.Assistant
Let's create a temp test script (not added to repo) to validate both scenarios: no-thread print identity preserved, and mo.Thread routing/isolation works.
Assistant
Both pass. Now let's verify the fix actually matters by reverting temporarily and confirming the first test fails without the fix.
Assistant
Confirms the bug reproduces without the fix, and our fix resolves it. Now let's test actual Numba compilation directly if available.
Assistant
Now let's review full diff and re-run the broader relevant test suites once more for safety.
Assistant
None reference the module-dict patch directly. Now let's run the full targeted test directories once more (messaging, runtime) to be thorough.
Assistant
Only the pre-existing environment-related failure remains (already confirmed unrelated to our change). All other 1444 tests pass.
Assistant
Let's do a final review of the diff.
Assistant
This looks good and minimal. Let's go through the checklist explicitly now.
Assistant
Checklist review
-
If an error was reported in the issue description, I have successfully reproduced it. Yes. I wrote a temporary test (run inside the real kernel test harness, then removed) that showed that in the previous code,
print is builtins.printevaluated toFalseinside a notebook that never usedmo.Thread, becausepatches.create_main_module()unconditionally shadowedprintin the notebook's global namespace withprint_override. This is precisely the condition that breaks Numba compilation (Numba's typing machinery requires the resolved globalprintto be identical to the builtinprintobject). -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. After the fix, the same test shows
print is builtins.printisTruefor notebooks that never construct amo.Thread. I verified this both by running the test against the patched code (passes) and by stashing the patch and re-running (fails), confirming the fix is what resolves it. -
I have thought about edge cases and made sure my fix handles them as well.
mo.Threadoutput routing to the spawning cell still works (verified via existing tests intests/_runtime/test_threads.py, e.g.test_thread_has_own_stream,test_thread_output_append, plus my own temporary end-to-end test), sinceprintis patched into the shared globals lazily viactx.globals.setdefault("print", print_override)the first time amo.Threadis created.- Output isolation between concurrent threads is preserved because
RuntimeContext/ExecutionContextare already thread-local (get_context()), and eachmo.Threadgets its ownKernelRuntimeContext/ScriptRuntimeContextcopy with its own stream/cell id, soprint_override's existing thread-id-based routing logic is untouched. - Used
setdefault(not blind assignment) so a user who has already defined their ownprintin notebook globals is not clobbered. - Script-mode (
AppScriptRunner) already passedprint_override=Nonebefore this change, so its behavior is unaffected. - Avoided introducing a circular import between
threads.pyandprint_override.pyby doing the import lazily insideThread.__init__. - No new public interfaces were introduced; parameter signatures of
create_main_module/patch_main_moduleare unchanged for backward compatibility with existing callers/test helpers.
-
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/_messaging,tests/_runtime(1444 passed, 183 skipped, 6 xfailed, 8 xpassed, only 1 pre-existing unrelated failure due toos.setsid()sandbox restriction, confirmed to fail identically before my change too), plus targeted runs oftest_print_override.py,test_threads.py, andtest_thread_local_proxy.py(all passing).
I'm satisfied with the fix. Submitting now.
-
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 →