Repository navigation
fix(ffi/objc): enter a Node callback scope for native callbacks from a host-owned run loop - #102
NathanWalker wants to merge 2 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
|
The approach is right. In Node/Electron builds we can't reach the isolate or context like 1. The return value can be left uninitialized. 2. Don't silently drop the exception. 3. Skip the new path when JS is already on the stack.
This also keeps microtask timing and async context unchanged for nested callbacks, so the behavior change only applies to callbacks that arrive from a bare run loop, where it's actually needed. 4. Keep it next to the existing callback scope code. 5. Fix the description. 6. Run it once on Deno and Bun. Smaller, not blocking:
|
92db5c9 to
c6a2fbc
Compare
|
Thanks for the review. All points are addressed in c6a2fbc (amended and force-pushed):
|
4f87f05 to
9633aba
Compare
…a host-owned run loop In the Node-API build, Objective-C → JS callbacks (method overrides, target/action, delegates, blocks, C function pointers) assumed JS was already on the stack. That holds under Node, where the app pumps the Cocoa run loop from JS, but not in hosts that own the native run loop, such as Electron's main process: Cocoa re-enters JS with no V8 context entered and no HandleScope open, and the process aborts with "Cannot create a handle without a HandleScope". Route same-thread callbacks through a native trampoline invoked with napi_make_callback, which enters the env's context and opens a Node callback scope (draining nextTicks and microtasks when the outermost scope closes). The trampoline and async context are created at init, while JS is on the stack. Objective-C exceptions raised inside the callback are caught and rethrown after napi_make_callback returns, so they never unwind through V8 frames. ENABLE_JS_RUNTIME builds are unchanged.
…cope - Same-thread callbacks call JS directly in a handle scope while a JS→native call is in progress on the thread (tracked by NativeCallRuntimeUnlockScope); only callbacks with no JS on the stack go through napi_make_callback. - Zero the native return value when the trampoline does not run. - Report a JS error left pending by a callback through the closure uncaught-error handling, inside the callback scope. - The helper is the Node-API branch of NativeCallbackScope and takes a function pointer plus data instead of a std::function.
c6a2fbc to
e6ea813
Compare
Summary
Hosts that own the native run loop (Electron's main process, for one) crash the Node-API runtime on the first Objective-C → JS callback. Under Node, JS pumps the Cocoa run loop, so a V8 context and HandleScope are always on the stack. Under Electron, Cocoa re-enters JS with neither, and V8 aborts:
Same-thread callbacks now enter JS through
NativeCallbackScope::run, the Node-API branch of the existing callback scope inCallbackThreading.h:NativeCallRuntimeUnlockScope, which already wraps JS→native calls, keeps a thread-local depth counter in Node-API builds. A call site that doesn't use it only takes the slower path, which is still correct.napi_async_contextare created innativescript_initwhile JS is on the stack, and released with an env cleanup hook.napi_async_init,napi_make_callback, etc. are declared locally because this tree vendorsjs_native_api.hbut notnode_api.h.ENABLE_JS_RUNTIMEbuilds compile exactly as before. Every change is under#ifndef ENABLE_JS_RUNTIME.AsyncLocalStorageinside a delegate callback sees init-time values.Evidence
All on macOS 26.6.2 (arm64),
--macos --no-enginebuild. Each check exercises one native→JS path:Fatal error in V8: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScopenapi_make_callback/napi_async_initon Deno and Bun, the same checks were also run on a throwaway build with the depth check disabled: 6/6 on all four hosts.MACOS_TEST_FFI_BACKEND=napi npm run test:macos(engine build with this bridge,ENABLE_JS_RUNTIME): 714 tests, 0 failures, 17 skipped.npm run test:macos(V8 bridge): 714 tests, 0 failures, 10 skipped.A larger proof of concept also ran on this build: Electron + NativeScript on macOS, with NSToolbar delegates, NSGlassEffectView mouse overrides, MKMapViewDelegate, MKLocalSearch completion blocks, NSColorPanel target/action, NSPopover/NSDatePicker and AVSpeechSynthesizer, all inside a BrowserWindow.
Merge Danger
Door: two-way
Isolated to
ffi/objc/napi; a revert restores the previous behavior exactly.Blast Radius: Node-API builds
napi_make_callback: they drain microtasks when they return, instead of on the next tick.Not in this PR, but found along the way:
build_nativescript.shhashes./metadata-generator/src …whilebuild_metadata_generator.shwrites the hash fromsrc …, so the generator rebuilds on every build.npm run metagen macostargets macOS 11, which drops declarations introduced later (e.g.NSGlassEffectView).