slint-ui/slint · error · napi::Error
Failed to create weak reference
Error message
Failed to create weak reference
What it means
The custom weak-reference wrapper in api/node/rust/weak_ref.rs calls the raw N-API napi_create_reference with a 0 (non-owning) refcount. If N-API returns anything other than napi_ok, the constructor throws 'Failed to create weak reference'. This typically indicates a V8/engine-level failure to create the reference rather than a caller error.
Solutions
- Avoid creating weak references during environment teardown/shutdown; defer or skip the reference creation and handle the missing reference gracefully.
- Ensure the value handle and env used to build the reference come from the same, current N-API environment/thread.
- Log the napi_status alongside the error to identify the precise engine failure and address the root cause (e.g. wrong env, GC finalizer context).
Example fix
// before
let weak = WeakRef::new(env, value); // may throw at teardown
// after
let weak = match WeakRef::new(env, value) {
Ok(w) => Some(w),
Err(e) => { log::warn!("weak ref unavailable: {e}"); None }
}; Defensive patterns
Strategy: try-catch
Validate before calling
if (process.env.NODE_ENV !== 'shutdown' && !envIsTearingDown) {
weak = new WeakRef(env, value);
} Try / catch
let weak;
try {
weak = new WeakRef(env, value);
} catch (e) {
console.warn('weak reference unavailable:', e.message);
weak = null;
} Prevention
- Do not create weak references during environment teardown or finalizers.
- Always use the env and value handles from the same thread/isolate.
- Log the underlying napi_status for diagnosis.
- Design the API so a missing weak reference is a recoverable condition.
When it happens
Trigger: Constructing the weak reference wrapper during Node.js shutdown or inside a finalizer where the environment is being torn down; creating a reference for a value in an invalid state at the engine level; mixing environments (using an env/raw value from another thread or isolate).
Common situations: Addons performing cleanup during process exit; multithreaded N-API usage without proper environment/threadsafe-function handling; native module loaded into a shutting-down worker.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- expect Boolean, got
- expect Number, got
- expect String, got
- failed ( )
- Component should cleanly upgrade here
AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16).
Data as JSON: /api/errors/e9fcccb72233bd7f.
Report an issue: GitHub.
Appendix: source
Thrown at api/node/rust/weak_ref.rs:65
///
/// The reference doesn't prevent garbage collection.
/// If V8 collects the value,
/// [`get_unknown`](WeakValueRef::get_unknown) returns `None`.
pub struct WeakValueRef {
raw_ref: napi::sys::napi_ref,
raw_env: napi::sys::napi_env,
}
impl WeakValueRef {
/// Create a weak reference to any JS value.
pub fn new<'a, T: JsValue<'a>>(env: &Env, value: &T) -> Result<Self> {
let raw_env = env.raw();
let raw_val = value.raw();
let mut raw_ref = std::ptr::null_mut();
// Safety: raw_env and raw_val are valid handles from napi-rs.
let status = unsafe { napi::sys::napi_create_reference(raw_env, raw_val, 0, &mut raw_ref) };
if status != napi::sys::Status::napi_ok {
return Err(napi::Error::new(
napi::Status::from(status),
"Failed to create weak reference",
));
}
Ok(Self { raw_ref, raw_env })
}
/// Get the value back,
/// or `None` if it was garbage-collected.
pub fn get_unknown(&self) -> Option<Unknown<'_>> {
let mut value = std::ptr::null_mut();
// Safety: raw_env and raw_ref are valid (created in new, deleted in drop).
let status =
unsafe { napi::sys::napi_get_reference_value(self.raw_env, self.raw_ref, &mut value) };
if status != napi::sys::Status::napi_ok || value.is_null() {
return None;
}
// Safety: value is a live JS handle from the reference.View on GitHub (pinned to bb937076de)