Skip to content

Undo Wasm threading fix on panic context tracking #1107

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

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions godot-core/src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use sys::out;

#[cfg(feature = "trace")]
pub use crate::meta::trace;
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
use std::cell::RefCell;

use crate::global::godot_error;
Expand Down Expand Up @@ -321,12 +321,12 @@ pub(crate) fn has_error_print_level(level: u8) -> bool {
/// Internal type used to store context information for debug purposes. Debug context is stored on the thread-local
/// ERROR_CONTEXT_STACK, which can later be used to retrieve the current context in the event of a panic. This value
/// probably shouldn't be used directly; use ['get_gdext_panic_context()'](get_gdext_panic_context) instead.
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
struct ScopedFunctionStack {
functions: Vec<*const dyn Fn() -> String>,
}

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
impl ScopedFunctionStack {
/// # Safety
/// Function must be removed (using [`pop_function()`](Self::pop_function)) before lifetime is invalidated.
Expand All @@ -351,7 +351,7 @@ impl ScopedFunctionStack {
}
}

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
thread_local! {
static ERROR_CONTEXT_STACK: RefCell<ScopedFunctionStack> = const {
RefCell::new(ScopedFunctionStack { functions: Vec::new() })
Expand All @@ -360,10 +360,10 @@ thread_local! {

// Value may return `None`, even from panic hook, if called from a non-Godot thread.
pub fn get_gdext_panic_context() -> Option<String> {
#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
return ERROR_CONTEXT_STACK.with(|cell| cell.borrow().get_last());

#[cfg(not(all(debug_assertions, not(wasm_nothreads))))]
#[cfg(not(debug_assertions))]
None
}

Expand All @@ -378,10 +378,10 @@ where
E: Fn() -> String,
F: FnOnce() -> R + std::panic::UnwindSafe,
{
#[cfg(not(all(debug_assertions, not(wasm_nothreads))))]
let _ = error_context; // Unused in Release or `wasm_nothreads` builds.
#[cfg(not(debug_assertions))]
let _ = error_context; // Unused in Release.

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
ERROR_CONTEXT_STACK.with(|cell| unsafe {
// SAFETY: &error_context is valid for lifetime of function, and is removed from LAST_ERROR_CONTEXT before end of function.
cell.borrow_mut().push_function(&error_context)
Expand All @@ -390,7 +390,7 @@ where
let result =
std::panic::catch_unwind(code).map_err(|payload| extract_panic_message(payload.as_ref()));

#[cfg(all(debug_assertions, not(wasm_nothreads)))]
#[cfg(debug_assertions)]
ERROR_CONTEXT_STACK.with(|cell| cell.borrow_mut().pop_function());
result
}
Expand Down
Loading