Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ else if (typeof process !== 'undefined') {
var parentPort = WorkerThreads.parentPort;
worker.send = parentPort.postMessage.bind(parentPort);
worker.on = parentPort.on.bind(parentPort);
worker.exit = process.exit.bind(process);
worker.exit = function (code) {
process.exitCode = code;
parentPort.postMessage('__workerpool-terminate__');
Copy link
Contributor

@joshLong145 joshLong145 Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused on why we are seeing a message to the parent for shutdown when this message typically sent from parent to child, not child to parent. The parent message handler does not have implementations to deal with this message type.

Perhaps I am missing something, but to me this is not going to achieve the goals of providing the exit code on shutdown. Can't there be another binding for worker.exitCode like worker.exit was binded?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have the same question indeed, this message is only send from the main process to the worker right now.

I would expect something like:

worker.exit = function (code) {
  process.exitCode = code
  process.exit()
}

};
} else {
worker.on = process.on.bind(process);
// ignore transfer argument since it is not supported by process
Expand All @@ -92,7 +95,10 @@ else if (typeof process !== 'undefined') {
worker.on('disconnect', function () {
process.exit(1);
});
worker.exit = process.exit.bind(process);
worker.exit = function (code) {
process.exitCode = code;
process.send('__workerpool-terminate__');
};
}
}
else {
Expand All @@ -102,8 +108,8 @@ else {
function convertError(error) {
return Object.getOwnPropertyNames(error).reduce(function(product, name) {
return Object.defineProperty(product, name, {
value: error[name],
enumerable: true
value: error[name],
enumerable: true
});
}, {});
}
Expand Down Expand Up @@ -382,4 +388,4 @@ worker.emit = function (payload) {
if (typeof exports !== 'undefined') {
exports.add = worker.register;
exports.emit = worker.emit;
}
}
2 changes: 1 addition & 1 deletion test/Pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ describe('Pool', function () {

assert.strictEqual(pool.workers.length, 1);

return pool.terminate(false, 1000)
return pool.terminate(false, 2000)
.then(function() {
assert.strictEqual(pool.workers.length, 0);
});
Expand Down
Loading