electron: Using worker_threads that keep running indefinitely fails assertion on SIGTERM
Issue Details
- Electron Version:
- v9.0.0-beta.21 and 8.2.4
- Operating System:
- macOS 10.15.4
Actual Behavior
An electron app that uses a worker thread that runs indefinitely in the main process (because of a server listening on a port etc.) throws the following error on exit:
/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron[93383]: ../../third_party/electron_node/src/node_worker.cc:358:virtual node::worker::Worker::~Worker(): Assertion `stopped_' failed.
1: 0x10f4ce9b5 node::Buffer::New(v8::Isolate*, char*, unsigned long, void (*)(char*, void*), void*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
2: 0x10f4ce73f node::Buffer::New(v8::Isolate*, char*, unsigned long, void (*)(char*, void*), void*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
3: 0x10f54eb01 node::MultiIsolatePlatform::CancelPendingDelayedTasks(v8::Isolate*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
4: 0x10f54eb3e node::MultiIsolatePlatform::CancelPendingDelayedTasks(v8::Isolate*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
5: 0x10f47f56f node::EmitAsyncDestroy(node::Environment*, node::async_context) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
6: 0x10f44cc4e node::FreeEnvironment(node::Environment*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
7: 0x1095c4c81 ElectronInitializeICUandStartNode [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
8: 0x10b0fac71 v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
9: 0x10b0fc514 v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
10: 0x10b0f8294 v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
11: 0x10afa1616 v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
12: 0x10afa126b v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
13: 0x10d522488 v8::internal::SetupIsolateDelegate::SetupHeap(v8::internal::Heap*) [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
14: 0x10a2222b4 ElectronInitializeICUandStartNode [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
15: 0x1094f8a54 ElectronMain [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
16: 0x108377110 [/Users/kishan/Downloads/electron-test/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron]
17: 0x7fff6cb41cc9 start [/usr/lib/system/libdyld.dylib]
Expected Behavior
It shouldn’t throw the error.
To Reproduce
Couldn’t repro this in Electron Fiddle but the following should work:
- Copy the following to
worker-exit-issue.js
const { Worker, isMainThread } = require('worker_threads')
if (isMainThread) {
new Worker(__filename)
console.log('main thread')
} else {
console.log('worker')
const http = require('http')
const server = http.createServer((req, res) => {
res.end()
})
server.listen(12345)
}
-
Run
electron worker-exit-issue.js -
Quit the Electron app that runs from Dock or equivalent. (CTRL+C/SIGINT will not throw the error)
-
You’ll see the above error printed in the console:

This doesn’t happen if I run node worker-exit-issue.js
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 28 (15 by maintainers)
Commits related to this issue
- fix: try to prevent https://github.com/electron/electron/issues/23315, but seems not working at all — committed to tiddly-gittly/TidGi-Desktop by linonetwo 4 years ago
- fix: use correct Node.js shutdown order The Environment is created after the Isolate and its platform data, so it also needs to be torn down before it. This fixes the crash mentioned below. (Thanks ... — committed to addaleax/electron by addaleax 3 years ago
I’m this boat as well - while trying to refactor some blocking native logic out of the main thread for performance reasons, I’m now encountering an app crash every time I exit the app because I followed the advice in https://www.electronjs.org/docs/tutorial/performance and used Node.js workers.
From where I’m sitting, it looks to me like there is no way to have multithreaded native logic without encountering this error. Is that accurate?
@KishanBagaria we are working around it by monkey-patching
process.exitto exit allworker_threadsbefore callingprocess.exit. This is a dirty hack because (among other reasons) it makesprocess.exitan asynchronous operation, but I’ve not been able to find a better workaround 😕You will have to audit all usages of
process.exitin your codebase and ensure that they all handle an asynchronousprocess.exitcorrectly. Additionally, your process can no longer exit normally when there are no queued events, you’ll always have to make sure you call the worker exit routine before exiting.Initially, we were calling
.terminate()on eachworker_threadonprocess.exitand waiting for that to finish, but some users were still running into problems withSIGABRT. So we switched to usingpostMessageto tell each thread to kill itself, waiting for that, and then exiting the parent process. This seems to work fine in practice: https://github.com/cypress-io/cypress/pull/7572/filesStill, this is a bad workaround due to the reasons mentioned above. I hope someone from the Electron team can take an in-depth look at this issue.
This issue seems like it’s present in Node.js v12 but not v14 - i can’t repro it after https://github.com/electron/electron/pull/25249 but can in versions prior
Sounds like this is fixed, so I’ll close the issue. Let me know if I’m mistaken and I can reopen.
Correction, the proper fix actually needs to be done in electron: https://github.com/electron/electron/pull/28468 Thanks @addaleax for helping me with this!
@m4heshd of course it can be fixed. Just need to fix node v12 first 😂 (See: https://github.com/nodejs/node/pull/38010)
Having the same issue. Blocked from everywhere. Can’t use web workers because of the need to use native modules and it can’t seem to load any kind of external module. Can’t use worker threads because of this issue. So frustrated not being able to move forward. @flotwig Even if you end all the threads gracefully, Electron will exit with code
-1073741819 (0xC0000005)whenever the process has been ended. An access violation on Windows.I’m fairly certain it’s this commit: https://github.com/nodejs/node/pull/30467/commits/b6738bc62479391e5d93026716ed92d6879ea60e
that would need to be backported to
9-x-ythrough11-x-y@KishanBagaria I believe I’m having the same issue: #23366