Skip to content

Commit

Permalink
Fix race conditions with functions returning "no output"
Browse files Browse the repository at this point in the history
The current implementation of `#[node_bindgen]` wrapping shim over
`async fn` functions special cased those with no return value
to return immediately, for some reason. This meant that if such
an `async fn` had a side-effect that following-up code wanted to observe,
there would be a race condition whereby such code could observe
causally-inconsitent state, leading to logic bugs.
  • Loading branch information
danielhenrymantilla committed Nov 19, 2020
1 parent 1641556 commit b191ff4
Showing 1 changed file with 6 additions and 23 deletions.
29 changes: 6 additions & 23 deletions nj-derive/src/generator/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,12 @@ pub fn generate_rust_invocation(ctx: &FnGeneratorCtx,cb_args: &mut CbArgs) -> To
// if this is async, wrap with JsFuture
let rust_invoke_ft_wrapper = if ctx.is_async() {


if ctx.has_default_output() {

// since this doesn't have any output, we don't need return promise, we just
// spawn async and return null ptr
quote! {

node_bindgen::core::future::spawn(async move {
#rust_invoke.await;
});

Ok(std::ptr::null_mut())
}

} else {

let async_name = format!("{}_ft", ctx.fn_name());
let async_lit = LitStr::new(&async_name, Span::call_site());
quote! {
(node_bindgen::core::JsPromiseFuture::new(
#rust_invoke,#async_lit
)).try_to_js(&js_env)
}
let async_name = format!("{}_ft", ctx.fn_name());
let async_lit = LitStr::new(&async_name, Span::call_site());
quote! {
(node_bindgen::core::JsPromiseFuture::new(
#rust_invoke, #async_lit
)).try_to_js(&js_env)
}


Expand Down

0 comments on commit b191ff4

Please sign in to comment.