mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -05:00
fix(napi): Don't run microtasks in napi_resolve_deferred (#25246)
Fixes an incredibly obscure bug that causes parcel's file watcher to not get any file update notifications on macOS. The issue was that the native addon was calling `napi_resolve_deferred`, but when we resolved the promise, v8 was running microtasks automatically. That executed JS, which called back into the native addon and broke the addon's assumption that the call wouldn't be reentrant.
This commit is contained in:
parent
6ccaebcdea
commit
37501aa323
1 changed files with 17 additions and 6 deletions
|
@ -3307,19 +3307,30 @@ fn napi_resolve_deferred(
|
|||
check_arg!(env, result);
|
||||
check_arg!(env, deferred);
|
||||
|
||||
// Make sure microtasks don't run and call back into JS
|
||||
env
|
||||
.scope()
|
||||
.set_microtasks_policy(v8::MicrotasksPolicy::Explicit);
|
||||
|
||||
let deferred_ptr =
|
||||
unsafe { NonNull::new_unchecked(deferred as *mut v8::PromiseResolver) };
|
||||
let global = unsafe { v8::Global::from_raw(env.isolate(), deferred_ptr) };
|
||||
let resolver = v8::Local::new(&mut env.scope(), global);
|
||||
|
||||
if !resolver
|
||||
let success = resolver
|
||||
.resolve(&mut env.scope(), result.unwrap())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return napi_generic_failure;
|
||||
}
|
||||
.unwrap_or(false);
|
||||
|
||||
napi_ok
|
||||
// Restore policy
|
||||
env
|
||||
.scope()
|
||||
.set_microtasks_policy(v8::MicrotasksPolicy::Auto);
|
||||
|
||||
if success {
|
||||
napi_ok
|
||||
} else {
|
||||
napi_generic_failure
|
||||
}
|
||||
}
|
||||
|
||||
#[napi_sym]
|
||||
|
|
Loading…
Reference in a new issue