Giter VIP home page Giter VIP logo

Comments (4)

juanarbol avatar juanarbol commented on June 10, 2024 1

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.

theanarkh avatar theanarkh commented on June 10, 2024
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.

  1. 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);
}
  1. then call w.unref() Node.js will run port.ref() but the number of message listener is still 1.
  2. 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.

juanarbol avatar juanarbol commented on June 10, 2024

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.

juanarbol avatar juanarbol commented on June 10, 2024

I found a workaround. A possible fix for this.

from node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.