Skip to content

Conversation

pbergeron-adsk
Copy link
Contributor

@pbergeron-adsk pbergeron-adsk commented Sep 15, 2025

Summarize your change.

Remove redirecting of python's print() command to mu's print().

Describe the reason for the change.

When we're running live review with debug traces on, we get a ton of stuff in the console (as expected) but also sometimes RV mysteriously crashes. It crashes with the message "Unexpected State" (but may crash elsewhere in mu-ish related handlers - it may crash with "bus error", or "segmentation fault")

This "Unexpected State" message is printed from the Mu garbage collector, which gets into an invalid state, because Live Review is running multiple threads, and Mu isn't thread safe.

But what does calling print() from python have to do Mu's garbage collector? Well, it's because when we issue a python print() statement, it's actually redirected to Mu's print() command, which calls the garbage collector (again, not thread safe), and (sometimes) crashes, but 100% crashes if you let it live review run long enough with print statements.

(The code that redirects python's print() to Mu's print() is in rv_commands_setup.py)

We looked into the possibility of just removing the redirection of python print, but, this is not an option since we need to show print statements into the RV console.

As an alternative, we instead detect mu being called from a non-main thread, and if so, prints out a warning message (see screenshot) that Mu isn't thread safe and that this should not be allowed.

Nevertheless, we will special-case "print()" being called from any thread, because it's often used to print traces to debug python stuff (even multi-threaded python stuff). However, for print(), we don't let the mu code execute. We call cout() directly via a helper function.

Describe what you have tested and on which operating system.

Tested on macOS, but the problem appears on any OS.

Add a list of changes, and note any that might need special attention during the review.

If possible, provide screenshots.

See that we detect an unsafe print() from python, and that the unsafe print (with a prefix) appears both in the console and in the terminal.

See that other mu commands (like sendInternalEvent) are detected in the wrong thread and a warning appears both in the console and in the terminal.

image

…ed cases (mu isn't thread safe)

Signed-off-by: Patrick Bergeron <[email protected]>
Signed-off-by: Patrick Bergeron <[email protected]>
@pbergeron-adsk pbergeron-adsk changed the title removed python print redirect to mu, to avoid crashes in multi thread… SG-40348: Fix print crashing when called from non-main thread; add warning for other mu commands Sep 15, 2025
Copy link
Contributor

@bernie-laberge bernie-laberge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
(apart from the unused prefix which could be removed I think)

@markreidvfx
Copy link
Contributor

markreidvfx commented Sep 16, 2025

Thanks @pbergeron-adsk, I think this is a bug I've been trying to track down for ages. I have been getting random crashes if calling print from a python thread and thought it might be something like this.

Signed-off-by: Patrick Bergeron <[email protected]>
@pbergeron-adsk
Copy link
Contributor Author

Thanks @pbergeron-adsk, I think this is a bug I've been trying to track down for ages. I have been getting random crashes if calling print from a python thread and thought it might be something like this.

No problem!

@pbergeron-adsk
Copy link
Contributor Author

Note: I will bypass view testing because this is an intermittent bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants