@@ -331,9 +331,11 @@ where
331
331
let info: Arc < Mutex < Option < GodotPanicInfo > > > = Arc :: new ( Mutex :: new ( None ) ) ;
332
332
333
333
// Back up previous hook, set new one
334
- let prev_hook = std :: panic :: take_hook ( ) ;
335
- {
334
+ # [ cfg ( debug_assertions ) ]
335
+ let prev_hook = {
336
336
let info = info. clone ( ) ;
337
+ let prev_hook = std:: panic:: take_hook ( ) ;
338
+
337
339
std:: panic:: set_hook ( Box :: new ( move |panic_info| {
338
340
if let Some ( location) = panic_info. location ( ) {
339
341
* info. lock ( ) . unwrap ( ) = Some ( GodotPanicInfo {
@@ -345,10 +347,15 @@ where
345
347
eprintln ! ( "panic occurred, but can't get location information" ) ;
346
348
}
347
349
} ) ) ;
348
- }
350
+
351
+ prev_hook
352
+ } ;
349
353
350
354
// Run code that should panic, restore hook
351
355
let panic = std:: panic:: catch_unwind ( code) ;
356
+
357
+ // Restore the previous panic hook if in Debug mode
358
+ #[ cfg( debug_assertions) ]
352
359
std:: panic:: set_hook ( prev_hook) ;
353
360
354
361
match panic {
@@ -358,17 +365,20 @@ where
358
365
// TODO write custom panic handler and move this there, before panic backtrace printing
359
366
flush_stdout ( ) ;
360
367
361
- let guard = info. lock ( ) . unwrap ( ) ;
362
- let info = guard. as_ref ( ) . expect ( "no panic info available" ) ;
363
-
364
- if print {
365
- godot_error ! (
366
- "Rust function panicked at {}:{}.\n Context: {}" ,
367
- info. file,
368
- info. line,
369
- error_context( )
370
- ) ;
371
- //eprintln!("Backtrace:\n{}", info.backtrace);
368
+ // Handle panic info only in Debug mode
369
+ #[ cfg( debug_assertions) ]
370
+ {
371
+ let guard = info. lock ( ) . unwrap ( ) ;
372
+ let info = guard. as_ref ( ) . expect ( "no panic info available" ) ;
373
+ if print {
374
+ godot_error ! (
375
+ "Rust function panicked at {}:{}.\n Context: {}" ,
376
+ info. file,
377
+ info. line,
378
+ error_context( )
379
+ ) ;
380
+ //eprintln!("Backtrace:\n{}", info.backtrace);
381
+ }
372
382
}
373
383
374
384
let msg = extract_panic_message ( err) ;
0 commit comments