electron icon indicating copy to clipboard operation
electron copied to clipboard

[Bug]: Using ThreadSafeFunction in child window causes assertion violation

Open martinzak-zaber opened this issue 3 years ago • 11 comments

Preflight Checklist

Electron Version

18.0.0

What operating system are you using?

Windows

Operating System Version

Windows 10 Pro 19044.1645

What arch are you using?

x64

Last Known Working Electron version

16.2.3

Expected Behavior

Assuming the N-API module is correctly implemented in context-aware matter the render process does not crash.

Actual Behavior

Crash log observed in the console:

C:\Windows\system32\cmd.exe [39088]: ../../third_party/electron_node/src/api/callback.cc:68: Assertion `(Environment::GetCurrent(isolate)) == (env)' failed.
 1: 00007FF7DD474AA6 node::Buffer::New+50438
 2: 00007FF7DD4747E9 node::Buffer::New+49737
 3: 00007FF7DD416A01 node::CallbackScope::CallbackScope+593
 4: 00007FF7DD4167ED node::CallbackScope::CallbackScope+61
 5: 00007FF7DE02CDB9 node::AsyncResource::CallbackScope::CallbackScope+89
 6: 00007FF7DD9D5DDC node_api_get_module_file_name+4092
 7: 00007FF7DD48802C uv_run+492
 8: 00007FF7DBB8DFB8 node::FreeArrayBufferAllocator+23608
 9: 00007FF7DEFA11E8 cppgc::internal::WriteBarrier::DijkstraMarkingBarrierRangeSlow+3187976
10: 00007FF7DF06A492 Cr_z_crc32+629122
11: 00007FF7DD4A30E2 uv_sleep+93074
12: 00007FF7DD4ACBEA uv_sleep+132762
13: 00007FF7DCEE2C45 uv_timer_get_repeat+6778117
14: 00007FF7DD34F202 IsSandboxedProcess+3875010
15: 00007FF7DBC87ECC v8::metrics::LongTaskStats::LongTaskStats+496028
16: 00007FF7DBC889AD v8::metrics::LongTaskStats::LongTaskStats+498813
17: 00007FF7DBC84F3C v8::metrics::LongTaskStats::LongTaskStats+483852
18: 00007FF7DBC852B0 v8::metrics::LongTaskStats::LongTaskStats+484736
19: 00007FF7DBA27F1D std::__1::__vector_base<v8::CpuProfileDeoptFrame,std::__1::allocator<v8::CpuProfileDeoptFrame>>::__vector_base<v8::CpuProfileDeoptFrame,std::__1::allocator<v8::CpuProfileDeoptFrame> >+46541
20: 00007FF7DF3BD8A2 Cr_z_crc32+4115858
21: 00007FF96F727034 BaseThreadInitThunk+20
22: 00007FF96F8C2651 RtlUserThreadStart+33

Testcase Gist URL

No response

Additional Information

I have created a separate reproduction repo with N-API module and a small electron application: https://github.com/martinzak-zaber/electron-crash-repro

Preconditions

  webPreferences: {
    nodeIntegration: true,
    contextIsolation: false,
  },

Basic Repro

The crash can only be observed in a child window that is opened using window.open function on the same domain and loads the same native module. The app crashes after a native module calls BlockingCall on ThreadSafeFunction created using child's Napi::Env. From all my understanding the native module is context-aware and works properly with Napi::Env. Sync calls do not seem to be affected.

Workaround

We have come up with two workarounds. The first is to set nativeWindowOpen to false. This is only possible on electron < 18. The second is to return deny in setWindowOpenHandler and still open the window manually using the provided URL. This works universally.

Notes

The crash does not occur if all the native work is done in preload script (as is perhaps for a lot of applications). The preload script is run only once in that scenario.

I am not 100% confident that this is a bug. I don't have a deep understanding of how v8 isolate and node_env should work in parent-child windows. Perhaps the native module is not correctly implemented.

You may wonder why we use nodeIntegration and contextIsolation. It's because our application is strictly desktop and only loads local assets. Using our native node.js library directly makes our development rather convenient.

Thank you, the electron team, and all the contributors for maintaining the amazing ecosystem.

martinzak-zaber avatar Apr 21 '22 00:04 martinzak-zaber

Any news on this @codebytere ? We are facing the same issue when using some native modules in native popup windows.

gpetrov avatar Aug 24 '22 17:08 gpetrov

Disabling the native windows doesn't help with us. Running on Windows 10 with Electron 18.3.9 The crash we got is:

../../third_party/electron_node/src/api/callback.cc:68: Assertion `(Environment::GetCurrent(isolate)) == (env)' failed.
 1: 00007FF78C0753E6 node::Buffer::New+50438
 2: 00007FF78C075129 node::Buffer::New+49737
 3: 00007FF78C017341 node::CallbackScope::CallbackScope+593
 4: 00007FF78C01712D node::CallbackScope::CallbackScope+61
 5: 00007FF78CC2D999 node::AsyncResource::CallbackScope::CallbackScope+89
 6: 00007FF78C5DBBA9 node_api_get_module_file_name+1993
 7: 00007FF78C71194F uv_signal_start_oneshot+1407
 8: 00007FF78C08897C uv_run+492
 9: 00007FF78A77F788 node::FreeArrayBufferAllocator+23624
10: 00007FF78DBA0E88 cppgc::internal::WriteBarrier::DijkstraMarkingBarrierRangeSlow+3156648
11: 00007FF78DC6A182 Cr_z_crc32+629202
12: 00007FF78C0A03E2 uv_sleep+79170
13: 00007FF78C0A9EEA uv_sleep+118858
14: 00007FF78BADCE15 uv_fs_get_result+6787749
15: 00007FF78BF4A482 IsSandboxedProcess+3878434
16: 00007FF78A87985C v8::metrics::LongTaskStats::LongTaskStats+496028
17: 00007FF78A87A33D v8::metrics::LongTaskStats::LongTaskStats+498813
18: 00007FF78A8768CC v8::metrics::LongTaskStats::LongTaskStats+483852
19: 00007FF78A876C40 v8::metrics::LongTaskStats::LongTaskStats+484736
20: 00007FF78A61832D std::__1::__vector_base<v8::CpuProfileDeoptFrame,std::__1::allocator<v8::CpuProfileDeoptFrame> >::__vector_base<v8::CpuProfileDeoptFrame,std::__1::allocator<v8::CpuProfileDeoptFrame> >+47581
21: 00007FF78DFBD122 Cr_z_crc32+4114802
22: 00007FFC1E2E7034 BaseThreadInitThunk+20
23: 00007FFC1E7A2651 RtlUserThreadStart+33
crash reason: Wappler process crashed.
Renderer process crashed - see https://www.electronjs.org/docs/tutorial/application-debugging for potential debugging information.

gpetrov avatar Aug 24 '22 18:08 gpetrov

Disabling the native windows doesn't help with us. Running on Windows 10 with Electron 18.3.9

The nativeWindowOpen workaround only works for Electron < 18 (see description). The denying of the window open is what we currently use on Electron 18.

    window.webContents.setWindowOpenHandler(details => {
      // This is a hack that avoid windows sharing a JS context (see window.opener).
      // We used to rely on nativeWindowOpen property that was removed from electron.
      // We had made changes to support multiple contexts but some electron assertion fails.
      // See https://github.com/electron/electron/issues/33868 .
      this.createWindow({ url: details.url });
      return ({ action: 'deny' });
    });

martinzak-zaber avatar Aug 24 '22 23:08 martinzak-zaber

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

github-actions[bot] avatar Nov 23 '22 02:11 github-actions[bot]

bump. not stale.

nekename avatar Jan 07 '23 14:01 nekename

It looks like this bug has been biting VSCode for some time now (Windows 7, 10 and Linux). My report is here https://github.com/microsoft/vscode/issues/130320#issuecomment-1207158020 and still seeing it with latest VSCode 1.75.0

 1: 0000000140CDC8F6 node::Buffer::New+50054
 2: 0000000140CDC639 node::Buffer::New+49353
 3: 0000000140CBFE12 node::EmitAsyncDestroy+248898
 4: 0000000141320707 uv_dlerror+646871
 5: 00000001422C484E v8::EmbedderStateScope::~EmbedderStateScope+104286
 6: 0000000142790C3C cppgc::internal::WriteBarrier::DijkstraMarkingBarrierRangeSlow+2094508
 7: 00000000C00CEB32

image

lorand-horvath avatar Feb 03 '23 14:02 lorand-horvath

I can confirm this is still happening in Electron 19. It also happens on MacOS. I am running electron with golden layout and a native module that's supposedly context-aware. Whenever a thread-safe function is called in a new window it crashes: ../../third_party/electron_node/src/api/callback.cc:74:node::InternalCallbackScope::InternalCallbackScope(node::Environment *, Local<v8::Object>, const node::async_context &, int): Assertion (Environment::GetCurrent(isolate)) == (env) failed.

I have this on my setup.ts

  private _setupSecurityAndPermissions(): void {
    this._mainWindow.webContents.setWindowOpenHandler(details => {
      if (
        !details.url.includes(this._customScheme) &&
        !details.url.startsWith('http://localhost')
      ) {
        return { action: 'deny' }
      }

      return {
        action: 'allow',
        overrideBrowserWindowOptions: {
          frame: true,
          parent: this._mainWindow,
          alwaysOnTop: true,
          minimizable: false,
          webPreferences: {
            contextIsolation: false,
            sandbox: false,
            nodeIntegration: true,
            nodeIntegrationInWorker: true,
            preload: Path.join(Electron.app.getAppPath(), 'build', 'src', 'preload.js')
          }
        }
      }
    })

    Electron.session.defaultSession.setPermissionRequestHandler(
      (webContents, permission, callback, details) => {
        callback(true)
      }
    )
  }

@gpetrov @ninjadev64 Were you able to fix this?

david-metrica avatar May 01 '23 16:05 david-metrica

this seems to be related to the parent window passed on overrideBrowserWindowOptions @codebytere

When specified a crash occurs on MacOS, like:

``` 2023-06-11 19:25:23.669 Electron[55553:6300949] -[NSTaggedPointerString parentWindow]: unrecognized selector sent to instance 0xb840f1c0f85f83ae 2023-06-11 19:25:23.671 Electron[55553:6300949] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString parentWindow]: unrecognized selector sent to instance 0xb840f1c0f85f83ae' *** First throw call stack: ( 0 CoreFoundation 0x00000001ad11c3e8 __exceptionPreprocess + 176 1 libobjc.A.dylib 0x00000001acc66ea8 objc_exception_throw + 60 2 CoreFoundation 0x00000001ad1bec0c -[NSObject(NSObject) __retain_OA] + 0 3 CoreFoundation 0x00000001ad082660 ___forwarding___ + 1600 4 CoreFoundation 0x00000001ad081f60 _CF_forwarding_prep_0 + 96 5 Electron Framework 0x000000010a979870 _ZN4node24FreeArrayBufferAllocatorEPNS_20ArrayBufferAllocatorE + 267920 6 Electron Framework 0x000000010a978f10 _ZN4node24FreeArrayBufferAllocatorEPNS_20ArrayBufferAllocatorE + 265520 7 Electron Framework 0x000000010a8c547c _ZN2v89CodeEvent10GetCommentEv + 4068 8 Electron Framework 0x000000010a7f96d4 _ZNK2v88internal20CodeCommentsIterator4sizeEv + 56264 9 Electron Framework 0x000000010a7fa9f8 _ZNK2v88internal20CodeCommentsIterator4sizeEv + 61164 10 Electron Framework 0x000000010a7fb0f0 _ZNK2v88internal20CodeCommentsIterator4sizeEv + 62948 11 Electron Framework 0x000000010a7ef9c8 _ZNK2v88internal20CodeCommentsIterator4sizeEv + 16060 12 Electron Framework 0x000000010a7ef8c4 _ZNK2v88internal20CodeCommentsIterator4sizeEv + 15800 13 Electron Framework 0x000000010b709fbc _ZN2v88internal9Accessors12MakeAccessorEPNS0_7IsolateENS0_6HandleINS0_4NameEEEPFvNS_5LocalINS_4NameEEERKNS_20PropertyCallbackInfoINS_5ValueEEEEPFvS9_NS7_ISB_EERKNSA_INS_7BooleanEEEE + 15500 14 Electron Framework 0x000000010b709b90 _ZN2v88internal9Accessors12MakeAccessorEPNS0_7IsolateENS0_6HandleINS0_4NameEEEPFvNS_5LocalINS_4NameEEERKNS_20PropertyCallbackInfoINS_5ValueEEEEPFvS9_NS7_ISB_EERKNSA_INS_7BooleanEEEE + 14432 15 Electron Framework 0x000000010b70907c _ZN2v88internal9Accessors12MakeAccessorEPNS0_7IsolateENS0_6HandleINS0_4NameEEEPFvNS_5LocalINS_4NameEEERKNS_20PropertyCallbackInfoINS_5ValueEEEEPFvS9_NS7_ISB_EERKNSA_INS_7BooleanEEEE + 11596 16 ??? 0x0000000157edb518 0x0 + 5770163480 17 ??? 0x0000000157e49f08 0x0 + 5769568008 18 ??? 0x0000000157f98d38 0x0 + 5770939704 19 ??? 0x0000000157e4ccfc 0x0 + 5769579772 20 ??? 0x0000000157e4ccfc 0x0 + 5769579772 21 ??? 0x00000001503ec984 0x0 + 5641259396 22 ??? 0x00000001503b6ce8 0x0 + 5641039080 23 ??? 0x00000001504cbcec 0x0 + 5642173676 24 ??? 0x0000000157e4ab48 0x0 + 5769571144 25 ??? 0x0000000157e4a838 0x0 + 5769570360 26 Electron Framework 0x000000010b7c5014 _ZN2v88internal9Execution4CallEPNS0_7IsolateENS0_6HandleINS0_6ObjectEEES6_iPS6_ + 452 27 Electron Framework 0x000000010b6b8dcc _ZN2v88Function4CallENS_5LocalINS_7ContextEEENS1_INS_5ValueEEEiPS5_ + 516 28 Electron Framework 0x0000000110a78c8c _ZN4node13CallbackScopeD1Ev + 1744 29 Electron Framework 0x0000000110a78ff4 _ZN4node12MakeCallbackEPN2v87IsolateENS0_5LocalINS0_6ObjectEEENS3_INS0_8FunctionEEEiPNS3_INS0_5ValueEEENS_13async_contextE + 256 30 Electron Framework 0x000000010a937174 _ZN2v89CodeEvent13GetScriptNameEv + 189132 31 Electron Framework 0x000000010a8793e4 _ZN2v815ValueSerializer8Delegate23GetWasmModuleTransferIdEPNS_7IsolateENS_5LocalINS_16WasmModuleObjectEEE + 103540 32 Electron Framework 0x000000010a86039c _ZN2v815ValueSerializer8Delegate23GetWasmModuleTransferIdEPNS_7IsolateENS_5LocalINS_16WasmModuleObjectEEE + 1068 33 Electron Framework 0x000000010a86008c _ZN2v815ValueSerializer8Delegate23GetWasmModuleTransferIdEPNS_7IsolateENS_5LocalINS_16WasmModuleObjectEEE + 284 34 Electron Framework 0x000000010cc749f4 _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 8792828 35 Electron Framework 0x000000010cb2eff4 _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 7459068 36 Electron Framework 0x000000010b4e0b94 _ZN4node25GetEnvironmentIsolateDataEPNS_11EnvironmentE + 477436 37 Electron Framework 0x000000010cb4d324 _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 7582764 38 Electron Framework 0x000000010d79c770 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 11692968 39 Electron Framework 0x000000010d7a10d4 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 11711756 40 Electron Framework 0x000000010d79e580 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 11700664 41 Electron Framework 0x000000010da83058 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 14734480 42 Electron Framework 0x000000010d4b71c4 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 8656380 43 Electron Framework 0x000000010d4d14f0 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 8763688 44 Electron Framework 0x000000010d51f1b0 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 9082344 45 Electron Framework 0x000000010a73c010 Electron Framework + 1720336 46 Electron Framework 0x000000010d51e8f8 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 9080112 47 CoreFoundation 0x00000001ad0a1a08 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 48 CoreFoundation 0x00000001ad0a199c __CFRunLoopDoSource0 + 176 49 CoreFoundation 0x00000001ad0a170c __CFRunLoopDoSources0 + 244 50 CoreFoundation 0x00000001ad0a0310 __CFRunLoopRun + 836 51 CoreFoundation 0x00000001ad09f878 CFRunLoopRunSpecific + 612 52 HIToolbox 0x00000001b677ffa0 RunCurrentEventLoopInMode + 292 53 HIToolbox 0x00000001b677fde4 ReceiveNextEventCommon + 672 54 HIToolbox 0x00000001b677fb2c _BlockUntilNextEventMatchingListInModeWithFilter + 72 55 AppKit 0x00000001b0325838 _DPSNextEvent + 632 56 AppKit 0x00000001b03249c8 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 728 57 AppKit 0x00000001b0318df8 -[NSApplication run] + 464 58 Electron Framework 0x000000010d51fd80 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 9085368 59 Electron Framework 0x000000010d51e478 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 9078960 60 Electron Framework 0x000000010d4d2050 _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 8766600 61 Electron Framework 0x000000010d49c4dc _ZN4node23GetMultiIsolatePlatformEPNS_11IsolateDataE + 8546580 62 Electron Framework 0x000000010c78dfd0 _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 3653848 63 Electron Framework 0x000000010c78f8e4 _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 3660268 64 Electron Framework 0x000000010c78bc5c _ZN2v88internal20SetupIsolateDelegate13SetupBuiltinsEPNS0_7IsolateEb + 3644772 65 Electron Framework 0x000000010aa99608 _ZN2v88internal8compiler10BasicBlock15set_loop_headerEPS2_ + 13524 66 Electron Framework 0x000000010aa9a514 _ZN2v88internal8compiler10BasicBlock15set_loop_headerEPS2_ + 17376 67 Electron Framework 0x000000010aa9a364 _ZN2v88internal8compiler10BasicBlock15set_loop_headerEPS2_ + 16944 68 Electron Framework 0x000000010aa98e28 _ZN2v88internal8compiler10BasicBlock15set_loop_headerEPS2_ + 11508 69 Electron Framework 0x000000010aa99054 _ZN2v88internal8compiler10BasicBlock15set_loop_headerEPS2_ + 12064 70 Electron Framework 0x000000010a7c91d8 ElectronMain + 128 71 dyld 0x00000001acc97e50 start + 2544 ) ```

When parent is omitted that no crash occurs.

gpetrov avatar Jun 11 '23 19:06 gpetrov

I'm having a very similar issue with an iframe

 webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            nodeIntegrationInSubFrames: true,
        },

the error I get is the same : electron_node\src\api\callback.cc:74: Assertion '(Environment::GetCurrent(isolate)) == (env)' failed.

is there a workaround?

alissawix avatar Dec 16 '23 15:12 alissawix

I'm having a very similar issue with an iframe

 webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            nodeIntegrationInSubFrames: true,
        },

the error I get is the same : electron_node\src\api\callback.cc:74: Assertion '(Environment::GetCurrent(isolate)) == (env)' failed.

is there a workaround?

Same issue for my app.

LittoCats avatar Jan 10 '24 02:01 LittoCats

Hi @codebytere

  • Is the below crash related to the same root cause?
  • I got that crash when closing the child window that is created via window.open and the overrideBrowserWindowOptions has these values contextIsolation: false and nodeIntegration: false, on electron v28.2.5 and that Child window doesn't use any native modules!!
  • FYI the main browser window has this webPreferences: {contextIsolation: false,nodeIntegration: false,sandbox: false,
C:\Windows\system32\cmd.exe [32400]: ..\..\third_party\electron_node\src\api\callback.cc:74: Assertion `(Environment::GetCurrent(isolate)) == (env)' failed.
 1: 00007FF6BDB78506 node::SetTracingController+86134
 2: 00007FF6BDB780DC node::SetTracingController+85068
 3: 00007FF6BDBF6086 node::CallbackScope::CallbackScope+502     
 4: 00007FF6BDBF5ECD node::CallbackScope::CallbackScope+61      
 5: 00007FF6BEA7DBE4 node::AsyncResource::CallbackScope::CallbackScope+84
 6: 00007FF6BE349CC4 node_api_get_module_file_name+3716
 7: 00007FF6BDBFAE88 uv_run+792
 8: 00007FF6BDBC741C node::loader::ModuleWrap::Initialize+164380
 9: 00007FF6BDBC7D42 node::loader::ModuleWrap::Initialize+166722
10: 00007FF6BDBF340A node::FreeEnvironment+186
11: 00007FF6BBD2FE95 v8::Extension::auto_enable+981
12: 00007FF6BBD4FB29 v8::Extension::auto_enable+131177
13: 00007FF6BDA4572F uv_fs_get_system_error+1556319
14: 00007FF6BECDB607 uv_version_string+2480503
15: 00007FF6BE68A1AD uv_stream_set_blocking+3246445
16: 00007FF6BDFBA699 GetHandleVerifier+3731625
17: 00007FF6BDFDD80C GetHandleVerifier+3875356
18: 00007FF6BE039F9B GetHandleVerifier+4254123
19: 00007FF6BFD6180B cppgc::internal::WriteBarrier::DijkstraMarkingBarrierRangeSlow+4512667
20: 00007FF6BFE27757 Cr_z_adler32+623271
21: 00007FF6BFE2FD03 Cr_z_adler32+657491
22: 00007FF6BDC3A5A9 GetHandleVerifier+61369
23: 00007FF6BD55FC48 uv_os_getpid+181320
24: 00007FF6BDA312E0 uv_fs_get_system_error+1473296
25: 00007FF6BBE3D862 v8::internal::StrongRootAllocatorBase::StrongRootAllocatorBase+542194
26: 00007FF6BBE3E6D9 v8::internal::StrongRootAllocatorBase::StrongRootAllocatorBase+545897
27: 00007FF6BBE3A9E0 v8::internal::StrongRootAllocatorBase::StrongRootAllocatorBase+530288
28: 00007FF6BBE3AB5D v8::internal::StrongRootAllocatorBase::StrongRootAllocatorBase+530669
29: 00007FF6BBB7CBC5 std::__Cr::vector<v8::CpuProfileDeoptInfo,std::__Cr::allocator<v8::CpuProfileDeoptInfo> >::vector<v8::CpuProfileDeoptInfo,std::__Cr::allocator<v8::CpuProfileDeoptInfo> >+60933
30: 00007FF6C0218642 Cr_z_adler32+4755858
31: 00007FFFCB72257D BaseThreadInitThunk+29
32: 00007FFFCC6CAA58 RtlUserThreadStart+40

oalfroukh avatar Mar 26 '24 12:03 oalfroukh