Terminate worker when there are some errors in the worker

21 views
Skip to first unread message

wenzheng chen

unread,
Mar 4, 2024, 11:12:48 AMMar 4
to emscripten-discuss
Hi friends,

Recently, I hit an issue during my developing with emscripten. I found it was different to terminate workers when there were some errors in the worker.
I use below params to generate related files: test.js, test.worker.js, test.wasm

```
    LINKFLAGS=[
        '--js-library', webCodecsLibrary.abspath,
        '-lembind',
        '-sINITIAL_MEMORY=1GB',
        '-sEXPORTED_FUNCTIONS=%s' % ','.join(exportedFunctions),
        '-sEXPORTED_RUNTIME_METHODS=%s' % ','.join(exportedRuntime),
        '-sEXPORT_ES6=1',
        '-sMODULARIZE=1',
        '-sENVIRONMENT=web,worker',
        '-sPTHREAD_POOL_SIZE=8',
        '-sDYNAMIC_EXECUTION=0',
    ])
```
I need to test a scenario where the network is disconnected. I've observed that even when the network is disconnected, workers are still created. However, due to the failure to load files within the worker, loading the WebAssembly module fails, and as a result, the worker persists. I'd like to know how to terminate these workers.

Found killThread codes in test.js, but I don't know how post a killThread message from worker.

worker.onmessage = (e) => {
var d = e['data'];
var cmd = d['cmd'];
// Sometimes we need to backproxy events to the calling thread (e.g.
// HTML5 DOM events handlers such as
// emscripten_set_mousemove_callback()), so keep track in a globally
// accessible variable about the thread that initiated the proxying.
if (worker.pthread_ptr) PThread.currentProxiedOperationCallerThread = worker.pthread_ptr;
// If this message is intended to a recipient that is not the main thread, forward it to the target thread.
if (d['targetThread'] && d['targetThread'] != _pthread_self()) {
var targetWorker = PThread.pthreads[d.targetThread];
if (targetWorker) {
targetWorker.postMessage(d, d['transferList']);
} else {
err('Internal error! Worker sent a message "' + cmd + '" to target pthread ' + d['targetThread'] + ', but that thread no longer exists!');
}
PThread.currentProxiedOperationCallerThread = undefined;
return;
}
if (cmd === 'processProxyingQueue') {
executeNotifiedProxyingQueue(d['queue']);
} else if (cmd === 'spawnThread') {
spawnThread(d);
} else if (cmd === 'cleanupThread') {
cleanupThread(d['thread']);
} else if (cmd === 'killThread') {
killThread(d['thread']);
} else if (cmd === 'cancelThread') {
cancelThread(d['thread']);
} else if (cmd === 'loaded') {
worker.loaded = true;
onFinishedLoading(worker);
} else if (cmd === 'print') {
out('Thread ' + d['threadId'] + ': ' + d['text']);
} else if (cmd === 'printErr') {
err('Thread ' + d['threadId'] + ': ' + d['text']);
} else if (cmd === 'alert') {
alert('Thread ' + d['threadId'] + ': ' + d['text']);
} else if (d.target === 'setimmediate') {
// Worker wants to postMessage() to itself to implement setImmediate()
// emulation.
worker.postMessage(d);
} else if (cmd === 'callHandler') {
Module[d['handler']](...d['args']);
} else if (cmd) {
// The received message looks like something that should be handled by this message
// handler, (since there is a e.data.cmd field present), but is not one of the
// recognized commands:
err("worker sent an unknown command " + cmd);
}
PThread.currentProxiedOperationCallerThread = undefined;
};
Reply all
Reply to author
Forward
0 new messages