From 2bf1525fe8b0609ed17ab62fb7e174e98e4a03df Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Mon, 31 Oct 2022 13:52:51 -0700 Subject: [PATCH] Fix panic when defining function during shutdown This fixes a race condition that can happen when the VM is shutting down where our call to `napi::create_function` succeeds but then our subsequent call to `napi::add_finalizer` fails, causing a panic. We need to let this specific failure mode slide to avoid crashing. --- crates/neon/src/sys/fun.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/neon/src/sys/fun.rs b/crates/neon/src/sys/fun.rs index 18b72ba5a..e8fb68d06 100644 --- a/crates/neon/src/sys/fun.rs +++ b/crates/neon/src/sys/fun.rs @@ -54,7 +54,13 @@ where // If adding the finalizer fails the closure will leak, but it would // be unsafe to drop it because there's no guarantee V8 won't use the // pointer. - assert_eq!(status, napi::Status::Ok); + // + // As a special case, we also allow PendingException, because it means + // we're shutting down the VM and don't need to worry about cleanup. + assert!(matches!( + status, + napi::Status::Ok | napi::Status::PendingException + )); } Ok(out)