Skip to content

Commit f29f084

Browse files
committed
fix node hang and address feedback
1 parent 10c43d7 commit f29f084

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/library_pthread.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ var LibraryPThread = {
120120
noExitRuntime = false;
121121
#endif
122122
},
123-
124123
initMessageRelay: function() {
125124
// Spawn a dedicated worker for passing messages between threads. Instead
126125
// of having each thread hold a message port for every other thread, they
@@ -145,7 +144,6 @@ Object.assign(global, {
145144
`;
146145
}
147146
#endif
148-
149147
relayCode += '(' + (() => {
150148
// Map pthread IDs to message ports we use to communicate with those
151149
// pthreads.
@@ -158,7 +156,7 @@ Object.assign(global, {
158156
function handleMessage(msg) {
159157
const thread = msg.data.targetThread;
160158
const port = threadPorts.get(thread);
161-
if (port !== undefined) {
159+
if (port) {
162160
console.log("forwarding message to", thread);
163161
port.postMessage(msg.data, msg.data.transferList);
164162
} else {
@@ -183,29 +181,33 @@ Object.assign(global, {
183181
bufferedMessages.get(thread).forEach(handleMessage);
184182
bufferedMessages.delete(thread);
185183
}
186-
return;
187-
}
188-
if (cmd === 'destroy') {
184+
} else if (cmd === 'destroy') {
189185
bufferedMessages.delete(thread);
190186
}
187+
#if ASSERTIONS
188+
else {
189+
console.error('unrecognized message relay command:', cmd);
190+
}
191+
#endif
191192
};
192193
}).toString() + ')()';
193194

194-
var base64;
195195
#if ENVIRONMENT_MAY_BE_NODE
196196
if (ENVIRONMENT_IS_NODE) {
197197
// TODO: Node 16+ has btoa, so remove this when we drop support for
198198
// older Nodes.
199-
base64 = (s) => { return Buffer.from(s).toString('base64'); };
200-
} else {
201-
base64 = btoa;
199+
global.btoa = (s) => { return Buffer.from(s).toString('base64'); };
202200
}
203-
#else
204-
base64 = btoa;
205201
#endif
206-
207202
PThread.messageRelay = new Worker(new URL(
208-
'data:text/javascript;base64,' + base64(relayCode)));
203+
'data:text/javascript;base64,' + btoa(relayCode)));
204+
#if ENVIRONMENT_MAY_BE_NODE
205+
if (ENVIRONMENT_IS_NODE) {
206+
// Do not keep Node alive if the message relay is the only thing
207+
// running.
208+
PThread.messageRelay.unref();
209+
}
210+
#endif
209211
},
210212

211213
#if PTHREADS_PROFILING
@@ -336,9 +338,9 @@ Object.assign(global, {
336338
// accessible variable about the thread that initiated the proxying.
337339
if (worker.pthread_ptr) PThread.currentProxiedOperationCallerThread = worker.pthread_ptr;
338340

339-
if (d['targetThread'] && d['targetThread'] != _pthread_self()) {
340-
abort("unexpected message intended for thread" + d['targetThread']);
341-
}
341+
#if ASSERTIONS
342+
assert(!d['targetThread'] || d['targetThread'] == _pthread_self());
343+
#endif
342344

343345
if (cmd === 'processProxyingQueue') {
344346
executeNotifiedProxyingQueue(d['queue']);
@@ -593,7 +595,6 @@ Object.assign(global, {
593595
}
594596
},
595597

596-
597598
$killThread__deps: ['_emscripten_thread_free_data'],
598599
$killThread: function(pthread_ptr) {
599600
#if PTHREADS_DEBUG
@@ -708,18 +709,13 @@ Object.assign(global, {
708709

709710
worker.pthread_ptr = threadParams.pthread_ptr;
710711

711-
var MsgChannel;
712712
#if ENVIRONMENT_MAY_BE_NODE
713713
if (ENVIRONMENT_IS_NODE) {
714714
// TODO: This isn't necessary in Node 18+
715-
MsgChannel = require('worker_threads').MessageChannel;
716-
} else {
717-
MsgChannel = MessageChannel;
715+
global.MessageChannel = require('worker_threads').MessageChannel;
718716
}
719-
#else
720-
MsgChannel = MessageChannel;
721717
#endif
722-
var channel = new MsgChannel();
718+
var channel = new MessageChannel();
723719
var msg = {
724720
'cmd': 'run',
725721
'start_routine': threadParams.startRoutine,

0 commit comments

Comments
 (0)