-
-
Notifications
You must be signed in to change notification settings - Fork 22.9k
Prevent infinite recursion during printing #108860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't test, but this makes a lot of sense to me.
As discussed in the earlier PR, we can expect for infinite recursions to occur again and again, especially during development. We should really have a robust fallback.
thread_local
notably incurs some cost, but given that printing is not performance critical (and costly anyway), it should be acceptable.
cd7cae5
to
7a45095
Compare
If anyone wants to try it for themselves, this MRP should (only with this PR) force the recursion to happen and thus show these new fallback messages in the terminal output: logger_recursion_2025-07-22_13-58-04.zip |
I tested with the reproducer. I didn't get an infinite recursion on master (71a9948) (but I did get a single recursion, perhaps this is what you wanted to test?). Instead, the
The new implementation printed a bunch of fallback errors, and falls back for the second error message:
One thing I'm noting is that there is no "following error" in the last example. It's particularly unclear to me which is the "following error" and which is the "another error". I have a third proposition for the message, what do you think? "While attempting to print an error, another error was printed:" |
7a45095
to
836a1a0
Compare
On The MRP was mostly just an easy way to test these new fallbacks, now that the reentrancy guard in
Fair enough. That does indeed read a bit weird.
Done! That does indeed read better. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-approving after changes. Should be good to merge now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested locally with the MRP from #108860 (comment), it works as expected. Code looks good to me.
Just printing normally from _enter_tree
While attempting to print a message, another message was printed:
Trying to print normally from _log_message
While attempting to print a message, another message was printed:
Trying to print rich from _log_message
Just printing rich from _enter_tree
While attempting to print a message, another message was printed:
Trying to print normally from _log_message
While attempting to print a message, another message was printed:
Trying to print rich from _log_message
ERROR: Printing an error from _enter_tree
at: push_error (./core/variant/variant_utility.cpp:1024)
GDScript backtrace (most recent call first):
[0] _enter_tree (res://main.gd:18)
While attempting to print an error, another error was printed:
ERROR: Trying to log an error from _log_error
at: push_error (./core/variant/variant_utility.cpp:1024)
Note that the recursion messages are not colored according to their type (i.e. errors appear as plain text, not red). This could probably be added later on unless there's a good reason not to do it.
Recursion messages also won't appear in the editor Output panel, but I don't think anything can be done about that.
I think we don't want these messages to appear in the editor. Getting them there risks more recursion, and you really don't want more risks in your fallback. |
Thanks! |
I don't know if it makes any real difference, but with this being merged we might want to re-evaluate the |
Fixes #108838.
Supersedes #108839.
This introduces a reentrancy guard to
_err_print_error
, and another reentrancy guard to cover__print_line
,__print_line_rich
andprint_error
, which is meant to prevent cases where we (deliberately or not) emit an error from within a logger, or print from within a logger, thus causing infinite recursion.In such cases we would now fall back on using
fprintf
instead.I also removed an already existing reentrancy guard in
CoreBind::OS::LoggerBind
, which I added as part of #91006 to achieve the same effect, albeit more localized.