@emnapi/wasi-threads 2.0.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/wasi-threads.d.ts +29 -0
- package/dist/wasi-threads.js +243 -43
- package/package.json +1 -1
package/dist/wasi-threads.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ export declare interface CommandPayloadMap {
|
|
|
27
27
|
'cleanup-thread': CleanupThreadPayload;
|
|
28
28
|
'terminate-all-threads': TerminateAllThreadsPayload;
|
|
29
29
|
'spawn-thread': SpawnThreadPayload;
|
|
30
|
+
'thread-error': ThreadErrorPayload;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export declare type CommandType = keyof CommandPayloadMap;
|
|
@@ -68,6 +69,13 @@ export declare interface ReuseWorkerOptions {
|
|
|
68
69
|
strict?: boolean;
|
|
69
70
|
}
|
|
70
71
|
|
|
72
|
+
export declare interface SerializedError {
|
|
73
|
+
name: string;
|
|
74
|
+
message: string;
|
|
75
|
+
stack?: string;
|
|
76
|
+
cause?: unknown;
|
|
77
|
+
}
|
|
78
|
+
|
|
71
79
|
export declare interface SpawnThreadPayload {
|
|
72
80
|
startArg: number;
|
|
73
81
|
errorOrTid: number;
|
|
@@ -87,6 +95,14 @@ export declare interface StartResult {
|
|
|
87
95
|
export declare interface TerminateAllThreadsPayload {
|
|
88
96
|
}
|
|
89
97
|
|
|
98
|
+
export declare interface ThreadErrorPayload {
|
|
99
|
+
error: SerializedError;
|
|
100
|
+
phase: ThreadFailurePhase;
|
|
101
|
+
tid?: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export declare type ThreadFailurePhase = 'load' | 'start' | 'async-work' | 'message' | 'messageerror' | 'exit';
|
|
105
|
+
|
|
90
106
|
export declare class ThreadManager {
|
|
91
107
|
unusedWorkers: WorkerLike[];
|
|
92
108
|
pthreads: Record<number, WorkerLike>;
|
|
@@ -94,6 +110,10 @@ export declare class ThreadManager {
|
|
|
94
110
|
wasmModule: WebAssembly.Module | null;
|
|
95
111
|
wasmMemory: WebAssembly.Memory | null;
|
|
96
112
|
private readonly messageEvents;
|
|
113
|
+
private readonly registeredWorkers;
|
|
114
|
+
private readonly expectedTerminations;
|
|
115
|
+
private readonly loadRejects;
|
|
116
|
+
private _fatalError;
|
|
97
117
|
private readonly _childThread;
|
|
98
118
|
private readonly _onCreateWorker;
|
|
99
119
|
private readonly _reuseWorker;
|
|
@@ -108,14 +128,20 @@ export declare class ThreadManager {
|
|
|
108
128
|
loadWasmModuleToAllWorkers(): Promise<WorkerLike[]>;
|
|
109
129
|
preloadWorkers(): Promise<WorkerLike[]>;
|
|
110
130
|
setup(wasmModule: WebAssembly.Module, wasmMemory: WebAssembly.Memory): void;
|
|
131
|
+
private assertRunning;
|
|
132
|
+
private fail;
|
|
111
133
|
markId(worker: WorkerLike): number;
|
|
112
134
|
returnWorkerToPool(worker: WorkerLike): void;
|
|
113
135
|
loadWasmModuleToWorker(worker: WorkerLike, sab?: Int32Array): Promise<WorkerLike>;
|
|
114
136
|
allocateUnusedWorker(): WorkerLike;
|
|
137
|
+
private allocatePoolWorker;
|
|
138
|
+
private dropFailedLoad;
|
|
115
139
|
getNewWorker(sab?: Int32Array): WorkerLike | undefined;
|
|
140
|
+
private allocateAndLoadWorker;
|
|
116
141
|
cleanThread(worker: WorkerLike, tid: number, force?: boolean): void;
|
|
117
142
|
terminateWorker(worker: WorkerLike): void;
|
|
118
143
|
terminateAllThreads(): void;
|
|
144
|
+
private shutdownAllWorkers;
|
|
119
145
|
addMessageEventListener(worker: WorkerLike, onMessage: (e: WorkerMessageEvent) => void): () => void;
|
|
120
146
|
fireMessageEvent(worker: WorkerLike, e: WorkerMessageEvent): void;
|
|
121
147
|
}
|
|
@@ -150,7 +176,10 @@ export declare class ThreadMessageHandler {
|
|
|
150
176
|
private _load;
|
|
151
177
|
private _start;
|
|
152
178
|
protected _loaded(err: Error | null, source: WebAssembly.WebAssemblyInstantiatedSource | null, payload: LoadPayload): void;
|
|
179
|
+
private _failQueuedStarts;
|
|
153
180
|
protected handleAfterLoad<E extends WorkerMessageEvent>(e: E, f: (e: E) => void): void;
|
|
181
|
+
protected beforeReportError(_error: Error, _type: WorkerMessageType | 'async-worker-init', _tid?: number): void;
|
|
182
|
+
protected reportError(value: unknown, type: WorkerMessageType | 'async-worker-init', tid?: number): void;
|
|
154
183
|
}
|
|
155
184
|
|
|
156
185
|
export declare interface ThreadMessageHandlerOptions {
|
package/dist/wasi-threads.js
CHANGED
|
@@ -14,13 +14,74 @@ function getPostMessage(options) {
|
|
|
14
14
|
? postMessage
|
|
15
15
|
: undefined;
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function normalizeError(value) {
|
|
18
|
+
if (value instanceof Error)
|
|
19
|
+
return value;
|
|
20
|
+
if (typeof value === 'object' && value !== null && 'error' in value) {
|
|
21
|
+
const nested = value.error;
|
|
22
|
+
if (nested !== undefined && nested !== value)
|
|
23
|
+
return normalizeError(nested);
|
|
24
|
+
}
|
|
25
|
+
const record = typeof value === 'object' && value !== null
|
|
26
|
+
? value
|
|
27
|
+
: undefined;
|
|
28
|
+
const message = record?.message !== undefined
|
|
29
|
+
? String(record.message)
|
|
30
|
+
: typeof value === 'string'
|
|
31
|
+
? value
|
|
32
|
+
: 'Worker sent an unknown error';
|
|
33
|
+
const name = record?.name !== undefined ? String(record.name) : 'Error';
|
|
34
|
+
let error;
|
|
35
|
+
try {
|
|
36
|
+
const ErrorConstructor = name === 'RuntimeError'
|
|
37
|
+
? (_WebAssembly?.RuntimeError ?? Error)
|
|
38
|
+
: Error;
|
|
39
|
+
error = new ErrorConstructor(message);
|
|
40
|
+
}
|
|
41
|
+
catch (_) {
|
|
42
|
+
error = new Error(message);
|
|
43
|
+
}
|
|
44
|
+
error.name = name;
|
|
45
|
+
if (record?.stack !== undefined) {
|
|
46
|
+
Object.defineProperty(error, 'stack', {
|
|
47
|
+
value: String(record.stack),
|
|
48
|
+
writable: true,
|
|
49
|
+
enumerable: false,
|
|
50
|
+
configurable: true
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (record?.cause !== undefined) {
|
|
54
|
+
Object.defineProperty(error, 'cause', {
|
|
55
|
+
value: record.cause,
|
|
56
|
+
writable: true,
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: true
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return error;
|
|
62
|
+
}
|
|
63
|
+
function serializeError(value) {
|
|
64
|
+
const error = normalizeError(value);
|
|
65
|
+
const serialized = {
|
|
66
|
+
name: error.name,
|
|
67
|
+
message: error.message
|
|
68
|
+
};
|
|
69
|
+
if (error.stack !== undefined)
|
|
70
|
+
serialized.stack = error.stack;
|
|
71
|
+
if ('cause' in error && error.cause !== undefined)
|
|
72
|
+
serialized.cause = error.cause;
|
|
73
|
+
return serialized;
|
|
74
|
+
}
|
|
75
|
+
function deserializeError(value) {
|
|
76
|
+
return normalizeError(value);
|
|
77
|
+
}
|
|
78
|
+
function serializeErrorToBuffer(sab, code, error) {
|
|
18
79
|
const i32array = new Int32Array(sab);
|
|
19
80
|
Atomics.store(i32array, 0, code);
|
|
20
81
|
if (code > 1 && error) {
|
|
21
82
|
const name = error.name;
|
|
22
83
|
const message = error.message;
|
|
23
|
-
const stack = error.stack;
|
|
84
|
+
const stack = error.stack ?? '';
|
|
24
85
|
const nameBuffer = new TextEncoder().encode(name);
|
|
25
86
|
const messageBuffer = new TextEncoder().encode(message);
|
|
26
87
|
const stackBuffer = new TextEncoder().encode(stack);
|
|
@@ -33,7 +94,7 @@ function serizeErrorToBuffer(sab, code, error) {
|
|
|
33
94
|
buffer.set(stackBuffer, 16 + nameBuffer.length + messageBuffer.length);
|
|
34
95
|
}
|
|
35
96
|
}
|
|
36
|
-
function
|
|
97
|
+
function deserializeErrorFromBuffer(sab) {
|
|
37
98
|
const i32array = new Int32Array(sab);
|
|
38
99
|
const status = Atomics.load(i32array, 0);
|
|
39
100
|
if (status <= 1) {
|
|
@@ -59,16 +120,17 @@ function deserizeErrorFromBuffer(sab) {
|
|
|
59
120
|
});
|
|
60
121
|
return error;
|
|
61
122
|
}
|
|
123
|
+
const deserizeErrorFromBuffer = deserializeErrorFromBuffer;
|
|
62
124
|
function isSharedArrayBuffer(value) {
|
|
63
125
|
return ((typeof SharedArrayBuffer === 'function' && value instanceof SharedArrayBuffer) ||
|
|
64
126
|
(Object.prototype.toString.call(value) === '[object SharedArrayBuffer]'));
|
|
65
127
|
}
|
|
66
128
|
function isTrapError(e) {
|
|
67
129
|
try {
|
|
68
|
-
return e instanceof _WebAssembly.RuntimeError;
|
|
130
|
+
return e instanceof _WebAssembly.RuntimeError || e?.name === 'RuntimeError';
|
|
69
131
|
}
|
|
70
132
|
catch (_) {
|
|
71
|
-
return
|
|
133
|
+
return e?.name === 'RuntimeError';
|
|
72
134
|
}
|
|
73
135
|
}
|
|
74
136
|
|
|
@@ -131,6 +193,9 @@ class ThreadManager {
|
|
|
131
193
|
this.wasmModule = null;
|
|
132
194
|
this.wasmMemory = null;
|
|
133
195
|
this.messageEvents = new WeakMap();
|
|
196
|
+
this.registeredWorkers = new Set();
|
|
197
|
+
this.expectedTerminations = new WeakSet();
|
|
198
|
+
this.loadRejects = new WeakMap();
|
|
134
199
|
if (!options) {
|
|
135
200
|
throw new TypeError('ThreadManager(): options is not provided');
|
|
136
201
|
}
|
|
@@ -166,11 +231,7 @@ class ThreadManager {
|
|
|
166
231
|
if (this._reuseWorker.size) {
|
|
167
232
|
let pthreadPoolSize = this._reuseWorker.size;
|
|
168
233
|
while (pthreadPoolSize--) {
|
|
169
|
-
|
|
170
|
-
if (ENVIRONMENT_IS_NODE) {
|
|
171
|
-
worker.once('message', () => { });
|
|
172
|
-
worker.unref();
|
|
173
|
-
}
|
|
234
|
+
this.allocatePoolWorker();
|
|
174
235
|
}
|
|
175
236
|
}
|
|
176
237
|
}
|
|
@@ -209,7 +270,22 @@ class ThreadManager {
|
|
|
209
270
|
this.wasmModule = wasmModule;
|
|
210
271
|
this.wasmMemory = wasmMemory;
|
|
211
272
|
}
|
|
273
|
+
assertRunning() {
|
|
274
|
+
if (this._fatalError)
|
|
275
|
+
throw this._fatalError;
|
|
276
|
+
}
|
|
277
|
+
fail(value, rethrow) {
|
|
278
|
+
if (this._fatalError)
|
|
279
|
+
return;
|
|
280
|
+
this._fatalError = normalizeError(value);
|
|
281
|
+
this.shutdownAllWorkers(false, this._fatalError);
|
|
282
|
+
if (rethrow) {
|
|
283
|
+
const error = this._fatalError;
|
|
284
|
+
Promise.resolve().then(() => { throw error; });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
212
287
|
markId(worker) {
|
|
288
|
+
this.assertRunning();
|
|
213
289
|
if (worker.__emnapi_tid)
|
|
214
290
|
return worker.__emnapi_tid;
|
|
215
291
|
const tid = nextWorkerID + 43;
|
|
@@ -219,6 +295,10 @@ class ThreadManager {
|
|
|
219
295
|
return tid;
|
|
220
296
|
}
|
|
221
297
|
returnWorkerToPool(worker) {
|
|
298
|
+
if (this._fatalError) {
|
|
299
|
+
this.terminateWorker(worker);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
222
302
|
var tid = worker.__emnapi_tid;
|
|
223
303
|
if (tid !== undefined) {
|
|
224
304
|
delete this.pthreads[tid];
|
|
@@ -230,31 +310,36 @@ class ThreadManager {
|
|
|
230
310
|
}
|
|
231
311
|
}
|
|
232
312
|
loadWasmModuleToWorker(worker, sab) {
|
|
313
|
+
this.assertRunning();
|
|
233
314
|
if (worker.whenLoaded)
|
|
234
315
|
return worker.whenLoaded;
|
|
316
|
+
this.registeredWorkers.add(worker);
|
|
235
317
|
const err = this.printErr;
|
|
236
318
|
const beforeLoad = this._beforeLoad;
|
|
237
319
|
const _this = this;
|
|
238
320
|
worker.whenLoaded = new Promise((resolve, reject) => {
|
|
239
321
|
const handleError = function (e) {
|
|
322
|
+
if (_this.expectedTerminations.has(worker))
|
|
323
|
+
return;
|
|
240
324
|
let message = 'worker sent an error!';
|
|
241
325
|
if (worker.__emnapi_tid !== undefined) {
|
|
242
326
|
message = 'worker (tid = ' + worker.__emnapi_tid + ') sent an error!';
|
|
243
327
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
try {
|
|
248
|
-
_this.terminateAllThreads();
|
|
249
|
-
}
|
|
250
|
-
catch (_) { }
|
|
251
|
-
}
|
|
328
|
+
const error = normalizeError(e);
|
|
329
|
+
if (error.message) {
|
|
330
|
+
err(message + ' ' + error.message);
|
|
252
331
|
}
|
|
253
332
|
else {
|
|
254
333
|
err(message);
|
|
255
334
|
}
|
|
256
|
-
|
|
257
|
-
|
|
335
|
+
if (!worker.loaded) {
|
|
336
|
+
reject(error);
|
|
337
|
+
_this.dropFailedLoad(worker);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
_this.fail(error, !ENVIRONMENT_IS_NODE);
|
|
341
|
+
if (ENVIRONMENT_IS_NODE)
|
|
342
|
+
throw error;
|
|
258
343
|
};
|
|
259
344
|
const handleMessage = (data) => {
|
|
260
345
|
if (data.__emnapi__) {
|
|
@@ -262,6 +347,7 @@ class ThreadManager {
|
|
|
262
347
|
const payload = data.__emnapi__.payload;
|
|
263
348
|
if (type === 'loaded') {
|
|
264
349
|
worker.loaded = true;
|
|
350
|
+
this.loadRejects.delete(worker);
|
|
265
351
|
if (ENVIRONMENT_IS_NODE && !worker.__emnapi_tid) {
|
|
266
352
|
worker.unref();
|
|
267
353
|
}
|
|
@@ -278,6 +364,17 @@ class ThreadManager {
|
|
|
278
364
|
else if (type === 'terminate-all-threads') {
|
|
279
365
|
this.terminateAllThreads();
|
|
280
366
|
}
|
|
367
|
+
else if (type === 'thread-error') {
|
|
368
|
+
const threadError = payload;
|
|
369
|
+
const error = deserializeError(threadError.error);
|
|
370
|
+
if (!worker.loaded && threadError.phase === 'load') {
|
|
371
|
+
reject(error);
|
|
372
|
+
this.dropFailedLoad(worker);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
this.fail(error, true);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
281
378
|
}
|
|
282
379
|
};
|
|
283
380
|
worker.onmessage = (e) => {
|
|
@@ -285,6 +382,9 @@ class ThreadManager {
|
|
|
285
382
|
this.fireMessageEvent(worker, e);
|
|
286
383
|
};
|
|
287
384
|
worker.onerror = handleError;
|
|
385
|
+
worker.onmessageerror = (e) => {
|
|
386
|
+
handleError(normalizeError(e));
|
|
387
|
+
};
|
|
288
388
|
if (ENVIRONMENT_IS_NODE) {
|
|
289
389
|
worker.on('message', function (data) {
|
|
290
390
|
worker.onmessage?.({
|
|
@@ -294,35 +394,69 @@ class ThreadManager {
|
|
|
294
394
|
worker.on('error', function (e) {
|
|
295
395
|
worker.onerror?.(e);
|
|
296
396
|
});
|
|
397
|
+
worker.on('messageerror', function (e) {
|
|
398
|
+
handleError(normalizeError(e));
|
|
399
|
+
});
|
|
400
|
+
worker.on('exit', function (code) {
|
|
401
|
+
if (!_this.expectedTerminations.has(worker)) {
|
|
402
|
+
handleError(new Error('Worker stopped with exit code ' + code));
|
|
403
|
+
}
|
|
404
|
+
});
|
|
297
405
|
worker.on('detachedExit', function () { });
|
|
298
406
|
}
|
|
299
|
-
|
|
300
|
-
beforeLoad(worker);
|
|
301
|
-
}
|
|
407
|
+
this.loadRejects.set(worker, reject);
|
|
302
408
|
try {
|
|
409
|
+
if (typeof beforeLoad === 'function') {
|
|
410
|
+
beforeLoad(worker);
|
|
411
|
+
}
|
|
303
412
|
worker.postMessage(createMessage('load', {
|
|
304
413
|
wasmModule: this.wasmModule,
|
|
305
414
|
wasmMemory: this.wasmMemory,
|
|
306
415
|
sab
|
|
307
416
|
}));
|
|
308
417
|
}
|
|
309
|
-
catch (
|
|
310
|
-
|
|
311
|
-
|
|
418
|
+
catch (caughtError) {
|
|
419
|
+
let error = caughtError;
|
|
420
|
+
try {
|
|
421
|
+
checkSharedWasmMemory(this.wasmMemory);
|
|
422
|
+
}
|
|
423
|
+
catch (memoryError) {
|
|
424
|
+
error = memoryError;
|
|
425
|
+
}
|
|
426
|
+
reject(error);
|
|
427
|
+
this.dropFailedLoad(worker);
|
|
312
428
|
}
|
|
313
429
|
});
|
|
314
430
|
return worker.whenLoaded;
|
|
315
431
|
}
|
|
316
432
|
allocateUnusedWorker() {
|
|
433
|
+
this.assertRunning();
|
|
317
434
|
const _onCreateWorker = this._onCreateWorker;
|
|
318
435
|
if (typeof _onCreateWorker !== 'function') {
|
|
319
436
|
throw new TypeError('`options.onCreateWorker` is not provided');
|
|
320
437
|
}
|
|
321
438
|
const worker = _onCreateWorker({ type: 'thread', name: 'emnapi-pthread' });
|
|
439
|
+
this.registeredWorkers.add(worker);
|
|
322
440
|
this.unusedWorkers.push(worker);
|
|
323
441
|
return worker;
|
|
324
442
|
}
|
|
443
|
+
allocatePoolWorker() {
|
|
444
|
+
const worker = this.allocateUnusedWorker();
|
|
445
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
446
|
+
worker.once('message', () => { });
|
|
447
|
+
worker.unref();
|
|
448
|
+
}
|
|
449
|
+
return worker;
|
|
450
|
+
}
|
|
451
|
+
dropFailedLoad(worker) {
|
|
452
|
+
const idle = this.unusedWorkers.indexOf(worker) !== -1;
|
|
453
|
+
this.loadRejects.delete(worker);
|
|
454
|
+
this.terminateWorker(worker);
|
|
455
|
+
if (idle && !this._fatalError)
|
|
456
|
+
this.allocatePoolWorker();
|
|
457
|
+
}
|
|
325
458
|
getNewWorker(sab) {
|
|
459
|
+
this.assertRunning();
|
|
326
460
|
if (this._reuseWorker) {
|
|
327
461
|
if (this.unusedWorkers.length === 0) {
|
|
328
462
|
if (this._reuseWorker.strict) {
|
|
@@ -333,14 +467,21 @@ class ThreadManager {
|
|
|
333
467
|
return;
|
|
334
468
|
}
|
|
335
469
|
}
|
|
336
|
-
|
|
470
|
+
return this.allocateAndLoadWorker(sab);
|
|
471
|
+
}
|
|
472
|
+
const worker = this.unusedWorkers.pop();
|
|
473
|
+
if (!worker.whenLoaded) {
|
|
337
474
|
this.loadWasmModuleToWorker(worker, sab);
|
|
338
475
|
}
|
|
339
|
-
return
|
|
476
|
+
return worker;
|
|
340
477
|
}
|
|
478
|
+
return this.allocateAndLoadWorker(sab);
|
|
479
|
+
}
|
|
480
|
+
allocateAndLoadWorker(sab) {
|
|
341
481
|
const worker = this.allocateUnusedWorker();
|
|
482
|
+
this.unusedWorkers.pop();
|
|
342
483
|
this.loadWasmModuleToWorker(worker, sab);
|
|
343
|
-
return this.
|
|
484
|
+
return this.expectedTerminations.has(worker) ? undefined : worker;
|
|
344
485
|
}
|
|
345
486
|
cleanThread(worker, tid, force) {
|
|
346
487
|
if (!force && this._reuseWorker) {
|
|
@@ -354,27 +495,43 @@ class ThreadManager {
|
|
|
354
495
|
}
|
|
355
496
|
terminateWorker(worker) {
|
|
356
497
|
const tid = worker.__emnapi_tid;
|
|
498
|
+
this.expectedTerminations.add(worker);
|
|
499
|
+
this.registeredWorkers.delete(worker);
|
|
500
|
+
const index = this.unusedWorkers.indexOf(worker);
|
|
501
|
+
if (index !== -1)
|
|
502
|
+
this.unusedWorkers.splice(index, 1);
|
|
503
|
+
this.loadRejects.delete(worker);
|
|
357
504
|
worker.terminate();
|
|
358
505
|
this.messageEvents.get(worker)?.clear();
|
|
359
506
|
this.messageEvents.delete(worker);
|
|
360
507
|
worker.onmessage = (e) => {
|
|
361
508
|
if (e.data.__emnapi__) {
|
|
509
|
+
const type = e.data.__emnapi__.type;
|
|
510
|
+
if (type === 'loaded')
|
|
511
|
+
return;
|
|
362
512
|
const err = this.printErr;
|
|
363
|
-
err('received "' +
|
|
513
|
+
err('received "' + type + '" command from terminated worker: ' + tid);
|
|
364
514
|
}
|
|
365
515
|
};
|
|
366
516
|
}
|
|
367
517
|
terminateAllThreads() {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
518
|
+
this.shutdownAllWorkers(true);
|
|
519
|
+
}
|
|
520
|
+
shutdownAllWorkers(recreatePool, reason) {
|
|
521
|
+
const workers = new Set([
|
|
522
|
+
...this.registeredWorkers,
|
|
523
|
+
...Object.values(this.pthreads),
|
|
524
|
+
...this.unusedWorkers
|
|
525
|
+
]);
|
|
526
|
+
workers.forEach((worker) => {
|
|
527
|
+
if (reason)
|
|
528
|
+
this.loadRejects.get(worker)?.(reason);
|
|
529
|
+
this.terminateWorker(worker);
|
|
530
|
+
});
|
|
375
531
|
this.unusedWorkers = [];
|
|
376
532
|
this.pthreads = Object.create(null);
|
|
377
|
-
this.
|
|
533
|
+
if (recreatePool && !this._fatalError)
|
|
534
|
+
this.preparePool();
|
|
378
535
|
}
|
|
379
536
|
addMessageEventListener(worker, onMessage) {
|
|
380
537
|
let listeners = this.messageEvents.get(worker);
|
|
@@ -657,7 +814,7 @@ class WASIThreads {
|
|
|
657
814
|
worker.whenLoaded.catch((err) => {
|
|
658
815
|
delete worker.whenLoaded;
|
|
659
816
|
PThread.cleanThread(worker, tid, true);
|
|
660
|
-
|
|
817
|
+
PThread.printErr('failed to load the worker of thread ' + tid + ': ' + ((err && err.stack) || err));
|
|
661
818
|
});
|
|
662
819
|
}
|
|
663
820
|
if (isNewABI) {
|
|
@@ -799,7 +956,7 @@ class ThreadMessageHandler {
|
|
|
799
956
|
}
|
|
800
957
|
this.postMessage = postMsg;
|
|
801
958
|
this.onLoad = options?.onLoad;
|
|
802
|
-
this.onError = typeof options?.onError === 'function' ? options.onError : (
|
|
959
|
+
this.onError = typeof options?.onError === 'function' ? options.onError : (error) => { throw error; };
|
|
803
960
|
this.instance = undefined;
|
|
804
961
|
this.messagesBeforeLoad = [];
|
|
805
962
|
}
|
|
@@ -824,7 +981,8 @@ class ThreadMessageHandler {
|
|
|
824
981
|
}
|
|
825
982
|
}
|
|
826
983
|
catch (err) {
|
|
827
|
-
|
|
984
|
+
const tid = type === 'start' ? payload.tid : undefined;
|
|
985
|
+
this.reportError(err, type, tid);
|
|
828
986
|
}
|
|
829
987
|
}
|
|
830
988
|
}
|
|
@@ -841,7 +999,14 @@ class ThreadMessageHandler {
|
|
|
841
999
|
}
|
|
842
1000
|
const then = source && 'then' in source ? source.then : undefined;
|
|
843
1001
|
if (typeof then === 'function') {
|
|
844
|
-
then.call(source, (source) => { this._loaded(null, source, payload); }, (err) => {
|
|
1002
|
+
then.call(source, (source) => { this._loaded(null, source, payload); }, (err) => {
|
|
1003
|
+
try {
|
|
1004
|
+
this._loaded(normalizeError(err), null, payload);
|
|
1005
|
+
}
|
|
1006
|
+
catch (loadError) {
|
|
1007
|
+
this.reportError(loadError, 'load');
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
845
1010
|
}
|
|
846
1011
|
else {
|
|
847
1012
|
this._loaded(null, source, payload);
|
|
@@ -874,17 +1039,20 @@ class ThreadMessageHandler {
|
|
|
874
1039
|
_loaded(err, source, payload) {
|
|
875
1040
|
if (err) {
|
|
876
1041
|
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
1042
|
+
this._failQueuedStarts(err);
|
|
877
1043
|
throw err;
|
|
878
1044
|
}
|
|
879
1045
|
if (source == null) {
|
|
880
1046
|
const err = new TypeError('onLoad should return an object');
|
|
881
1047
|
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
1048
|
+
this._failQueuedStarts(err);
|
|
882
1049
|
throw err;
|
|
883
1050
|
}
|
|
884
1051
|
const instance = source.instance;
|
|
885
1052
|
if (!instance) {
|
|
886
1053
|
const err = new TypeError('onLoad should return an object which includes "instance"');
|
|
887
1054
|
notifyPthreadCreateResult(payload.sab, 2, err);
|
|
1055
|
+
this._failQueuedStarts(err);
|
|
888
1056
|
throw err;
|
|
889
1057
|
}
|
|
890
1058
|
this.instance = instance;
|
|
@@ -897,6 +1065,14 @@ class ThreadMessageHandler {
|
|
|
897
1065
|
this.handle({ data });
|
|
898
1066
|
}
|
|
899
1067
|
}
|
|
1068
|
+
_failQueuedStarts(err) {
|
|
1069
|
+
this.messagesBeforeLoad = this.messagesBeforeLoad.filter((data) => {
|
|
1070
|
+
if (data?.__emnapi__?.type !== 'start')
|
|
1071
|
+
return true;
|
|
1072
|
+
notifyPthreadCreateResult(data.__emnapi__.payload.sab, 2, err);
|
|
1073
|
+
return false;
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
900
1076
|
handleAfterLoad(e, f) {
|
|
901
1077
|
if (this.instance !== undefined) {
|
|
902
1078
|
f.call(this, e);
|
|
@@ -905,10 +1081,34 @@ class ThreadMessageHandler {
|
|
|
905
1081
|
this.messagesBeforeLoad.push(e.data);
|
|
906
1082
|
}
|
|
907
1083
|
}
|
|
1084
|
+
beforeReportError(_error, _type, _tid) { }
|
|
1085
|
+
reportError(value, type, tid) {
|
|
1086
|
+
const error = normalizeError(value);
|
|
1087
|
+
this.beforeReportError(error, type, tid);
|
|
1088
|
+
const phase = type === 'async-worker-init' ? 'async-work' : type;
|
|
1089
|
+
const report = () => {
|
|
1090
|
+
try {
|
|
1091
|
+
this.postMessage(createMessage('thread-error', {
|
|
1092
|
+
error: serializeError(error),
|
|
1093
|
+
phase,
|
|
1094
|
+
tid
|
|
1095
|
+
}));
|
|
1096
|
+
}
|
|
1097
|
+
catch (_) { }
|
|
1098
|
+
};
|
|
1099
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
1100
|
+
this.onError(error, type);
|
|
1101
|
+
report();
|
|
1102
|
+
}
|
|
1103
|
+
else {
|
|
1104
|
+
report();
|
|
1105
|
+
this.onError(error, type);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
908
1108
|
}
|
|
909
1109
|
function notifyPthreadCreateResult(sab, result, error) {
|
|
910
1110
|
if (sab) {
|
|
911
|
-
|
|
1111
|
+
serializeErrorToBuffer(sab.buffer, result, error);
|
|
912
1112
|
Atomics.notify(sab, 0);
|
|
913
1113
|
}
|
|
914
1114
|
}
|