Skip to content

Commit

Permalink
cache response message on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
dygabo committed Apr 29, 2024
1 parent 4a1a877 commit 02c8677
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/internal/modules/esm/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
// so it can detect the exit event.
const { exit } = process;
process.exit = function(code) {
const exitMsg = wrapMessage('exit', code ?? process.exitCode);
if (hooks) {
for (let i = 0; i < allThreadRegisteredHandlerPorts.length; i++) {
const registeredPort = allThreadRegisteredHandlerPorts[i];
registeredPort.postMessage(wrapMessage('exit', code ?? process.exitCode));
registeredPort.postMessage(exitMsg);
}

for (const { port, lock: operationLock } of unsettledResponsePorts) {
port.postMessage(wrapMessage('exit', code ?? process.exitCode));
port.postMessage(exitMsg);
// Wake all threads that have pending operations. Is that needed???
AtomicsAdd(operationLock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
AtomicsNotify(operationLock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
}
}
syncCommPort.postMessage(wrapMessage('exit', code ?? process.exitCode));
syncCommPort.postMessage(exitMsg);
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
return ReflectApply(exit, this, arguments);
Expand Down Expand Up @@ -185,7 +186,7 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
/**
* @callback registerHandler
* @param {MessagePort} toWorkerThread - Upon Worker creation a message channel between the new Worker
* and the Hooks thread is bein initialized. This is the message part that the Hooks thread will use post
* and the Hooks thread is being initialized. This is the MessagePort that the Hooks thread will use post
* messages to the worker. The other MessagePort is passed to the new Worker itself via LOAD_SCRIPT message
*/
function registerHandler(toWorkerThread) {
Expand Down

0 comments on commit 02c8677

Please sign in to comment.