Comments (4)
Marking this as won't fix. Every time the worker receives an event listener it unrefs the internal handle and rewire it again after listeners are set. It unrefs (at first) for graceful shutdown as documented; that seems to be an intended behavior; consider unref the messagePort
as shown in #53036 (comment) if you need the worker.unref
to unref even after future listeners.
I'll close this issue, but feel free to re open if needed or something.
from node.
w.unref()
w.on('message', () => {})
// ❗️ stuck
Node.js will call w.[kPublicPort].ref()
when listen the message
event. So the process will not exit.
But i think there is a bug in the follows code.
w.on('message', () => {})
w.unref()
w.on('message', () => {})
It looks like the process won't exit but it does.
- call
w.on('message', () => {})
Node.js will run the follows code.
// size means the number of message listener, here is 0
if (size === 0) {
port.ref();
FunctionPrototypeCall(MessagePortPrototype.start, port);
}
- then call
w.unref()
Node.js will runport.ref()
but the number of message listener is still 1. - so when call
w.on('message', () => {})
again, Node.js will run the follows code again.
// size is 1, so do nothing
if (size === 0) {
port.ref();
FunctionPrototypeCall(MessagePortPrototype.start, port);
}
finally the port is unref so the process exits.
from node.
Yes, @theanarkh is right; the problem here is that the parentPort
still opened and listening for events, the handle will remain open.
By modifying the snippet a bit. The issue is gone without stop listening to parentPort messages.
process.once('beforeExit', () => {
console.log('beforeExit')
})
const { Worker } = require('worker_threads')
const w = new Worker(`
const { parentPort } = require('worker_threads');
parentPort.on('message', (msg) => {
parentPort.postMessage(msg);
});
parentPort.unref(); // unref handle
`, { eval: true })
// w.on('message', () => {}) // <--- enable this line to let Node.js exit
w.unref()
w.on('message', () => {}) // <--- or comment this line to let Node.js exit
from node.
I found a workaround. A possible fix for this.
from node.
Related Issues (20)
- The registration order of http server listen depends on the Node.js version HOT 5
- Ambiguous steps in `PACKAGE_IMPORTS_EXPORTS_RESOLVE` reference documentation HOT 8
- Memory leak or GC overrun in sort() HOT 2
- (Discuss) `_stream_*`
- Failed to build node in windows because dependency openssl.lib didn't build
- Keep fs.exists not deprecated HOT 2
- CVE-2024-28863 in Nodejs.18 HOT 3
- Linking uses too much memory when building v22.2.0 on macOS HOT 3
- Wtn
- Estoy compartiendo 'WTN.CSV' con usted desde WPS Office
- No such built-in module: sea HOT 6
- `DOMException`s do not work with `v8.serialize()`
- In "fs.copyFile(src, dest[, mode], callback)" , for callback,'err' is not defined. HOT 5
- /rbd/pnpm-volume/040a3e2e-c162-47cf-9d77-c827c149c920/node_modules/@discordjs/util/dist/index.js:39 return () => defaultValue ??= cb(); ^^^ SyntaxError: Unexpected token '??=' at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:12) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:101:18) at Object.<anonymous> (/rbd/pnpm-volume/040a3e2e-c162-47cf-9d77-c827c149c920/node_modules/discord.js/src/index.js:3:29) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) HOT 1
- Fatal error nullptr check in AsyncModuleExecutionFulfilled HOT 1
- Inconsistent output depending on if it's piped somewhere or not HOT 4
- Build fail on macOS arm64 - ld: symbol(s) not found for architecture arm64 HOT 1
- Expose SQLite? HOT 1
- Source maps not working when source code is piped in
- Migrating from object.ref()/object.unref() to process.ref()/process.unref() HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node.