Skip to content

Commit 6ea1629

Browse files
committed
Cross-thread access in singlethreaded mode: protect against insta-abort during unwind
1 parent d133839 commit 6ea1629

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

godot-ffi/src/binding/single_threaded.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,23 @@ impl BindingStorage {
156156
"Godot engine not available; make sure you are not calling it from unit/doc tests",
157157
);
158158

159-
assert_eq!(
160-
main_thread_id,
161-
std::thread::current().id(),
162-
"attempted to access binding from different thread than main thread; this is UB - use the \"experimental-threads\" feature."
163-
);
159+
if main_thread_id != std::thread::current().id() {
160+
// If a binding is accessed the first time, this will panic and start unwinding. It can then happen that during unwinding,
161+
// another FFI call happens (e.g. Godot destructor), which would cause immediate abort, swallowing the error message.
162+
// Thus check if a panic is already in progress.
163+
164+
if std::thread::panicking() {
165+
eprintln!(
166+
"ERROR: Attempted to access binding from different thread than main thread; this is UB.\n\
167+
Cannot panic because panic unwind is already in progress. Please check surrounding messages to fix the bug."
168+
);
169+
} else {
170+
panic!(
171+
"attempted to access binding from different thread than main thread; \
172+
this is UB - use the \"experimental-threads\" feature."
173+
)
174+
};
175+
}
164176
}
165177

166178
// SAFETY: This function can only be called when the binding is initialized and from the main thread, so we know that it's initialized.

0 commit comments

Comments
 (0)