Skip to content
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

fix(neon): Do not panic when dropping a Root if the VM has shutdown #677

Merged
merged 2 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install g++-4.8
run: sudo apt-get install -y g++-4.8
- name: install build-essential
run: sudo apt-get install -y build-essential
- name: run cargo test
run: xvfb-run --auto-servernum cargo test --release -- --nocapture
21 changes: 9 additions & 12 deletions crates/neon-runtime/src/napi/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ unsafe impl Sync for Tsfn {}
/// function for scheduling tasks to execute on a JavaScript thread.
pub struct ThreadsafeFunction<T> {
tsfn: Tsfn,
callback: fn(Env, T),
callback: fn(Option<Env>, T),
}

#[derive(Debug)]
struct Callback<T> {
callback: fn(Env, T),
callback: fn(Option<Env>, T),
data: T,
}

Expand All @@ -66,7 +66,7 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {
/// Safety: `Env` must be valid for the current thread
pub unsafe fn new(
env: Env,
callback: fn(Env, T),
callback: fn(Option<Env>, T),
) -> Self {
Self::with_capacity(env, 0, callback)
}
Expand All @@ -76,7 +76,7 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {
pub unsafe fn with_capacity(
env: Env,
max_queue_size: usize,
callback: fn(Env, T),
callback: fn(Option<Env>, T),
) -> Self {
let mut result = MaybeUninit::uninit();

Expand Down Expand Up @@ -171,20 +171,17 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {
_context: *mut c_void,
data: *mut c_void,
) {
// Event loop may be terminated
if data.is_null() {
return;
}

let Callback {
callback,
data,
} = *Box::from_raw(data as *mut Callback<T>);

// Event loop has terminated
if env.is_null() {
return;
}
let env = if env.is_null() {
None
} else {
Some(env)
};

callback(env, data);
}
Expand Down
12 changes: 11 additions & 1 deletion src/context/internal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "legacy-runtime")]
use std::any::Any;
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use std::mem::MaybeUninit;
#[cfg(feature = "legacy-runtime")]
use std::os::raw::c_void;
Expand Down Expand Up @@ -28,6 +28,11 @@ pub struct Env(raw::Isolate);
#[derive(Clone, Copy)]
pub struct Env(raw::Env);

thread_local! {
#[allow(unused)]
pub(crate) static IS_RUNNING: RefCell<bool> = RefCell::new(false);
}

#[cfg(feature = "legacy-runtime")]
extern "C" fn drop_class_map(map: Box<ClassMap>) {
std::mem::drop(map);
Expand Down Expand Up @@ -227,6 +232,11 @@ pub fn initialize_module(exports: Handle<JsObject>, init: fn(ModuleContext) -> N
#[cfg(feature = "napi-1")]
pub fn initialize_module(env: raw::Env, exports: Handle<JsObject>, init: fn(ModuleContext) -> NeonResult<()>) {
unsafe { neon_runtime::setup(env); }

IS_RUNNING.with(|v| {
*v.borrow_mut() = true;
});

ModuleContext::with(Env(env), exports, |cx| {
let _ = init(cx);
});
Expand Down
10 changes: 8 additions & 2 deletions src/event/event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ impl EventQueue {
}

// Monomorphized trampoline funciton for calling the user provided closure
fn callback(env: Env, callback: Callback) {
callback(env)
fn callback(env: Option<Env>, callback: Callback) {
if let Some(env) = env {
callback(env);
} else {
crate::context::internal::IS_RUNNING.with(|v| {
*v.borrow_mut() = false;
});
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/handle/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ impl<T> Drop for Root<T> {
// panic and instead prefer to leak.
if std::thread::panicking() {
eprintln!("Warning: neon::sync::Root leaked during a panic");
} else {
return;
}

// Only panic if the event loop is still running
if let Ok(true) = crate::context::internal::IS_RUNNING.try_with(|v| *v.borrow()) {
panic!("Must call `into_inner` or `drop` on `Root` \
https://docs.rs/neon/latest/neon/sync/index.html#drop-safety");
}
Expand Down