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
Let me start by exploring the repository structure to understand the
Assistant
.Let me now look at how print is currently handled. The issue says "Marimo's print routing breaks Numba compilation". The key issue is that when
mo.Threadis NOT used,printmust retain builtin identity so Numba can compile functions that call it. But whenmo.ThreadIS used,printmust route output to the spawning cell.Let me search for where
printmight be overridden or replaced in the marimo codebase.Assistant
Now I can see the print_override.py file. Let me see how it's being used in patches.py.
Assistant
Now I understand the problem! The
print_overridefunction is replacing the built-inprintfunction in the kernel's main module. Numba JIT compilation does not allow calling functions that are not the built-inprint(it requiresprintto be the exactbuiltins.printfunction).Let me verify this by reproducing the issue.
Assistant
Now let me see how
print_overrideis used in the runtime.Assistant
Let me look at the existing tests for this:
Assistant
Now let me reproduce the issue with Numba:
Assistant
Hmm interesting, the simple case worked. Let me try a more complex case to see when it fails.
Assistant
Let me try with print as a first-class function or in unsupported ways.
Assistant
Now I see! The key line is:
if callee is print:This is checking if the callee is THE print function (the built-in
printfunction). If we have ourprint_overridefunction in__main__.__dict__, Numba will try to call it but won't matchis print, so it will fail with "Unknown" function call.Wait, but the test showed it worked... Let me re-test more carefully. Let me look at how
__main__.printwould be resolved at function compilation.Looking again - the numba code calls
func_ir.infer_constant(expr.func)to get the print function from the IR. In our case,printis looked up in the function's globals, which is__main__. So when__main__hasprint = print_override, it getsprint_override. But that fails theis printcheck.Assistant
Now I can reproduce the issue. The error is:
Untyped global name 'print': Cannot determine Numba type of <class 'function'>This is the exact error mentioned in the issue. Now let me see the thread class to understand the setup:
Assistant
Now I need to understand the issue. The
print_overrideis installed in__main__.__dict__to makemo.Threadwork. But this breaks Numba because Numba's@njitchecks ifprintis the built-inprint.Let me see how
mo.Threadinvokes its target:Assistant
Now let me look more carefully at how the cell is executed:
Assistant
Let me now reproduce the issue more concretely with a script:
Assistant
Now I can reproduce the issue. Let me also look at the
printfunction to make sure I understand how to fix it:Assistant
Now let me see how the test for threads work:
Assistant
Now I have a clear understanding. Let me look at the issue more carefully to design the solution:
Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The key insight is:
- Currently
print_overrideis always installed in__main__.__dict__, breaking Numba. - We should only install
print_overridewhenmo.Threadis running.
Let me first look at the print_override test to make sure my fix is compatible:
- Currently
Assistant
Now let me look at how the print_override is used in production:
Assistant
Let me now implement the fix. First, let me see what the actual context types and scripts look like:
Assistant
Now I have a good understanding. Let me now implement the fix:
Assistant
Now let me update
threads.pyto use the new install/uninstall functions:Assistant
Let me fix the import ordering:
Assistant
Now let me update the
runtime.pyto not installprint_overridein__main__.__dict__by default:Assistant
Now let me verify the fix works:
Assistant
I introduced a circular import. Let me fix this:
Assistant
The fix works. Now let me verify the concurrent threads case:
Assistant
All test cases pass. Now let me run the existing tests:
Assistant
All tests pass. Now let me run the thread tests:
Assistant
All thread tests pass too. Now let me also test the cell-level print (the test_print.py in _server):
Assistant
All tests pass. Now let me also run a broader test to make sure nothing else is broken:
Assistant
Let me check the one failure to see if it's related to my change:
Assistant
This is a pre-existing test failure unrelated to my changes (a
PermissionErrorwithos.setsid()in the test environment). Let me verify by reverting my changes temporarily:Assistant
Good, this test was already failing before my changes. It's an environment-specific issue, not related to my fix.
Now let me look at the final state of my changes:
Assistant
Now let me write a test that verifies my fix works end-to-end:
Assistant
Hmm, the test failed because the
numbamodule was uninstalled byuv sync. Let me reinstall it.Assistant
Test 1 passes (Numba works without mo.Thread). Test 2 shows that Numba fails inside a mo.Thread, which is the expected behavior given Python's name resolution limitations. Test 3 confirms
printis restored to builtins after the thread ends.Let me also test the actual thread print routing:
Assistant
The
test_thread_printtest passes. Let me also run a broader test of related tests:Assistant
All related tests pass. Now let me also check the patches test:
Assistant
Let me look at the runtime tests in general for any breakage:
Assistant
The first failure is the pre-existing test failure unrelated to my changes. Let me skip it and run the rest:
Assistant
All tests pass. Now let me also think about edge cases:
Assistant
All edge cases work correctly. Let me also verify the patches test:
Assistant
The test session still uses
print_override_fn=print_overridewhich still installs the override. This is OK for tests, but let me verify there's no test that explicitly uses Numba:Assistant
No Numba tests in the test suite. Let me look at the test session helper to understand its behavior:
Assistant
Let me look at the final state of my changes:
Assistant
Let me also check the conftest.py to see if there are any related fixtures:
Assistant
Let me do a final review of the changes:
Assistant
Let me now run the relevant tests to confirm everything works:
Assistant
All tests pass. Now let me also write a comprehensive end-to-end test to verify the fix:
Assistant
The
printfunction in__builtins__may not be accessible as a member if__builtins__is a dict (rather than a module). Let me check.Assistant
I see -
__builtins__is a dict in this case (not a module). Let me update the test:Assistant
All three scenarios pass:
- Notebook without mo.Thread:
printis the built-in print. - Numba compilation: Works in notebook without mo.Thread.
- mo.Thread is used: Print is the built-in print before and after the thread runs.
Let me clean up the temporary test file:
- Notebook without mo.Thread:
Assistant
Let me also check that I haven't broken any import paths or other things:
Assistant
The
_active_countis showing 0 in the worker because each worker imports it at the start, so the local reference is stale. Let me check the actual module:Assistant
The concurrent thread handling works correctly:
- The counter goes from 0 → 1 → 2 → 3 as threads are started.
- The counter goes from 3 → 2 → 1 → 0 as threads end.
printisprint_overridewhile threads are running.printisbuiltins.printafter all threads have ended.
Now let me also test that an exception in the thread properly handles the counter:
Assistant
The counter is properly managed even when an exception occurs.
Let me also look at the test that uses the test session helper to make sure the actual thread test passes (with the test session's
print_override_fn=print_override):Assistant
All 8 thread tests pass, including
test_thread_printwhich validates the print routing behavior.Let me also run a broader test set to make sure nothing else is broken:
Assistant
All 303 tests pass. Let me also make sure the broader runtime tests pass:
Assistant
All tests pass. Let me also do a final check to make sure the test_thread_print test specifically tests the print routing to the cell:
Assistant
Let me also verify that the test for
test_print_override.pystill testsprint_overridedirectly:Assistant
All 6 print_override tests pass.
Let me now do a final summary of the changes I made:
Assistant
Let me also double-check my changes look good:
Assistant
Let me also run a final check on all the tests:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.