4343#endif
4444
4545static ErrorHandlerList *error_handler_list = nullptr ;
46+ static thread_local bool is_printing_error = false ;
47+
48+ static void _err_print_fallback (const char *p_function, const char *p_file, int p_line, const char *p_error_details, ErrorHandlerType p_type, bool p_reentrance) {
49+ if (p_reentrance) {
50+ fprintf (stderr, " While attempting to print an error, another error was printed:\n " );
51+ }
52+
53+ fprintf (stderr, " %s: %s\n " , _error_handler_type_string (p_type), p_error_details);
54+
55+ if (p_function && p_file) {
56+ fprintf (stderr, " at: %s (%s:%i)\n " , p_function, p_file, p_line);
57+ }
58+ }
4659
4760void add_error_handler (ErrorHandlerList *p_handler) {
4861 // If p_handler is already in error_handler_list
@@ -91,12 +104,21 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
91104
92105// Main error printing function.
93106void _err_print_error (const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, bool p_editor_notify, ErrorHandlerType p_type) {
107+ if (is_printing_error) {
108+ // Fallback if we're already printing an error, to prevent infinite recursion.
109+ const char *err_details = (p_message && *p_message) ? p_message : p_error;
110+ _err_print_fallback (p_function, p_file, p_line, err_details, p_type, true );
111+ return ;
112+ }
113+
114+ is_printing_error = true ;
115+
94116 if (OS::get_singleton ()) {
95117 OS::get_singleton ()->print_error (p_function, p_file, p_line, p_error, p_message, p_editor_notify, (Logger::ErrorType)p_type, ScriptServer::capture_script_backtraces (false ));
96118 } else {
97119 // Fallback if errors happen before OS init or after it's destroyed.
98120 const char *err_details = (p_message && *p_message) ? p_message : p_error;
99- fprintf (stderr, " %s: %s \n at: %s (%s:%i) \n " , _error_handler_type_string (p_type) , err_details, p_function, p_file, p_line );
121+ _err_print_fallback (p_function, p_file, p_line , err_details, p_type, false );
100122 }
101123
102124 _global_lock ();
@@ -108,6 +130,8 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
108130 }
109131
110132 _global_unlock ();
133+
134+ is_printing_error = false ;
111135}
112136
113137// For printing errors when we may crash at any point, so we must flush ASAP a lot of lines
@@ -116,11 +140,19 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
116140void _err_print_error_asap (const String &p_error, ErrorHandlerType p_type) {
117141 const char *err_details = p_error.utf8 ().get_data ();
118142
143+ if (is_printing_error) {
144+ // Fallback if we're already printing an error, to prevent infinite recursion.
145+ _err_print_fallback (nullptr , nullptr , 0 , err_details, p_type, true );
146+ return ;
147+ }
148+
149+ is_printing_error = true ;
150+
119151 if (OS::get_singleton ()) {
120152 OS::get_singleton ()->printerr (" %s: %s\n " , _error_handler_type_string (p_type), err_details);
121153 } else {
122154 // Fallback if errors happen before OS init or after it's destroyed.
123- fprintf (stderr, " %s: %s \n " , _error_handler_type_string ( p_type), err_details );
155+ _err_print_fallback ( nullptr , nullptr , 0 , err_details, p_type, false );
124156 }
125157
126158 _global_lock ();
@@ -132,6 +164,8 @@ void _err_print_error_asap(const String &p_error, ErrorHandlerType p_type) {
132164 }
133165
134166 _global_unlock ();
167+
168+ is_printing_error = false ;
135169}
136170
137171// Errors with message. (All combinations of p_error and p_message as String or char*.)
0 commit comments