Pitfalls: Rust & FFI
Traps and patterns for Rust, GDExtension (gdext), FFI boundaries, concurrency, and persistent storage.
GDExtension Boundary & gdext
- Parameter type marshaling: In
gdext, onlyGString,bool, andi64are reliably marshaled as direct input parameters in#[func]methods. PassingArray<Variant>,Dictionary, or bareVariantcan silently fail (the GDScript call succeeds while the Rust method body never executes). Serialize complex structured payloads to JSON strings and deserialize withserde_json. - Typed arrays across FFI: Passing a GDScript typed array like
Array[Dictionary]to a RustArray<Variant>parameter fails at runtime with type mismatch errors. Keep arrays untyped across the boundary. - Renamed exports break at runtime: Renaming an exported
#[func]is syntactically valid in GDScript but fails at runtime when the method is invoked.scripts/check-ffi-surfacecross-checks all calls against the exported GDExtension surface. - Library pinning on exit: Unloading the GDExtension shared library (
dlcloseorFreeLibrary) while background tokio worker or PTY reader threads are still running causes immediate segmentation faults.pin_library()pins the mapping usingRTLD_NODELETEon Unix andGetModuleHandleExWon Windows. - Rebuilding maps and SIGBUS: Overwriting a mapped
.soin place withcpcauses running processes to crash withSIGBUS(“Misaligned address error”).scripts/buildinstalls extension libraries by staging to a.newtemporary file and performing an atomic rename over the target path.
Concurrency, Threads & Tokio
- SceneTree is strictly single-threaded: Never invoke Godot engine methods, emit signals, or mutate nodes from background tokio tasks or worker threads. Queue updates in bounded thread-safe channels for GDScript to poll on
_process(), or invokecall_deferred(). - Short-lived TermGrid locks:
TermGridis shared across threads viaArc<Mutex<TermGrid>>. Acquire the lock briefly to copy cell damage or push input, and release it immediately. Holding the lock during blocking I/O freezes the rendering thread. std::sync::Mutexand poisoning: Usestd::sync::Mutexthroughout. A poisoned lock indicates a thread panic; log and report viagpty_core::lock::lock_or_warn(mutex, "what")rather than using bare unwraps or silent ignores.tokio::pin!+reset()for timeouts: Re-arming timeouts inside capture loops should usetokio::pin!(sleep)andsleep.as_mut().reset(deadline)rather than recreating timer instances in everyselect!iteration.tokio::select!on closed channels: When anmpsc::ReceiverreturnsNone,tokio::select!disables that branch but continues spinning on remaining branches, pegging CPU at 100%. Bind the receiver explicitly and break when the channel closes.- Task handles detach on drop: Dropping a
tokio::task::JoinHandledoes not abort the task; it continues running in the background. Explicitly callhandle.abort()inDropimplementations when child tasks must terminate. - CLI single-thread runtime: The CLI binary runs on
#[tokio::main(flavor = "current_thread")]. CLI commands execute single requests and exit immediately; multi-threaded worker pools add process startup overhead and introduce teardown exit races.
Storage, SQLite & Persistence
- Process-wide write serialization: Multiple SQLite writer threads across panes competing for the same database file cause busy timeouts and dropped batches.
store::write_guardprovides process-wide write serialization. - Damaged database recovery: A corrupted database file that begins with SQLite page bytes but lacks a valid schema causes SQLite recovery to fail.
store::open_with_recoverydetects corrupt databases, moves them to<path>.corrupt, and initializes fresh, clean stores. - Forward-compatible store readers: Readers for JSON configuration stores (
plugins.json, etc.) must ignore unknown sibling keys rather than failing as corrupt. Closed validation rules belong on manifest checkers and writers, not on store deserializers. - Owner-only file permissions: Stores containing terminal scrollback, settings, or encryption secrets must be created and repaired to 0700 for directories and 0600 for files using
gpty_core::owner_only.