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 what we're working with.
Assistant
Now let me look at the threads module:
Assistant
Let me look at where
printis being installed/imported as the print_override:Assistant
Let me see where the print override is used in kernel_runner:
Assistant
Now let me look at the script context to understand how the print override is used there:
Assistant
Let me search for related tests:
Assistant
Now let me see how
printis set up in the kernel. Let me also look forinput_overrideto compare:Assistant
Now let me look at the script_context.py file to understand how it uses print_override:
Assistant
Let me look at the script_runner.py and kernel_runner.py to see how print_override is passed:
Assistant
Let me see if there is a way to detect if Numba is being used:
Assistant
Let me first try to reproduce the issue:
Assistant
Let me try to reproduce the issue in a marimo kernel:
Assistant
Now I have reproduced the issue. Let me understand the solution better. The fix should be:
- By default,
printshould be the builtinprint(so Numba can compile functions) - When
mo.Threadis being used,printshould beprint_overrideto route output to the spawning cell
Let me look at the threads module more carefully to understand where to add this:
- By default,
Assistant
Let me verify where the print_override is actually installed:
Assistant
Now I have a clear understanding of the issue. Let me reproduce it once more to be sure:
Assistant
Let me try a simple approach first - just removing the print_override from runtime.py:
Assistant
Let me look at the threads.py to understand the flow:
Assistant
Now let me look at the print_override.py and understand the current implementation:
Assistant
Now I have a clear understanding. Let me implement the fix. The plan is:
- Don't install the print_override in the main module by default in
patches.py - In
threads.py, install the print_override only when amo.Threadis running (using ref-counting) - Update the relevant files
- Don't install the print_override in the main module by default in
Assistant
Now let me make the changes. First, let me update
patches.pyto not install theprint_overridein the main module (keep the parameter for API compatibility but make it a no-op):Assistant
Now let me update
threads.pyto install the print_override when amo.Threadis running:Assistant
Now let me update
threads.pyto install the print_override when a mo.Thread is running:Assistant
Let me run the existing tests to make sure I haven't broken anything:
Assistant
I have a circular import. Let me fix it by using a lazy import in
print_override.py:Assistant
Now let me run the reproduction test to confirm the fix works:
Assistant
The Numba test now passes. Let me write a more comprehensive test to make sure the mo.Thread still works correctly:
Assistant
I see a bug. Let me fix it:
Assistant
Now let me test the case where the user has a custom
printfunction:Assistant
Let me run the existing tests:
Assistant
All existing tests pass. Let me also run the patches tests:
Assistant
Let me look at the threads test to understand how it tests the print functionality:
Assistant
Now let me write a more comprehensive test that uses the actual mo.Thread and verifies the print routing works:
Assistant
The test setup is getting complex. Let me just verify the existing tests pass and write a simpler test:
Assistant
Let me run all the tests in test_threads.py:
Assistant
Let me also run a broader test to make sure I haven't broken anything:
Assistant
Let me check if the failed test is related to my changes:
Assistant
Let me verify this test was already failing before my changes:
Assistant
Good, this test was already failing before my changes. Let me restore my changes:
Assistant
Let me also run the messaging tests:
Assistant
Let me check the current state of the changes:
Assistant
Let me write a test that specifically tests the Numba scenario with the marimo kernel:
Assistant
Let me write a test using the existing test infrastructure:
Assistant
Let me write the test in the right location:
Assistant
The test passes! Now let me delete my test file since I'm not supposed to add new tests:
Assistant
Let me run all the relevant tests one more time to make sure everything works:
Assistant
Let me run my reproduction test to confirm the fix works:
Assistant
Let me also test a more complex scenario - what happens when we have multiple threads:
Assistant
The ref-counted approach works correctly. Now let me also test that the Numba compilation works in a scenario with mo.Thread:
Assistant
Let me fix the test to check correctly:
Assistant
Let me test a scenario where a user-defined
printis preserved across thread lifecycle:Assistant
Let me check if there are any other places where the
print_overrideis used:Assistant
Let me clean up the unused
print_overrideparameter inpatches.pyandkernel_lifecycle.py. First let me check if it's truly safe to remove:Assistant
I see there's a test helper that also uses
print_override_fn=print_override. Let me check that.Assistant
Let me run the specific test to make sure the print routing still works:
Assistant
Let me also verify my changes don't have any issues by running a wider set of tests:
Assistant
All 30 tests pass. Let me also run my reproduction test one more time:
Assistant
Let me clean up the temporary test files:
Assistant
Let me check the final state of my changes:
Assistant
Let me run a broader test to make sure nothing is broken:
Assistant
Excellent! All 1444 tests pass (the one failure is pre-existing and unrelated to my changes - it's a permissions issue in the test environment). Let me create a final reproduction test to verify the fix:
Assistant
Let me clean up the temporary test file:
Assistant
Let me do a final check on the changes:
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 →
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 →