@napi-rs/cli 3.10.4 → 3.10.6
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/cli.js +1943 -153
- package/dist/index.cjs +1942 -152
- package/dist/index.d.cts +19 -7
- package/dist/index.d.ts +19 -7
- package/dist/index.js +1943 -153
- package/docs/build.md +1 -0
- package/docs/wasi.md +351 -8
- package/package.json +2 -2
- package/src/api/__tests__/__snapshots__/templates.spec.ts.md +2629 -86
- package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
- package/src/api/__tests__/templates.spec.ts +2192 -10
- package/src/api/build.ts +38 -1
- package/src/api/templates/load-wasi-template.ts +1412 -30
- package/src/api/templates/wasi-worker-template.ts +191 -63
- package/src/commands/__tests__/build.spec.ts +6 -0
- package/src/def/build.ts +13 -0
- package/src/utils/__tests__/ohos-selfsign.spec.ts +285 -0
- package/src/utils/__tests__/reconciliation.spec.ts +19 -0
- package/src/utils/config.ts +10 -6
- package/src/utils/index.ts +1 -0
- package/src/utils/misc.ts +5 -4
- package/src/utils/ohos-selfsign.ts +646 -0
|
@@ -135,6 +135,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
135
135
|
let __emnapiContextDestroyPromise␊
|
|
136
136
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
137
137
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
138
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
139
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
140
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
141
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
142
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
143
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
144
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
145
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
146
|
+
let __finishParkedWasmEnvCleanup␊
|
|
147
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
148
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
149
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
150
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
151
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
138
152
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
139
153
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
140
154
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -247,7 +261,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
247
261
|
}␊
|
|
248
262
|
␊
|
|
249
263
|
function __prepareWasmEnvCleanup() {␊
|
|
250
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
264
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
265
|
+
return␊
|
|
266
|
+
}␊
|
|
267
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
268
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
269
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
270
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
271
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
272
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
273
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
274
|
+
// joined and the workers never drained.␊
|
|
275
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
276
|
+
if (finishParked !== undefined) {␊
|
|
277
|
+
finishParked()␊
|
|
278
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
279
|
+
return␊
|
|
280
|
+
}␊
|
|
281
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
251
282
|
return␊
|
|
252
283
|
}␊
|
|
253
284
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -262,10 +293,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
262
293
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
263
294
|
}␊
|
|
264
295
|
__emnapiWasmEnvCleanupRan = true␊
|
|
296
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
265
297
|
}␊
|
|
266
298
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
267
299
|
}␊
|
|
268
300
|
␊
|
|
301
|
+
/**␊
|
|
302
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
303
|
+
* could deliver them.␊
|
|
304
|
+
*␊
|
|
305
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
306
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
307
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
308
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
309
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
310
|
+
* were for then hang forever, silently.␊
|
|
311
|
+
*␊
|
|
312
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
313
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
314
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
315
|
+
* is already unreachable by then.␊
|
|
316
|
+
*/␊
|
|
317
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
318
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
319
|
+
return␊
|
|
320
|
+
}␊
|
|
321
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
322
|
+
if (typeof pending !== 'function') {␊
|
|
323
|
+
return␊
|
|
324
|
+
}␊
|
|
325
|
+
let queued␊
|
|
326
|
+
try {␊
|
|
327
|
+
queued = pending()␊
|
|
328
|
+
} catch {␊
|
|
329
|
+
return␊
|
|
330
|
+
}␊
|
|
331
|
+
if (!queued) {␊
|
|
332
|
+
return␊
|
|
333
|
+
}␊
|
|
334
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
335
|
+
try {␊
|
|
336
|
+
const consoleHost = globalThis.console␊
|
|
337
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
338
|
+
consoleHost.error(␊
|
|
339
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
340
|
+
queued +␊
|
|
341
|
+
" queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",␊
|
|
342
|
+
)␊
|
|
343
|
+
}␊
|
|
344
|
+
} catch {}␊
|
|
345
|
+
}␊
|
|
346
|
+
␊
|
|
269
347
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
270
348
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
271
349
|
// ahead of it on a faster queue.␊
|
|
@@ -315,6 +393,291 @@ Generated by [AVA](https://avajs.dev).
|
|
|
315
393
|
}␊
|
|
316
394
|
}␊
|
|
317
395
|
␊
|
|
396
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
397
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
398
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
399
|
+
// the loop instead of yielding it.␊
|
|
400
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
401
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
402
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
403
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
404
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
405
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
406
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
407
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
408
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
409
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
410
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
411
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
412
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
413
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
414
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
415
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
416
|
+
␊
|
|
417
|
+
/**␊
|
|
418
|
+
* Pacing state for one runtime-work poll.␊
|
|
419
|
+
*␊
|
|
420
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
421
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
422
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
423
|
+
* instance must not disarm the fallback for the next one.␊
|
|
424
|
+
*/␊
|
|
425
|
+
function __createWasmRuntimePollPace() {␊
|
|
426
|
+
return {␊
|
|
427
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
428
|
+
arrivals: 0,␊
|
|
429
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
430
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
431
|
+
settleTurn: undefined,␊
|
|
432
|
+
turnTimerDueAt: 0,␊
|
|
433
|
+
}␊
|
|
434
|
+
}␊
|
|
435
|
+
␊
|
|
436
|
+
/**␊
|
|
437
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
438
|
+
*␊
|
|
439
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
440
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
441
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
442
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
443
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
444
|
+
*␊
|
|
445
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
446
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
447
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
448
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
449
|
+
* the rescue is how far back that newest live backup is:␊
|
|
450
|
+
*␊
|
|
451
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
452
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
453
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
454
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
455
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
456
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
457
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
458
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
459
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
460
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
461
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
462
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
463
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
464
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
465
|
+
* a throttle last let one through.␊
|
|
466
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
467
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
468
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
469
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
470
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
471
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
472
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
473
|
+
* the second.␊
|
|
474
|
+
*␊
|
|
475
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
476
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
477
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
478
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
479
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
480
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
481
|
+
*/␊
|
|
482
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
483
|
+
const setTimer = globalThis.setTimeout␊
|
|
484
|
+
if (typeof setTimer !== 'function') {␊
|
|
485
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
486
|
+
// already, and that one cannot park.␊
|
|
487
|
+
return␊
|
|
488
|
+
}␊
|
|
489
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
490
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
491
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
492
|
+
let handle␊
|
|
493
|
+
try {␊
|
|
494
|
+
handle = setTimer(() => {␊
|
|
495
|
+
const settleTurn = pace.settleTurn␊
|
|
496
|
+
if (␊
|
|
497
|
+
!settleTurn ||␊
|
|
498
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
499
|
+
) {␊
|
|
500
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
501
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
502
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
503
|
+
return␊
|
|
504
|
+
}␊
|
|
505
|
+
pace.arrivals = 0␊
|
|
506
|
+
pace.settleTurn = undefined␊
|
|
507
|
+
settleTurn()␊
|
|
508
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
509
|
+
} catch {␊
|
|
510
|
+
return␊
|
|
511
|
+
}␊
|
|
512
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
513
|
+
try {␊
|
|
514
|
+
handle.unref()␊
|
|
515
|
+
} catch {}␊
|
|
516
|
+
}␊
|
|
517
|
+
}␊
|
|
518
|
+
␊
|
|
519
|
+
/**␊
|
|
520
|
+
* One turn of the runtime-work poll.␊
|
|
521
|
+
*␊
|
|
522
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
523
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
524
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
525
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
526
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
527
|
+
* ever call \`…_finish\`.␊
|
|
528
|
+
*␊
|
|
529
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
530
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
531
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
532
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
533
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
534
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
535
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
536
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
537
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
538
|
+
*/␊
|
|
539
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
540
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
541
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
542
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
543
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
544
|
+
return new Promise((resolve) => {␊
|
|
545
|
+
let settled = false␊
|
|
546
|
+
const settle = () => {␊
|
|
547
|
+
if (settled) {␊
|
|
548
|
+
return␊
|
|
549
|
+
}␊
|
|
550
|
+
settled = true␊
|
|
551
|
+
if (pace.settleTurn === settle) {␊
|
|
552
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
553
|
+
// due time this turn has already answered.␊
|
|
554
|
+
pace.settleTurn = undefined␊
|
|
555
|
+
}␊
|
|
556
|
+
resolve()␊
|
|
557
|
+
}␊
|
|
558
|
+
__scheduleTimer(() => {␊
|
|
559
|
+
pace.arrivals++␊
|
|
560
|
+
settle()␊
|
|
561
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
562
|
+
// Read next to the arming it describes; see␊
|
|
563
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
564
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
565
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
566
|
+
__scheduleMacrotask(settle)␊
|
|
567
|
+
return␊
|
|
568
|
+
}␊
|
|
569
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
570
|
+
// the timer never arrives.␊
|
|
571
|
+
pace.settleTurn = settle␊
|
|
572
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
573
|
+
})␊
|
|
574
|
+
}␊
|
|
575
|
+
␊
|
|
576
|
+
/**␊
|
|
577
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
578
|
+
* event-loop turns in the middle.␊
|
|
579
|
+
*␊
|
|
580
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
581
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
582
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
583
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
584
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
585
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
586
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
587
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
588
|
+
* entire point.␊
|
|
589
|
+
*␊
|
|
590
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
591
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
592
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
593
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
594
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
595
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
596
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
597
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
598
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
599
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
600
|
+
*␊
|
|
601
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
602
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
603
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
604
|
+
* an idle disposal synchronous.␊
|
|
605
|
+
*/␊
|
|
606
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
607
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
608
|
+
return␊
|
|
609
|
+
}␊
|
|
610
|
+
const exports = __napiInstance?.exports␊
|
|
611
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
612
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
613
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
614
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
615
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
616
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
617
|
+
try {␊
|
|
618
|
+
__prepareWasmEnvCleanup()␊
|
|
619
|
+
} finally {␊
|
|
620
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
621
|
+
}␊
|
|
622
|
+
return␊
|
|
623
|
+
}␊
|
|
624
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
625
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
626
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
627
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
628
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
629
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
630
|
+
let live␊
|
|
631
|
+
try {␊
|
|
632
|
+
live = begin()␊
|
|
633
|
+
} catch (error) {␊
|
|
634
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
635
|
+
throw error␊
|
|
636
|
+
}␊
|
|
637
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
638
|
+
const finishCleanup = () => {␊
|
|
639
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
640
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
641
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
642
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
643
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
644
|
+
return␊
|
|
645
|
+
}␊
|
|
646
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
647
|
+
try {␊
|
|
648
|
+
finish()␊
|
|
649
|
+
} finally {␊
|
|
650
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
651
|
+
}␊
|
|
652
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
653
|
+
}␊
|
|
654
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
655
|
+
finishCleanup()␊
|
|
656
|
+
return␊
|
|
657
|
+
}␊
|
|
658
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
659
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
660
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
661
|
+
return (async () => {␊
|
|
662
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
663
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
664
|
+
// what let that happen at all.␊
|
|
665
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
666
|
+
for (;;) {␊
|
|
667
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
668
|
+
try {␊
|
|
669
|
+
if (!workPending()) {␊
|
|
670
|
+
return␊
|
|
671
|
+
}␊
|
|
672
|
+
} catch {␊
|
|
673
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
674
|
+
// reachable work left. Stop polling and finish.␊
|
|
675
|
+
return␊
|
|
676
|
+
}␊
|
|
677
|
+
}␊
|
|
678
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
679
|
+
}␊
|
|
680
|
+
␊
|
|
318
681
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
319
682
|
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
320
683
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
@@ -451,6 +814,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
451
814
|
}␊
|
|
452
815
|
␊
|
|
453
816
|
__prepareWasmEnvCleanup()␊
|
|
817
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
818
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
819
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
820
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
821
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
822
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
823
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
824
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
825
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
826
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
827
|
+
// rather than skipping it.␊
|
|
828
|
+
return␊
|
|
829
|
+
}␊
|
|
454
830
|
const result = __emnapiContext.destroy()␊
|
|
455
831
|
if (!__isThenable(result)) {␊
|
|
456
832
|
__emnapiContextDestroyed = true␊
|
|
@@ -724,11 +1100,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
724
1100
|
return __finishWasiDisposal()␊
|
|
725
1101
|
}␊
|
|
726
1102
|
␊
|
|
727
|
-
function
|
|
728
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
729
|
-
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
730
|
-
// back to back is what strands them.␊
|
|
731
|
-
__prepareWasmEnvCleanup()␊
|
|
1103
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
732
1104
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
733
1105
|
if (__isThenable(drainResult)) {␊
|
|
734
1106
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -736,6 +1108,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
736
1108
|
return __continueWasiDisposal()␊
|
|
737
1109
|
}␊
|
|
738
1110
|
␊
|
|
1111
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
1112
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
1113
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
1114
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
1115
|
+
// these back to back is what strands them.␊
|
|
1116
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
1117
|
+
if (__isThenable(prepareResult)) {␊
|
|
1118
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
1119
|
+
}␊
|
|
1120
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
1121
|
+
}␊
|
|
1122
|
+
␊
|
|
739
1123
|
function __startWasiDisposal() {␊
|
|
740
1124
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
741
1125
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -896,14 +1280,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
896
1280
|
// be reached without the async-work drain below running first.␊
|
|
897
1281
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
898
1282
|
const cleanupErrors = []␊
|
|
1283
|
+
let prepareResult␊
|
|
1284
|
+
try {␊
|
|
1285
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
1286
|
+
} catch (cleanupError) {␊
|
|
1287
|
+
cleanupErrors.push(cleanupError)␊
|
|
1288
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1289
|
+
}␊
|
|
1290
|
+
if (__isThenable(prepareResult)) {␊
|
|
1291
|
+
return Promise.resolve(prepareResult).then(␊
|
|
1292
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
1293
|
+
(cleanupError) => {␊
|
|
1294
|
+
cleanupErrors.push(cleanupError)␊
|
|
1295
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1296
|
+
},␊
|
|
1297
|
+
)␊
|
|
1298
|
+
}␊
|
|
1299
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
1300
|
+
}␊
|
|
1301
|
+
␊
|
|
1302
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
1303
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
1304
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
1305
|
+
// did not finish does.␊
|
|
1306
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
899
1307
|
let drainResult␊
|
|
900
|
-
let settlementsUnreached = false␊
|
|
901
1308
|
try {␊
|
|
902
|
-
__prepareWasmEnvCleanup()␊
|
|
903
1309
|
drainResult = __drainWasmEnvCleanup()␊
|
|
904
1310
|
} catch (cleanupError) {␊
|
|
905
1311
|
cleanupErrors.push(cleanupError)␊
|
|
906
|
-
|
|
1312
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
907
1313
|
}␊
|
|
908
1314
|
if (__isThenable(drainResult)) {␊
|
|
909
1315
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -914,9 +1320,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
914
1320
|
},␊
|
|
915
1321
|
)␊
|
|
916
1322
|
}␊
|
|
917
|
-
if (settlementsUnreached) {␊
|
|
918
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
919
|
-
}␊
|
|
920
1323
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
921
1324
|
}␊
|
|
922
1325
|
␊
|
|
@@ -948,6 +1351,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
948
1351
|
return __rollbackWasmEnvForWasiInitialization()␊
|
|
949
1352
|
}␊
|
|
950
1353
|
␊
|
|
1354
|
+
// A view of the addon's crash flag, one word of the shared wasm memory, for the␊
|
|
1355
|
+
// pool workers: they raise it when their wasm thread dies, and the shutdown␊
|
|
1356
|
+
// waits inside the cleanup calls on this thread then trap instead of waiting on␊
|
|
1357
|
+
// the dead thread for good. Undefined for an addon built with an older napi.␊
|
|
1358
|
+
let __wasiAddonCrashFlag␊
|
|
1359
|
+
␊
|
|
1360
|
+
function __captureWasiAddonCrashFlag(instance) {␊
|
|
1361
|
+
try {␊
|
|
1362
|
+
const getAddress = instance.exports.napi_wasm_thread_crash_flag_address␊
|
|
1363
|
+
if (typeof getAddress !== 'function') {␊
|
|
1364
|
+
return␊
|
|
1365
|
+
}␊
|
|
1366
|
+
const address = getAddress() >>> 0␊
|
|
1367
|
+
const buffer = __sharedMemory.buffer␊
|
|
1368
|
+
if (address === 0 || address % 4 !== 0 || address + 4 > buffer.byteLength) {␊
|
|
1369
|
+
return␊
|
|
1370
|
+
}␊
|
|
1371
|
+
__wasiAddonCrashFlag = new Int32Array(buffer, address, 1)␊
|
|
1372
|
+
} catch {}␊
|
|
1373
|
+
}␊
|
|
1374
|
+
␊
|
|
1375
|
+
function __shareWasiAddonCrashFlag(worker) {␊
|
|
1376
|
+
if (__wasiAddonCrashFlag === undefined) {␊
|
|
1377
|
+
return␊
|
|
1378
|
+
}␊
|
|
1379
|
+
try {␊
|
|
1380
|
+
worker.postMessage({ __napiRsAddonCrashFlag: __wasiAddonCrashFlag })␊
|
|
1381
|
+
} catch {}␊
|
|
1382
|
+
}␊
|
|
1383
|
+
␊
|
|
951
1384
|
let __wasiModule␊
|
|
952
1385
|
let __napiModule␊
|
|
953
1386
|
␊
|
|
@@ -978,6 +1411,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
978
1411
|
type: 'module',␊
|
|
979
1412
|
})␊
|
|
980
1413
|
__wasiWorkers.add(worker)␊
|
|
1414
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
981
1415
|
␊
|
|
982
1416
|
␊
|
|
983
1417
|
return worker␊
|
|
@@ -993,6 +1427,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
993
1427
|
},␊
|
|
994
1428
|
beforeInit({ instance }) {␊
|
|
995
1429
|
__napiInstance = instance␊
|
|
1430
|
+
__captureWasiAddonCrashFlag(instance)␊
|
|
1431
|
+
for (const worker of __wasiWorkers) {␊
|
|
1432
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
1433
|
+
}␊
|
|
996
1434
|
for (const name of Object.keys(instance.exports)) {␊
|
|
997
1435
|
if (name.startsWith('__napi_register__')) {␊
|
|
998
1436
|
instance.exports[name]()␊
|
|
@@ -1144,6 +1582,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1144
1582
|
let __emnapiContextDestroyPromise␊
|
|
1145
1583
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
1146
1584
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
1585
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
1586
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
1587
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
1588
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
1589
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
1590
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
1591
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
1592
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
1593
|
+
let __finishParkedWasmEnvCleanup␊
|
|
1594
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
1595
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
1596
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
1597
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
1598
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
1147
1599
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
1148
1600
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
1149
1601
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -1256,7 +1708,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1256
1708
|
}␊
|
|
1257
1709
|
␊
|
|
1258
1710
|
function __prepareWasmEnvCleanup() {␊
|
|
1259
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
1711
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
1712
|
+
return␊
|
|
1713
|
+
}␊
|
|
1714
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
1715
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
1716
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
1717
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
1718
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
1719
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
1720
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
1721
|
+
// joined and the workers never drained.␊
|
|
1722
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
1723
|
+
if (finishParked !== undefined) {␊
|
|
1724
|
+
finishParked()␊
|
|
1725
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
1726
|
+
return␊
|
|
1727
|
+
}␊
|
|
1728
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
1260
1729
|
return␊
|
|
1261
1730
|
}␊
|
|
1262
1731
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -1271,10 +1740,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1271
1740
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
1272
1741
|
}␊
|
|
1273
1742
|
__emnapiWasmEnvCleanupRan = true␊
|
|
1743
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
1274
1744
|
}␊
|
|
1275
1745
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
1276
1746
|
}␊
|
|
1277
1747
|
␊
|
|
1748
|
+
/**␊
|
|
1749
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
1750
|
+
* could deliver them.␊
|
|
1751
|
+
*␊
|
|
1752
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
1753
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
1754
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
1755
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
1756
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
1757
|
+
* were for then hang forever, silently.␊
|
|
1758
|
+
*␊
|
|
1759
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
1760
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
1761
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
1762
|
+
* is already unreachable by then.␊
|
|
1763
|
+
*/␊
|
|
1764
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
1765
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
1766
|
+
return␊
|
|
1767
|
+
}␊
|
|
1768
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
1769
|
+
if (typeof pending !== 'function') {␊
|
|
1770
|
+
return␊
|
|
1771
|
+
}␊
|
|
1772
|
+
let queued␊
|
|
1773
|
+
try {␊
|
|
1774
|
+
queued = pending()␊
|
|
1775
|
+
} catch {␊
|
|
1776
|
+
return␊
|
|
1777
|
+
}␊
|
|
1778
|
+
if (!queued) {␊
|
|
1779
|
+
return␊
|
|
1780
|
+
}␊
|
|
1781
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
1782
|
+
try {␊
|
|
1783
|
+
const consoleHost = globalThis.console␊
|
|
1784
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
1785
|
+
consoleHost.error(␊
|
|
1786
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
1787
|
+
queued +␊
|
|
1788
|
+
" queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",␊
|
|
1789
|
+
)␊
|
|
1790
|
+
}␊
|
|
1791
|
+
} catch {}␊
|
|
1792
|
+
}␊
|
|
1793
|
+
␊
|
|
1278
1794
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
1279
1795
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
1280
1796
|
// ahead of it on a faster queue.␊
|
|
@@ -1324,6 +1840,291 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1324
1840
|
}␊
|
|
1325
1841
|
}␊
|
|
1326
1842
|
␊
|
|
1843
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
1844
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
1845
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
1846
|
+
// the loop instead of yielding it.␊
|
|
1847
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
1848
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
1849
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
1850
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
1851
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
1852
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
1853
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
1854
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
1855
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
1856
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
1857
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
1858
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
1859
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
1860
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
1861
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
1862
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
1863
|
+
␊
|
|
1864
|
+
/**␊
|
|
1865
|
+
* Pacing state for one runtime-work poll.␊
|
|
1866
|
+
*␊
|
|
1867
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
1868
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
1869
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
1870
|
+
* instance must not disarm the fallback for the next one.␊
|
|
1871
|
+
*/␊
|
|
1872
|
+
function __createWasmRuntimePollPace() {␊
|
|
1873
|
+
return {␊
|
|
1874
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
1875
|
+
arrivals: 0,␊
|
|
1876
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
1877
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
1878
|
+
settleTurn: undefined,␊
|
|
1879
|
+
turnTimerDueAt: 0,␊
|
|
1880
|
+
}␊
|
|
1881
|
+
}␊
|
|
1882
|
+
␊
|
|
1883
|
+
/**␊
|
|
1884
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
1885
|
+
*␊
|
|
1886
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
1887
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
1888
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
1889
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
1890
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
1891
|
+
*␊
|
|
1892
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
1893
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
1894
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
1895
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
1896
|
+
* the rescue is how far back that newest live backup is:␊
|
|
1897
|
+
*␊
|
|
1898
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
1899
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
1900
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
1901
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
1902
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
1903
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
1904
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
1905
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
1906
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
1907
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
1908
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
1909
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
1910
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
1911
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
1912
|
+
* a throttle last let one through.␊
|
|
1913
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
1914
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
1915
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
1916
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
1917
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
1918
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
1919
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
1920
|
+
* the second.␊
|
|
1921
|
+
*␊
|
|
1922
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
1923
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
1924
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
1925
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
1926
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
1927
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
1928
|
+
*/␊
|
|
1929
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
1930
|
+
const setTimer = globalThis.setTimeout␊
|
|
1931
|
+
if (typeof setTimer !== 'function') {␊
|
|
1932
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
1933
|
+
// already, and that one cannot park.␊
|
|
1934
|
+
return␊
|
|
1935
|
+
}␊
|
|
1936
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
1937
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
1938
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
1939
|
+
let handle␊
|
|
1940
|
+
try {␊
|
|
1941
|
+
handle = setTimer(() => {␊
|
|
1942
|
+
const settleTurn = pace.settleTurn␊
|
|
1943
|
+
if (␊
|
|
1944
|
+
!settleTurn ||␊
|
|
1945
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
1946
|
+
) {␊
|
|
1947
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
1948
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
1949
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
1950
|
+
return␊
|
|
1951
|
+
}␊
|
|
1952
|
+
pace.arrivals = 0␊
|
|
1953
|
+
pace.settleTurn = undefined␊
|
|
1954
|
+
settleTurn()␊
|
|
1955
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
1956
|
+
} catch {␊
|
|
1957
|
+
return␊
|
|
1958
|
+
}␊
|
|
1959
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
1960
|
+
try {␊
|
|
1961
|
+
handle.unref()␊
|
|
1962
|
+
} catch {}␊
|
|
1963
|
+
}␊
|
|
1964
|
+
}␊
|
|
1965
|
+
␊
|
|
1966
|
+
/**␊
|
|
1967
|
+
* One turn of the runtime-work poll.␊
|
|
1968
|
+
*␊
|
|
1969
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
1970
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
1971
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
1972
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
1973
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
1974
|
+
* ever call \`…_finish\`.␊
|
|
1975
|
+
*␊
|
|
1976
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
1977
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
1978
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
1979
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
1980
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
1981
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
1982
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
1983
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
1984
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
1985
|
+
*/␊
|
|
1986
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
1987
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
1988
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
1989
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
1990
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
1991
|
+
return new Promise((resolve) => {␊
|
|
1992
|
+
let settled = false␊
|
|
1993
|
+
const settle = () => {␊
|
|
1994
|
+
if (settled) {␊
|
|
1995
|
+
return␊
|
|
1996
|
+
}␊
|
|
1997
|
+
settled = true␊
|
|
1998
|
+
if (pace.settleTurn === settle) {␊
|
|
1999
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
2000
|
+
// due time this turn has already answered.␊
|
|
2001
|
+
pace.settleTurn = undefined␊
|
|
2002
|
+
}␊
|
|
2003
|
+
resolve()␊
|
|
2004
|
+
}␊
|
|
2005
|
+
__scheduleTimer(() => {␊
|
|
2006
|
+
pace.arrivals++␊
|
|
2007
|
+
settle()␊
|
|
2008
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
2009
|
+
// Read next to the arming it describes; see␊
|
|
2010
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
2011
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
2012
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
2013
|
+
__scheduleMacrotask(settle)␊
|
|
2014
|
+
return␊
|
|
2015
|
+
}␊
|
|
2016
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
2017
|
+
// the timer never arrives.␊
|
|
2018
|
+
pace.settleTurn = settle␊
|
|
2019
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
2020
|
+
})␊
|
|
2021
|
+
}␊
|
|
2022
|
+
␊
|
|
2023
|
+
/**␊
|
|
2024
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
2025
|
+
* event-loop turns in the middle.␊
|
|
2026
|
+
*␊
|
|
2027
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
2028
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
2029
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
2030
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
2031
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
2032
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
2033
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
2034
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
2035
|
+
* entire point.␊
|
|
2036
|
+
*␊
|
|
2037
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
2038
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
2039
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
2040
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
2041
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
2042
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
2043
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
2044
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
2045
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
2046
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
2047
|
+
*␊
|
|
2048
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
2049
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
2050
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
2051
|
+
* an idle disposal synchronous.␊
|
|
2052
|
+
*/␊
|
|
2053
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
2054
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
2055
|
+
return␊
|
|
2056
|
+
}␊
|
|
2057
|
+
const exports = __napiInstance?.exports␊
|
|
2058
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
2059
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
2060
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
2061
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
2062
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
2063
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
2064
|
+
try {␊
|
|
2065
|
+
__prepareWasmEnvCleanup()␊
|
|
2066
|
+
} finally {␊
|
|
2067
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
2068
|
+
}␊
|
|
2069
|
+
return␊
|
|
2070
|
+
}␊
|
|
2071
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
2072
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
2073
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
2074
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
2075
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
2076
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
2077
|
+
let live␊
|
|
2078
|
+
try {␊
|
|
2079
|
+
live = begin()␊
|
|
2080
|
+
} catch (error) {␊
|
|
2081
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
2082
|
+
throw error␊
|
|
2083
|
+
}␊
|
|
2084
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
2085
|
+
const finishCleanup = () => {␊
|
|
2086
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
2087
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
2088
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
2089
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
2090
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
2091
|
+
return␊
|
|
2092
|
+
}␊
|
|
2093
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
2094
|
+
try {␊
|
|
2095
|
+
finish()␊
|
|
2096
|
+
} finally {␊
|
|
2097
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
2098
|
+
}␊
|
|
2099
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
2100
|
+
}␊
|
|
2101
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
2102
|
+
finishCleanup()␊
|
|
2103
|
+
return␊
|
|
2104
|
+
}␊
|
|
2105
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
2106
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
2107
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
2108
|
+
return (async () => {␊
|
|
2109
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
2110
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
2111
|
+
// what let that happen at all.␊
|
|
2112
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
2113
|
+
for (;;) {␊
|
|
2114
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
2115
|
+
try {␊
|
|
2116
|
+
if (!workPending()) {␊
|
|
2117
|
+
return␊
|
|
2118
|
+
}␊
|
|
2119
|
+
} catch {␊
|
|
2120
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
2121
|
+
// reachable work left. Stop polling and finish.␊
|
|
2122
|
+
return␊
|
|
2123
|
+
}␊
|
|
2124
|
+
}␊
|
|
2125
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
2126
|
+
}␊
|
|
2127
|
+
␊
|
|
1327
2128
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
1328
2129
|
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
1329
2130
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
@@ -1460,6 +2261,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1460
2261
|
}␊
|
|
1461
2262
|
␊
|
|
1462
2263
|
__prepareWasmEnvCleanup()␊
|
|
2264
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
2265
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
2266
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
2267
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
2268
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
2269
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
2270
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
2271
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
2272
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
2273
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
2274
|
+
// rather than skipping it.␊
|
|
2275
|
+
return␊
|
|
2276
|
+
}␊
|
|
1463
2277
|
const result = __emnapiContext.destroy()␊
|
|
1464
2278
|
if (!__isThenable(result)) {␊
|
|
1465
2279
|
__emnapiContextDestroyed = true␊
|
|
@@ -1733,11 +2547,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1733
2547
|
return __finishWasiDisposal()␊
|
|
1734
2548
|
}␊
|
|
1735
2549
|
␊
|
|
1736
|
-
function
|
|
1737
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
1738
|
-
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
1739
|
-
// back to back is what strands them.␊
|
|
1740
|
-
__prepareWasmEnvCleanup()␊
|
|
2550
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
1741
2551
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
1742
2552
|
if (__isThenable(drainResult)) {␊
|
|
1743
2553
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -1745,6 +2555,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1745
2555
|
return __continueWasiDisposal()␊
|
|
1746
2556
|
}␊
|
|
1747
2557
|
␊
|
|
2558
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
2559
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
2560
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
2561
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
2562
|
+
// these back to back is what strands them.␊
|
|
2563
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
2564
|
+
if (__isThenable(prepareResult)) {␊
|
|
2565
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
2566
|
+
}␊
|
|
2567
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
2568
|
+
}␊
|
|
2569
|
+
␊
|
|
1748
2570
|
function __startWasiDisposal() {␊
|
|
1749
2571
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
1750
2572
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -1905,14 +2727,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1905
2727
|
// be reached without the async-work drain below running first.␊
|
|
1906
2728
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
1907
2729
|
const cleanupErrors = []␊
|
|
2730
|
+
let prepareResult␊
|
|
2731
|
+
try {␊
|
|
2732
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
2733
|
+
} catch (cleanupError) {␊
|
|
2734
|
+
cleanupErrors.push(cleanupError)␊
|
|
2735
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2736
|
+
}␊
|
|
2737
|
+
if (__isThenable(prepareResult)) {␊
|
|
2738
|
+
return Promise.resolve(prepareResult).then(␊
|
|
2739
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
2740
|
+
(cleanupError) => {␊
|
|
2741
|
+
cleanupErrors.push(cleanupError)␊
|
|
2742
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2743
|
+
},␊
|
|
2744
|
+
)␊
|
|
2745
|
+
}␊
|
|
2746
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
2747
|
+
}␊
|
|
2748
|
+
␊
|
|
2749
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
2750
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
2751
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
2752
|
+
// did not finish does.␊
|
|
2753
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
1908
2754
|
let drainResult␊
|
|
1909
|
-
let settlementsUnreached = false␊
|
|
1910
2755
|
try {␊
|
|
1911
|
-
__prepareWasmEnvCleanup()␊
|
|
1912
2756
|
drainResult = __drainWasmEnvCleanup()␊
|
|
1913
2757
|
} catch (cleanupError) {␊
|
|
1914
2758
|
cleanupErrors.push(cleanupError)␊
|
|
1915
|
-
|
|
2759
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1916
2760
|
}␊
|
|
1917
2761
|
if (__isThenable(drainResult)) {␊
|
|
1918
2762
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -1923,9 +2767,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1923
2767
|
},␊
|
|
1924
2768
|
)␊
|
|
1925
2769
|
}␊
|
|
1926
|
-
if (settlementsUnreached) {␊
|
|
1927
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1928
|
-
}␊
|
|
1929
2770
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1930
2771
|
}␊
|
|
1931
2772
|
␊
|
|
@@ -1957,6 +2798,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1957
2798
|
return __rollbackWasmEnvForWasiInitialization()␊
|
|
1958
2799
|
}␊
|
|
1959
2800
|
␊
|
|
2801
|
+
// A view of the addon's crash flag, one word of the shared wasm memory, for the␊
|
|
2802
|
+
// pool workers: they raise it when their wasm thread dies, and the shutdown␊
|
|
2803
|
+
// waits inside the cleanup calls on this thread then trap instead of waiting on␊
|
|
2804
|
+
// the dead thread for good. Undefined for an addon built with an older napi.␊
|
|
2805
|
+
let __wasiAddonCrashFlag␊
|
|
2806
|
+
␊
|
|
2807
|
+
function __captureWasiAddonCrashFlag(instance) {␊
|
|
2808
|
+
try {␊
|
|
2809
|
+
const getAddress = instance.exports.napi_wasm_thread_crash_flag_address␊
|
|
2810
|
+
if (typeof getAddress !== 'function') {␊
|
|
2811
|
+
return␊
|
|
2812
|
+
}␊
|
|
2813
|
+
const address = getAddress() >>> 0␊
|
|
2814
|
+
const buffer = __sharedMemory.buffer␊
|
|
2815
|
+
if (address === 0 || address % 4 !== 0 || address + 4 > buffer.byteLength) {␊
|
|
2816
|
+
return␊
|
|
2817
|
+
}␊
|
|
2818
|
+
__wasiAddonCrashFlag = new Int32Array(buffer, address, 1)␊
|
|
2819
|
+
} catch {}␊
|
|
2820
|
+
}␊
|
|
2821
|
+
␊
|
|
2822
|
+
function __shareWasiAddonCrashFlag(worker) {␊
|
|
2823
|
+
if (__wasiAddonCrashFlag === undefined) {␊
|
|
2824
|
+
return␊
|
|
2825
|
+
}␊
|
|
2826
|
+
try {␊
|
|
2827
|
+
worker.postMessage({ __napiRsAddonCrashFlag: __wasiAddonCrashFlag })␊
|
|
2828
|
+
} catch {}␊
|
|
2829
|
+
}␊
|
|
2830
|
+
␊
|
|
1960
2831
|
let __wasiModule␊
|
|
1961
2832
|
let __napiModule␊
|
|
1962
2833
|
␊
|
|
@@ -1987,6 +2858,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1987
2858
|
type: 'module',␊
|
|
1988
2859
|
})␊
|
|
1989
2860
|
__wasiWorkers.add(worker)␊
|
|
2861
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
1990
2862
|
␊
|
|
1991
2863
|
worker.addEventListener('message', (event) => {␊
|
|
1992
2864
|
if (event.data && typeof event.data === 'object' && event.data.type === 'error') {␊
|
|
@@ -2015,6 +2887,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2015
2887
|
},␊
|
|
2016
2888
|
beforeInit({ instance }) {␊
|
|
2017
2889
|
__napiInstance = instance␊
|
|
2890
|
+
__captureWasiAddonCrashFlag(instance)␊
|
|
2891
|
+
for (const worker of __wasiWorkers) {␊
|
|
2892
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
2893
|
+
}␊
|
|
2018
2894
|
for (const name of Object.keys(instance.exports)) {␊
|
|
2019
2895
|
if (name.startsWith('__napi_register__')) {␊
|
|
2020
2896
|
instance.exports[name]()␊
|
|
@@ -2172,6 +3048,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2172
3048
|
let __emnapiContextDestroyPromise␊
|
|
2173
3049
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
2174
3050
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
3051
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
3052
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
3053
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
3054
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
3055
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
3056
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
3057
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
3058
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
3059
|
+
let __finishParkedWasmEnvCleanup␊
|
|
3060
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
3061
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
3062
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
3063
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
3064
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
2175
3065
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
2176
3066
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
2177
3067
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -2284,7 +3174,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2284
3174
|
}␊
|
|
2285
3175
|
␊
|
|
2286
3176
|
function __prepareWasmEnvCleanup() {␊
|
|
2287
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
3177
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
3178
|
+
return␊
|
|
3179
|
+
}␊
|
|
3180
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
3181
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
3182
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
3183
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
3184
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
3185
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
3186
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
3187
|
+
// joined and the workers never drained.␊
|
|
3188
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
3189
|
+
if (finishParked !== undefined) {␊
|
|
3190
|
+
finishParked()␊
|
|
3191
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
3192
|
+
return␊
|
|
3193
|
+
}␊
|
|
3194
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
2288
3195
|
return␊
|
|
2289
3196
|
}␊
|
|
2290
3197
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -2299,10 +3206,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2299
3206
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
2300
3207
|
}␊
|
|
2301
3208
|
__emnapiWasmEnvCleanupRan = true␊
|
|
3209
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
2302
3210
|
}␊
|
|
2303
3211
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
2304
3212
|
}␊
|
|
2305
3213
|
␊
|
|
3214
|
+
/**␊
|
|
3215
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
3216
|
+
* could deliver them.␊
|
|
3217
|
+
*␊
|
|
3218
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
3219
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
3220
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
3221
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
3222
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
3223
|
+
* were for then hang forever, silently.␊
|
|
3224
|
+
*␊
|
|
3225
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
3226
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
3227
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
3228
|
+
* is already unreachable by then.␊
|
|
3229
|
+
*/␊
|
|
3230
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
3231
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
3232
|
+
return␊
|
|
3233
|
+
}␊
|
|
3234
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
3235
|
+
if (typeof pending !== 'function') {␊
|
|
3236
|
+
return␊
|
|
3237
|
+
}␊
|
|
3238
|
+
let queued␊
|
|
3239
|
+
try {␊
|
|
3240
|
+
queued = pending()␊
|
|
3241
|
+
} catch {␊
|
|
3242
|
+
return␊
|
|
3243
|
+
}␊
|
|
3244
|
+
if (!queued) {␊
|
|
3245
|
+
return␊
|
|
3246
|
+
}␊
|
|
3247
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
3248
|
+
try {␊
|
|
3249
|
+
const consoleHost = globalThis.console␊
|
|
3250
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
3251
|
+
consoleHost.error(␊
|
|
3252
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
3253
|
+
queued +␊
|
|
3254
|
+
" queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",␊
|
|
3255
|
+
)␊
|
|
3256
|
+
}␊
|
|
3257
|
+
} catch {}␊
|
|
3258
|
+
}␊
|
|
3259
|
+
␊
|
|
2306
3260
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
2307
3261
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
2308
3262
|
// ahead of it on a faster queue.␊
|
|
@@ -2334,22 +3288,307 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2334
3288
|
}␊
|
|
2335
3289
|
})()␊
|
|
2336
3290
|
␊
|
|
2337
|
-
// A real, *referenced* timer, for waits that must let the whole host make␊
|
|
2338
|
-
// progress between looks — the async-work drain polls the addon rather than␊
|
|
2339
|
-
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there␊
|
|
2340
|
-
// would spin the loop instead of yielding it. Falls back to the macrotask␊
|
|
2341
|
-
// scheduler on a host without timers.␊
|
|
2342
|
-
function __scheduleTimer(callback, delay) {␊
|
|
2343
|
-
const setTimer = globalThis.setTimeout␊
|
|
2344
|
-
if (typeof setTimer !== 'function') {␊
|
|
2345
|
-
__scheduleMacrotask(callback)␊
|
|
3291
|
+
// A real, *referenced* timer, for waits that must let the whole host make␊
|
|
3292
|
+
// progress between looks — the async-work drain polls the addon rather than␊
|
|
3293
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there␊
|
|
3294
|
+
// would spin the loop instead of yielding it. Falls back to the macrotask␊
|
|
3295
|
+
// scheduler on a host without timers.␊
|
|
3296
|
+
function __scheduleTimer(callback, delay) {␊
|
|
3297
|
+
const setTimer = globalThis.setTimeout␊
|
|
3298
|
+
if (typeof setTimer !== 'function') {␊
|
|
3299
|
+
__scheduleMacrotask(callback)␊
|
|
3300
|
+
return␊
|
|
3301
|
+
}␊
|
|
3302
|
+
try {␊
|
|
3303
|
+
setTimer(callback, delay)␊
|
|
3304
|
+
} catch {␊
|
|
3305
|
+
__scheduleMacrotask(callback)␊
|
|
3306
|
+
}␊
|
|
3307
|
+
}␊
|
|
3308
|
+
␊
|
|
3309
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
3310
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
3311
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
3312
|
+
// the loop instead of yielding it.␊
|
|
3313
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
3314
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
3315
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
3316
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
3317
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
3318
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
3319
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
3320
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
3321
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
3322
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
3323
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
3324
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
3325
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
3326
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
3327
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
3328
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
3329
|
+
␊
|
|
3330
|
+
/**␊
|
|
3331
|
+
* Pacing state for one runtime-work poll.␊
|
|
3332
|
+
*␊
|
|
3333
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
3334
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
3335
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
3336
|
+
* instance must not disarm the fallback for the next one.␊
|
|
3337
|
+
*/␊
|
|
3338
|
+
function __createWasmRuntimePollPace() {␊
|
|
3339
|
+
return {␊
|
|
3340
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
3341
|
+
arrivals: 0,␊
|
|
3342
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
3343
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
3344
|
+
settleTurn: undefined,␊
|
|
3345
|
+
turnTimerDueAt: 0,␊
|
|
3346
|
+
}␊
|
|
3347
|
+
}␊
|
|
3348
|
+
␊
|
|
3349
|
+
/**␊
|
|
3350
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
3351
|
+
*␊
|
|
3352
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
3353
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
3354
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
3355
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
3356
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
3357
|
+
*␊
|
|
3358
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
3359
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
3360
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
3361
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
3362
|
+
* the rescue is how far back that newest live backup is:␊
|
|
3363
|
+
*␊
|
|
3364
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
3365
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
3366
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
3367
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
3368
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
3369
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
3370
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
3371
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
3372
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
3373
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
3374
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
3375
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
3376
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
3377
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
3378
|
+
* a throttle last let one through.␊
|
|
3379
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
3380
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
3381
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
3382
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
3383
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
3384
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
3385
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
3386
|
+
* the second.␊
|
|
3387
|
+
*␊
|
|
3388
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
3389
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
3390
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
3391
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
3392
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
3393
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
3394
|
+
*/␊
|
|
3395
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
3396
|
+
const setTimer = globalThis.setTimeout␊
|
|
3397
|
+
if (typeof setTimer !== 'function') {␊
|
|
3398
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
3399
|
+
// already, and that one cannot park.␊
|
|
3400
|
+
return␊
|
|
3401
|
+
}␊
|
|
3402
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
3403
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
3404
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
3405
|
+
let handle␊
|
|
3406
|
+
try {␊
|
|
3407
|
+
handle = setTimer(() => {␊
|
|
3408
|
+
const settleTurn = pace.settleTurn␊
|
|
3409
|
+
if (␊
|
|
3410
|
+
!settleTurn ||␊
|
|
3411
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
3412
|
+
) {␊
|
|
3413
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
3414
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
3415
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
3416
|
+
return␊
|
|
3417
|
+
}␊
|
|
3418
|
+
pace.arrivals = 0␊
|
|
3419
|
+
pace.settleTurn = undefined␊
|
|
3420
|
+
settleTurn()␊
|
|
3421
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
3422
|
+
} catch {␊
|
|
3423
|
+
return␊
|
|
3424
|
+
}␊
|
|
3425
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
3426
|
+
try {␊
|
|
3427
|
+
handle.unref()␊
|
|
3428
|
+
} catch {}␊
|
|
3429
|
+
}␊
|
|
3430
|
+
}␊
|
|
3431
|
+
␊
|
|
3432
|
+
/**␊
|
|
3433
|
+
* One turn of the runtime-work poll.␊
|
|
3434
|
+
*␊
|
|
3435
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
3436
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
3437
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
3438
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
3439
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
3440
|
+
* ever call \`…_finish\`.␊
|
|
3441
|
+
*␊
|
|
3442
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
3443
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
3444
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
3445
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
3446
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
3447
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
3448
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
3449
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
3450
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
3451
|
+
*/␊
|
|
3452
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
3453
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
3454
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
3455
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
3456
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
3457
|
+
return new Promise((resolve) => {␊
|
|
3458
|
+
let settled = false␊
|
|
3459
|
+
const settle = () => {␊
|
|
3460
|
+
if (settled) {␊
|
|
3461
|
+
return␊
|
|
3462
|
+
}␊
|
|
3463
|
+
settled = true␊
|
|
3464
|
+
if (pace.settleTurn === settle) {␊
|
|
3465
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
3466
|
+
// due time this turn has already answered.␊
|
|
3467
|
+
pace.settleTurn = undefined␊
|
|
3468
|
+
}␊
|
|
3469
|
+
resolve()␊
|
|
3470
|
+
}␊
|
|
3471
|
+
__scheduleTimer(() => {␊
|
|
3472
|
+
pace.arrivals++␊
|
|
3473
|
+
settle()␊
|
|
3474
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
3475
|
+
// Read next to the arming it describes; see␊
|
|
3476
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
3477
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
3478
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
3479
|
+
__scheduleMacrotask(settle)␊
|
|
3480
|
+
return␊
|
|
3481
|
+
}␊
|
|
3482
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
3483
|
+
// the timer never arrives.␊
|
|
3484
|
+
pace.settleTurn = settle␊
|
|
3485
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
3486
|
+
})␊
|
|
3487
|
+
}␊
|
|
3488
|
+
␊
|
|
3489
|
+
/**␊
|
|
3490
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
3491
|
+
* event-loop turns in the middle.␊
|
|
3492
|
+
*␊
|
|
3493
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
3494
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
3495
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
3496
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
3497
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
3498
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
3499
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
3500
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
3501
|
+
* entire point.␊
|
|
3502
|
+
*␊
|
|
3503
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
3504
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
3505
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
3506
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
3507
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
3508
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
3509
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
3510
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
3511
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
3512
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
3513
|
+
*␊
|
|
3514
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
3515
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
3516
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
3517
|
+
* an idle disposal synchronous.␊
|
|
3518
|
+
*/␊
|
|
3519
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
3520
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
3521
|
+
return␊
|
|
3522
|
+
}␊
|
|
3523
|
+
const exports = __napiInstance?.exports␊
|
|
3524
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
3525
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
3526
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
3527
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
3528
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
3529
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
3530
|
+
try {␊
|
|
3531
|
+
__prepareWasmEnvCleanup()␊
|
|
3532
|
+
} finally {␊
|
|
3533
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
3534
|
+
}␊
|
|
2346
3535
|
return␊
|
|
2347
3536
|
}␊
|
|
3537
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
3538
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
3539
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
3540
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
3541
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
3542
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
3543
|
+
let live␊
|
|
2348
3544
|
try {␊
|
|
2349
|
-
|
|
2350
|
-
} catch {␊
|
|
2351
|
-
|
|
3545
|
+
live = begin()␊
|
|
3546
|
+
} catch (error) {␊
|
|
3547
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3548
|
+
throw error␊
|
|
3549
|
+
}␊
|
|
3550
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
3551
|
+
const finishCleanup = () => {␊
|
|
3552
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
3553
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
3554
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
3555
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
3556
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
3557
|
+
return␊
|
|
3558
|
+
}␊
|
|
3559
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
3560
|
+
try {␊
|
|
3561
|
+
finish()␊
|
|
3562
|
+
} finally {␊
|
|
3563
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3564
|
+
}␊
|
|
3565
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
2352
3566
|
}␊
|
|
3567
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
3568
|
+
finishCleanup()␊
|
|
3569
|
+
return␊
|
|
3570
|
+
}␊
|
|
3571
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
3572
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
3573
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
3574
|
+
return (async () => {␊
|
|
3575
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
3576
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
3577
|
+
// what let that happen at all.␊
|
|
3578
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
3579
|
+
for (;;) {␊
|
|
3580
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
3581
|
+
try {␊
|
|
3582
|
+
if (!workPending()) {␊
|
|
3583
|
+
return␊
|
|
3584
|
+
}␊
|
|
3585
|
+
} catch {␊
|
|
3586
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
3587
|
+
// reachable work left. Stop polling and finish.␊
|
|
3588
|
+
return␊
|
|
3589
|
+
}␊
|
|
3590
|
+
}␊
|
|
3591
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
2353
3592
|
}␊
|
|
2354
3593
|
␊
|
|
2355
3594
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
@@ -2488,6 +3727,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2488
3727
|
}␊
|
|
2489
3728
|
␊
|
|
2490
3729
|
__prepareWasmEnvCleanup()␊
|
|
3730
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
3731
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
3732
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
3733
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
3734
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
3735
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
3736
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
3737
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
3738
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
3739
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
3740
|
+
// rather than skipping it.␊
|
|
3741
|
+
return␊
|
|
3742
|
+
}␊
|
|
2491
3743
|
const result = __emnapiContext.destroy()␊
|
|
2492
3744
|
if (!__isThenable(result)) {␊
|
|
2493
3745
|
__emnapiContextDestroyed = true␊
|
|
@@ -2761,11 +4013,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2761
4013
|
return __finishWasiDisposal()␊
|
|
2762
4014
|
}␊
|
|
2763
4015
|
␊
|
|
2764
|
-
function
|
|
2765
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
2766
|
-
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
2767
|
-
// back to back is what strands them.␊
|
|
2768
|
-
__prepareWasmEnvCleanup()␊
|
|
4016
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
2769
4017
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
2770
4018
|
if (__isThenable(drainResult)) {␊
|
|
2771
4019
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -2773,6 +4021,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2773
4021
|
return __continueWasiDisposal()␊
|
|
2774
4022
|
}␊
|
|
2775
4023
|
␊
|
|
4024
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
4025
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
4026
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
4027
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
4028
|
+
// these back to back is what strands them.␊
|
|
4029
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
4030
|
+
if (__isThenable(prepareResult)) {␊
|
|
4031
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
4032
|
+
}␊
|
|
4033
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
4034
|
+
}␊
|
|
4035
|
+
␊
|
|
2776
4036
|
function __startWasiDisposal() {␊
|
|
2777
4037
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
2778
4038
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -2933,14 +4193,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2933
4193
|
// be reached without the async-work drain below running first.␊
|
|
2934
4194
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
2935
4195
|
const cleanupErrors = []␊
|
|
4196
|
+
let prepareResult␊
|
|
4197
|
+
try {␊
|
|
4198
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
4199
|
+
} catch (cleanupError) {␊
|
|
4200
|
+
cleanupErrors.push(cleanupError)␊
|
|
4201
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4202
|
+
}␊
|
|
4203
|
+
if (__isThenable(prepareResult)) {␊
|
|
4204
|
+
return Promise.resolve(prepareResult).then(␊
|
|
4205
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
4206
|
+
(cleanupError) => {␊
|
|
4207
|
+
cleanupErrors.push(cleanupError)␊
|
|
4208
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4209
|
+
},␊
|
|
4210
|
+
)␊
|
|
4211
|
+
}␊
|
|
4212
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
4213
|
+
}␊
|
|
4214
|
+
␊
|
|
4215
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
4216
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
4217
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
4218
|
+
// did not finish does.␊
|
|
4219
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
2936
4220
|
let drainResult␊
|
|
2937
|
-
let settlementsUnreached = false␊
|
|
2938
4221
|
try {␊
|
|
2939
|
-
__prepareWasmEnvCleanup()␊
|
|
2940
4222
|
drainResult = __drainWasmEnvCleanup()␊
|
|
2941
4223
|
} catch (cleanupError) {␊
|
|
2942
4224
|
cleanupErrors.push(cleanupError)␊
|
|
2943
|
-
|
|
4225
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2944
4226
|
}␊
|
|
2945
4227
|
if (__isThenable(drainResult)) {␊
|
|
2946
4228
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -2951,9 +4233,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2951
4233
|
},␊
|
|
2952
4234
|
)␊
|
|
2953
4235
|
}␊
|
|
2954
|
-
if (settlementsUnreached) {␊
|
|
2955
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2956
|
-
}␊
|
|
2957
4236
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
2958
4237
|
}␊
|
|
2959
4238
|
␊
|
|
@@ -2985,6 +4264,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2985
4264
|
return __rollbackWasmEnvForWasiInitialization()␊
|
|
2986
4265
|
}␊
|
|
2987
4266
|
␊
|
|
4267
|
+
// A view of the addon's crash flag, one word of the shared wasm memory, for the␊
|
|
4268
|
+
// pool workers: they raise it when their wasm thread dies, and the shutdown␊
|
|
4269
|
+
// waits inside the cleanup calls on this thread then trap instead of waiting on␊
|
|
4270
|
+
// the dead thread for good. Undefined for an addon built with an older napi.␊
|
|
4271
|
+
let __wasiAddonCrashFlag␊
|
|
4272
|
+
␊
|
|
4273
|
+
function __captureWasiAddonCrashFlag(instance) {␊
|
|
4274
|
+
try {␊
|
|
4275
|
+
const getAddress = instance.exports.napi_wasm_thread_crash_flag_address␊
|
|
4276
|
+
if (typeof getAddress !== 'function') {␊
|
|
4277
|
+
return␊
|
|
4278
|
+
}␊
|
|
4279
|
+
const address = getAddress() >>> 0␊
|
|
4280
|
+
const buffer = __sharedMemory.buffer␊
|
|
4281
|
+
if (address === 0 || address % 4 !== 0 || address + 4 > buffer.byteLength) {␊
|
|
4282
|
+
return␊
|
|
4283
|
+
}␊
|
|
4284
|
+
__wasiAddonCrashFlag = new Int32Array(buffer, address, 1)␊
|
|
4285
|
+
} catch {}␊
|
|
4286
|
+
}␊
|
|
4287
|
+
␊
|
|
4288
|
+
function __shareWasiAddonCrashFlag(worker) {␊
|
|
4289
|
+
if (__wasiAddonCrashFlag === undefined) {␊
|
|
4290
|
+
return␊
|
|
4291
|
+
}␊
|
|
4292
|
+
try {␊
|
|
4293
|
+
worker.postMessage({ __napiRsAddonCrashFlag: __wasiAddonCrashFlag })␊
|
|
4294
|
+
} catch {}␊
|
|
4295
|
+
}␊
|
|
4296
|
+
␊
|
|
2988
4297
|
let __wasiModule␊
|
|
2989
4298
|
let __napiModule␊
|
|
2990
4299
|
␊
|
|
@@ -3015,6 +4324,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3015
4324
|
type: 'module',␊
|
|
3016
4325
|
})␊
|
|
3017
4326
|
__wasiWorkers.add(worker)␊
|
|
4327
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
3018
4328
|
worker.addEventListener('message', __wasmCreateOnMessageForFsProxy(__fs))␊
|
|
3019
4329
|
␊
|
|
3020
4330
|
worker.addEventListener('message', (event) => {␊
|
|
@@ -3044,6 +4354,10 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3044
4354
|
},␊
|
|
3045
4355
|
beforeInit({ instance }) {␊
|
|
3046
4356
|
__napiInstance = instance␊
|
|
4357
|
+
__captureWasiAddonCrashFlag(instance)␊
|
|
4358
|
+
for (const worker of __wasiWorkers) {␊
|
|
4359
|
+
__shareWasiAddonCrashFlag(worker)␊
|
|
4360
|
+
}␊
|
|
3047
4361
|
for (const name of Object.keys(instance.exports)) {␊
|
|
3048
4362
|
if (name.startsWith('__napi_register__')) {␊
|
|
3049
4363
|
instance.exports[name]()␊
|
|
@@ -3188,6 +4502,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3188
4502
|
let __emnapiContextDestroyPromise␊
|
|
3189
4503
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
3190
4504
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
4505
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
4506
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
4507
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
4508
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
4509
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
4510
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
4511
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
4512
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
4513
|
+
let __finishParkedWasmEnvCleanup␊
|
|
4514
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
4515
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
4516
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
4517
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
4518
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
3191
4519
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
3192
4520
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
3193
4521
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -3330,7 +4658,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3330
4658
|
}␊
|
|
3331
4659
|
␊
|
|
3332
4660
|
function __prepareWasmEnvCleanup() {␊
|
|
3333
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
4661
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
4662
|
+
return␊
|
|
4663
|
+
}␊
|
|
4664
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
4665
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
4666
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
4667
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
4668
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
4669
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
4670
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
4671
|
+
// joined and the workers never drained.␊
|
|
4672
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
4673
|
+
if (finishParked !== undefined) {␊
|
|
4674
|
+
finishParked()␊
|
|
4675
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
4676
|
+
return␊
|
|
4677
|
+
}␊
|
|
4678
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
3334
4679
|
return␊
|
|
3335
4680
|
}␊
|
|
3336
4681
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -3345,10 +4690,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3345
4690
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3346
4691
|
}␊
|
|
3347
4692
|
__emnapiWasmEnvCleanupRan = true␊
|
|
4693
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
3348
4694
|
}␊
|
|
3349
4695
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
3350
4696
|
}␊
|
|
3351
4697
|
␊
|
|
4698
|
+
/**␊
|
|
4699
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
4700
|
+
* could deliver them.␊
|
|
4701
|
+
*␊
|
|
4702
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
4703
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
4704
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
4705
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
4706
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
4707
|
+
* were for then hang forever, silently.␊
|
|
4708
|
+
*␊
|
|
4709
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
4710
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
4711
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
4712
|
+
* is already unreachable by then.␊
|
|
4713
|
+
*/␊
|
|
4714
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
4715
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
4716
|
+
return␊
|
|
4717
|
+
}␊
|
|
4718
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
4719
|
+
if (typeof pending !== 'function') {␊
|
|
4720
|
+
return␊
|
|
4721
|
+
}␊
|
|
4722
|
+
let queued␊
|
|
4723
|
+
try {␊
|
|
4724
|
+
queued = pending()␊
|
|
4725
|
+
} catch {␊
|
|
4726
|
+
return␊
|
|
4727
|
+
}␊
|
|
4728
|
+
if (!queued) {␊
|
|
4729
|
+
return␊
|
|
4730
|
+
}␊
|
|
4731
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
4732
|
+
try {␊
|
|
4733
|
+
const consoleHost = globalThis.console␊
|
|
4734
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
4735
|
+
consoleHost.error(␊
|
|
4736
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
4737
|
+
queued +␊
|
|
4738
|
+
" queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('napi.rs.wasi.dispose')]() instead: only it yields the event-loop turns the settlements need.",␊
|
|
4739
|
+
)␊
|
|
4740
|
+
}␊
|
|
4741
|
+
} catch {}␊
|
|
4742
|
+
}␊
|
|
4743
|
+
␊
|
|
3352
4744
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
3353
4745
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
3354
4746
|
// ahead of it on a faster queue.␊
|
|
@@ -3398,6 +4790,291 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3398
4790
|
}␊
|
|
3399
4791
|
}␊
|
|
3400
4792
|
␊
|
|
4793
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
4794
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
4795
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
4796
|
+
// the loop instead of yielding it.␊
|
|
4797
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
4798
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
4799
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
4800
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
4801
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
4802
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
4803
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
4804
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
4805
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
4806
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
4807
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
4808
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
4809
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
4810
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
4811
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
4812
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
4813
|
+
␊
|
|
4814
|
+
/**␊
|
|
4815
|
+
* Pacing state for one runtime-work poll.␊
|
|
4816
|
+
*␊
|
|
4817
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
4818
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
4819
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
4820
|
+
* instance must not disarm the fallback for the next one.␊
|
|
4821
|
+
*/␊
|
|
4822
|
+
function __createWasmRuntimePollPace() {␊
|
|
4823
|
+
return {␊
|
|
4824
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
4825
|
+
arrivals: 0,␊
|
|
4826
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
4827
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
4828
|
+
settleTurn: undefined,␊
|
|
4829
|
+
turnTimerDueAt: 0,␊
|
|
4830
|
+
}␊
|
|
4831
|
+
}␊
|
|
4832
|
+
␊
|
|
4833
|
+
/**␊
|
|
4834
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
4835
|
+
*␊
|
|
4836
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
4837
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
4838
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
4839
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
4840
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
4841
|
+
*␊
|
|
4842
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
4843
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
4844
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
4845
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
4846
|
+
* the rescue is how far back that newest live backup is:␊
|
|
4847
|
+
*␊
|
|
4848
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
4849
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
4850
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
4851
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
4852
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
4853
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
4854
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
4855
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
4856
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
4857
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
4858
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
4859
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
4860
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
4861
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
4862
|
+
* a throttle last let one through.␊
|
|
4863
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
4864
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
4865
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
4866
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
4867
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
4868
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
4869
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
4870
|
+
* the second.␊
|
|
4871
|
+
*␊
|
|
4872
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
4873
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
4874
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
4875
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
4876
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
4877
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
4878
|
+
*/␊
|
|
4879
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
4880
|
+
const setTimer = globalThis.setTimeout␊
|
|
4881
|
+
if (typeof setTimer !== 'function') {␊
|
|
4882
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
4883
|
+
// already, and that one cannot park.␊
|
|
4884
|
+
return␊
|
|
4885
|
+
}␊
|
|
4886
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
4887
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
4888
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
4889
|
+
let handle␊
|
|
4890
|
+
try {␊
|
|
4891
|
+
handle = setTimer(() => {␊
|
|
4892
|
+
const settleTurn = pace.settleTurn␊
|
|
4893
|
+
if (␊
|
|
4894
|
+
!settleTurn ||␊
|
|
4895
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
4896
|
+
) {␊
|
|
4897
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
4898
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
4899
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
4900
|
+
return␊
|
|
4901
|
+
}␊
|
|
4902
|
+
pace.arrivals = 0␊
|
|
4903
|
+
pace.settleTurn = undefined␊
|
|
4904
|
+
settleTurn()␊
|
|
4905
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
4906
|
+
} catch {␊
|
|
4907
|
+
return␊
|
|
4908
|
+
}␊
|
|
4909
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
4910
|
+
try {␊
|
|
4911
|
+
handle.unref()␊
|
|
4912
|
+
} catch {}␊
|
|
4913
|
+
}␊
|
|
4914
|
+
}␊
|
|
4915
|
+
␊
|
|
4916
|
+
/**␊
|
|
4917
|
+
* One turn of the runtime-work poll.␊
|
|
4918
|
+
*␊
|
|
4919
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
4920
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
4921
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
4922
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
4923
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
4924
|
+
* ever call \`…_finish\`.␊
|
|
4925
|
+
*␊
|
|
4926
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
4927
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
4928
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
4929
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
4930
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
4931
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
4932
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
4933
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
4934
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
4935
|
+
*/␊
|
|
4936
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
4937
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
4938
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
4939
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
4940
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
4941
|
+
return new Promise((resolve) => {␊
|
|
4942
|
+
let settled = false␊
|
|
4943
|
+
const settle = () => {␊
|
|
4944
|
+
if (settled) {␊
|
|
4945
|
+
return␊
|
|
4946
|
+
}␊
|
|
4947
|
+
settled = true␊
|
|
4948
|
+
if (pace.settleTurn === settle) {␊
|
|
4949
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
4950
|
+
// due time this turn has already answered.␊
|
|
4951
|
+
pace.settleTurn = undefined␊
|
|
4952
|
+
}␊
|
|
4953
|
+
resolve()␊
|
|
4954
|
+
}␊
|
|
4955
|
+
__scheduleTimer(() => {␊
|
|
4956
|
+
pace.arrivals++␊
|
|
4957
|
+
settle()␊
|
|
4958
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
4959
|
+
// Read next to the arming it describes; see␊
|
|
4960
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
4961
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
4962
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
4963
|
+
__scheduleMacrotask(settle)␊
|
|
4964
|
+
return␊
|
|
4965
|
+
}␊
|
|
4966
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
4967
|
+
// the timer never arrives.␊
|
|
4968
|
+
pace.settleTurn = settle␊
|
|
4969
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
4970
|
+
})␊
|
|
4971
|
+
}␊
|
|
4972
|
+
␊
|
|
4973
|
+
/**␊
|
|
4974
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
4975
|
+
* event-loop turns in the middle.␊
|
|
4976
|
+
*␊
|
|
4977
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
4978
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
4979
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
4980
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
4981
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
4982
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
4983
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
4984
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
4985
|
+
* entire point.␊
|
|
4986
|
+
*␊
|
|
4987
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
4988
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
4989
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
4990
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
4991
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
4992
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
4993
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
4994
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
4995
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
4996
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
4997
|
+
*␊
|
|
4998
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
4999
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
5000
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
5001
|
+
* an idle disposal synchronous.␊
|
|
5002
|
+
*/␊
|
|
5003
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
5004
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
5005
|
+
return␊
|
|
5006
|
+
}␊
|
|
5007
|
+
const exports = __napiInstance?.exports␊
|
|
5008
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
5009
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
5010
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
5011
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
5012
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
5013
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
5014
|
+
try {␊
|
|
5015
|
+
__prepareWasmEnvCleanup()␊
|
|
5016
|
+
} finally {␊
|
|
5017
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
5018
|
+
}␊
|
|
5019
|
+
return␊
|
|
5020
|
+
}␊
|
|
5021
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
5022
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
5023
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
5024
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
5025
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
5026
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
5027
|
+
let live␊
|
|
5028
|
+
try {␊
|
|
5029
|
+
live = begin()␊
|
|
5030
|
+
} catch (error) {␊
|
|
5031
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
5032
|
+
throw error␊
|
|
5033
|
+
}␊
|
|
5034
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
5035
|
+
const finishCleanup = () => {␊
|
|
5036
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
5037
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
5038
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
5039
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
5040
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
5041
|
+
return␊
|
|
5042
|
+
}␊
|
|
5043
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
5044
|
+
try {␊
|
|
5045
|
+
finish()␊
|
|
5046
|
+
} finally {␊
|
|
5047
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
5048
|
+
}␊
|
|
5049
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
5050
|
+
}␊
|
|
5051
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
5052
|
+
finishCleanup()␊
|
|
5053
|
+
return␊
|
|
5054
|
+
}␊
|
|
5055
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
5056
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
5057
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
5058
|
+
return (async () => {␊
|
|
5059
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
5060
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
5061
|
+
// what let that happen at all.␊
|
|
5062
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
5063
|
+
for (;;) {␊
|
|
5064
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
5065
|
+
try {␊
|
|
5066
|
+
if (!workPending()) {␊
|
|
5067
|
+
return␊
|
|
5068
|
+
}␊
|
|
5069
|
+
} catch {␊
|
|
5070
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
5071
|
+
// reachable work left. Stop polling and finish.␊
|
|
5072
|
+
return␊
|
|
5073
|
+
}␊
|
|
5074
|
+
}␊
|
|
5075
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
5076
|
+
}␊
|
|
5077
|
+
␊
|
|
3401
5078
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
3402
5079
|
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
3403
5080
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
@@ -3535,6 +5212,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3535
5212
|
␊
|
|
3536
5213
|
__disposeCurrentThreadHosts()␊
|
|
3537
5214
|
__prepareWasmEnvCleanup()␊
|
|
5215
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
5216
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
5217
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
5218
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
5219
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
5220
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
5221
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
5222
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
5223
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
5224
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
5225
|
+
// rather than skipping it.␊
|
|
5226
|
+
return␊
|
|
5227
|
+
}␊
|
|
3538
5228
|
const result = __emnapiContext.destroy()␊
|
|
3539
5229
|
if (!__isThenable(result)) {␊
|
|
3540
5230
|
__emnapiContextDestroyed = true␊
|
|
@@ -3808,11 +5498,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3808
5498
|
return __finishWasiDisposal()␊
|
|
3809
5499
|
}␊
|
|
3810
5500
|
␊
|
|
3811
|
-
function
|
|
3812
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
3813
|
-
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
3814
|
-
// back to back is what strands them.␊
|
|
3815
|
-
__prepareWasmEnvCleanup()␊
|
|
5501
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
3816
5502
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
3817
5503
|
if (__isThenable(drainResult)) {␊
|
|
3818
5504
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -3820,6 +5506,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3820
5506
|
return __continueWasiDisposal()␊
|
|
3821
5507
|
}␊
|
|
3822
5508
|
␊
|
|
5509
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
5510
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
5511
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
5512
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
5513
|
+
// these back to back is what strands them.␊
|
|
5514
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
5515
|
+
if (__isThenable(prepareResult)) {␊
|
|
5516
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
5517
|
+
}␊
|
|
5518
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
5519
|
+
}␊
|
|
5520
|
+
␊
|
|
3823
5521
|
function __startWasiDisposal() {␊
|
|
3824
5522
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
3825
5523
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -3980,14 +5678,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3980
5678
|
// be reached without the async-work drain below running first.␊
|
|
3981
5679
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
3982
5680
|
const cleanupErrors = []␊
|
|
5681
|
+
let prepareResult␊
|
|
5682
|
+
try {␊
|
|
5683
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
5684
|
+
} catch (cleanupError) {␊
|
|
5685
|
+
cleanupErrors.push(cleanupError)␊
|
|
5686
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
5687
|
+
}␊
|
|
5688
|
+
if (__isThenable(prepareResult)) {␊
|
|
5689
|
+
return Promise.resolve(prepareResult).then(␊
|
|
5690
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
5691
|
+
(cleanupError) => {␊
|
|
5692
|
+
cleanupErrors.push(cleanupError)␊
|
|
5693
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
5694
|
+
},␊
|
|
5695
|
+
)␊
|
|
5696
|
+
}␊
|
|
5697
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
5698
|
+
}␊
|
|
5699
|
+
␊
|
|
5700
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
5701
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
5702
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
5703
|
+
// did not finish does.␊
|
|
5704
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
3983
5705
|
let drainResult␊
|
|
3984
|
-
let settlementsUnreached = false␊
|
|
3985
5706
|
try {␊
|
|
3986
|
-
__prepareWasmEnvCleanup()␊
|
|
3987
5707
|
drainResult = __drainWasmEnvCleanup()␊
|
|
3988
5708
|
} catch (cleanupError) {␊
|
|
3989
5709
|
cleanupErrors.push(cleanupError)␊
|
|
3990
|
-
|
|
5710
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
3991
5711
|
}␊
|
|
3992
5712
|
if (__isThenable(drainResult)) {␊
|
|
3993
5713
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -3998,9 +5718,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3998
5718
|
},␊
|
|
3999
5719
|
)␊
|
|
4000
5720
|
}␊
|
|
4001
|
-
if (settlementsUnreached) {␊
|
|
4002
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4003
|
-
}␊
|
|
4004
5721
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
4005
5722
|
}␊
|
|
4006
5723
|
␊
|
|
@@ -4577,10 +6294,222 @@ Generated by [AVA](https://avajs.dev).
|
|
|
4577
6294
|
__scheduleMacrotask(__callback)␊
|
|
4578
6295
|
return␊
|
|
4579
6296
|
}␊
|
|
4580
|
-
try {␊
|
|
4581
|
-
__setTimer(__callback, __delay)␊
|
|
4582
|
-
} catch {␊
|
|
4583
|
-
__scheduleMacrotask(__callback)␊
|
|
6297
|
+
try {␊
|
|
6298
|
+
__setTimer(__callback, __delay)␊
|
|
6299
|
+
} catch {␊
|
|
6300
|
+
__scheduleMacrotask(__callback)␊
|
|
6301
|
+
}␊
|
|
6302
|
+
}␊
|
|
6303
|
+
␊
|
|
6304
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
6305
|
+
// reason the async-work wait uses one: this polls the addon instead of␊
|
|
6306
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
6307
|
+
// the loop instead of yielding it.␊
|
|
6308
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
6309
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
6310
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
6311
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
6312
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
6313
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
6314
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
6315
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
6316
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
6317
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
6318
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
6319
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
6320
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
6321
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
6322
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
6323
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
6324
|
+
␊
|
|
6325
|
+
/**␊
|
|
6326
|
+
* Pacing state for one runtime-work poll.␊
|
|
6327
|
+
*␊
|
|
6328
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
6329
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
6330
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
6331
|
+
* instance must not disarm the fallback for the next one.␊
|
|
6332
|
+
*/␊
|
|
6333
|
+
function __createWasmRuntimePollPace() {␊
|
|
6334
|
+
return {␊
|
|
6335
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
6336
|
+
arrivals: 0,␊
|
|
6337
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
6338
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
6339
|
+
settleTurn: undefined,␊
|
|
6340
|
+
turnTimerDueAt: 0,␊
|
|
6341
|
+
}␊
|
|
6342
|
+
}␊
|
|
6343
|
+
␊
|
|
6344
|
+
/**␊
|
|
6345
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
6346
|
+
*␊
|
|
6347
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
6348
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
6349
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
6350
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
6351
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
6352
|
+
*␊
|
|
6353
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
6354
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
6355
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
6356
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
6357
|
+
* the rescue is how far back that newest live backup is:␊
|
|
6358
|
+
*␊
|
|
6359
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
6360
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
6361
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
6362
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
6363
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
6364
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
6365
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
6366
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
6367
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
6368
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
6369
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
6370
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
6371
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
6372
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
6373
|
+
* a throttle last let one through.␊
|
|
6374
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
6375
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
6376
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
6377
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
6378
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
6379
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
6380
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
6381
|
+
* the second.␊
|
|
6382
|
+
*␊
|
|
6383
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
6384
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
6385
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
6386
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
6387
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
6388
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
6389
|
+
*/␊
|
|
6390
|
+
function __armWasmRuntimePollStallBackup(__pace) {␊
|
|
6391
|
+
const __setTimer = globalThis.setTimeout␊
|
|
6392
|
+
if (typeof __setTimer !== 'function') {␊
|
|
6393
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
6394
|
+
// already, and that one cannot park.␊
|
|
6395
|
+
return␊
|
|
6396
|
+
}␊
|
|
6397
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
6398
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
6399
|
+
const __dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
6400
|
+
let __handle␊
|
|
6401
|
+
try {␊
|
|
6402
|
+
__handle = __setTimer(() => {␊
|
|
6403
|
+
const __settleTurn = __pace.settleTurn␊
|
|
6404
|
+
if (␊
|
|
6405
|
+
!__settleTurn ||␊
|
|
6406
|
+
__pace.turnTimerDueAt > __dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
6407
|
+
) {␊
|
|
6408
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
6409
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
6410
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
6411
|
+
return␊
|
|
6412
|
+
}␊
|
|
6413
|
+
__pace.arrivals = 0␊
|
|
6414
|
+
__pace.settleTurn = undefined␊
|
|
6415
|
+
__settleTurn()␊
|
|
6416
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
6417
|
+
} catch {␊
|
|
6418
|
+
return␊
|
|
6419
|
+
}␊
|
|
6420
|
+
if (__handle && typeof __handle.unref === 'function') {␊
|
|
6421
|
+
try {␊
|
|
6422
|
+
__handle.unref()␊
|
|
6423
|
+
} catch {}␊
|
|
6424
|
+
}␊
|
|
6425
|
+
}␊
|
|
6426
|
+
␊
|
|
6427
|
+
/**␊
|
|
6428
|
+
* One turn of the runtime-work poll.␊
|
|
6429
|
+
*␊
|
|
6430
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
6431
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
6432
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
6433
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
6434
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
6435
|
+
* ever call \`…_finish\`.␊
|
|
6436
|
+
*␊
|
|
6437
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
6438
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
6439
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
6440
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
6441
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
6442
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
6443
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
6444
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
6445
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
6446
|
+
*/␊
|
|
6447
|
+
function __yieldWasmRuntimePollTurn(__pace) {␊
|
|
6448
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
6449
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
6450
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
6451
|
+
__armWasmRuntimePollStallBackup(__pace)␊
|
|
6452
|
+
return new Promise((resolve) => {␊
|
|
6453
|
+
let __settled = false␊
|
|
6454
|
+
const __settle = () => {␊
|
|
6455
|
+
if (__settled) {␊
|
|
6456
|
+
return␊
|
|
6457
|
+
}␊
|
|
6458
|
+
__settled = true␊
|
|
6459
|
+
if (__pace.settleTurn === __settle) {␊
|
|
6460
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
6461
|
+
// due time this turn has already answered.␊
|
|
6462
|
+
__pace.settleTurn = undefined␊
|
|
6463
|
+
}␊
|
|
6464
|
+
resolve()␊
|
|
6465
|
+
}␊
|
|
6466
|
+
__scheduleTimer(() => {␊
|
|
6467
|
+
__pace.arrivals++␊
|
|
6468
|
+
__settle()␊
|
|
6469
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
6470
|
+
// Read next to the arming it describes; see␊
|
|
6471
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
6472
|
+
const __turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
6473
|
+
if (__pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
6474
|
+
__scheduleMacrotask(__settle)␊
|
|
6475
|
+
return␊
|
|
6476
|
+
}␊
|
|
6477
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
6478
|
+
// the timer never arrives.␊
|
|
6479
|
+
__pace.settleTurn = __settle␊
|
|
6480
|
+
__pace.turnTimerDueAt = __turnTimerDueAt␊
|
|
6481
|
+
})␊
|
|
6482
|
+
}␊
|
|
6483
|
+
␊
|
|
6484
|
+
/**␊
|
|
6485
|
+
* Yield event-loop turns until the addon reports its runtime work finished.␊
|
|
6486
|
+
*␊
|
|
6487
|
+
* The window between \`napi_prepare_wasm_env_cleanup_begin\` and␊
|
|
6488
|
+
* \`…_finish\` — the turns are the entire point of splitting the barrier, because␊
|
|
6489
|
+
* on a threaded artifact the work \`…_finish\` joins can itself be waiting for a␊
|
|
6490
|
+
* JavaScript turn from this very thread.␊
|
|
6491
|
+
*␊
|
|
6492
|
+
* Unbounded, for the same reason the async-work wait above is: giving up means␊
|
|
6493
|
+
* calling \`…_finish\`, which joins on this thread, and the work it would join is␊
|
|
6494
|
+
* the work waiting for a turn from this thread — so a bound does not end the␊
|
|
6495
|
+
* wait, it only moves it somewhere the JavaScript thread can no longer be␊
|
|
6496
|
+
* reached. A blocking closure that never returns keeps the disposal promise␊
|
|
6497
|
+
* pending instead. The host contract is in \`crates/async-runtime/README.md\`: a␊
|
|
6498
|
+
* blocking closure must never wait on a JavaScript turn.␊
|
|
6499
|
+
*/␊
|
|
6500
|
+
async function __pollWasmRuntimeWork(__workPending) {␊
|
|
6501
|
+
const __pace = __createWasmRuntimePollPace()␊
|
|
6502
|
+
for (;;) {␊
|
|
6503
|
+
await __yieldWasmRuntimePollTurn(__pace)␊
|
|
6504
|
+
try {␊
|
|
6505
|
+
if (!__workPending()) {␊
|
|
6506
|
+
return␊
|
|
6507
|
+
}␊
|
|
6508
|
+
} catch {␊
|
|
6509
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
6510
|
+
// reachable work left. Stop polling and finish.␊
|
|
6511
|
+
return␊
|
|
6512
|
+
}␊
|
|
4584
6513
|
}␊
|
|
4585
6514
|
}␊
|
|
4586
6515
|
␊
|
|
@@ -4955,7 +6884,9 @@ Generated by [AVA](https://avajs.dev).
|
|
|
4955
6884
|
// hooks unrun. Refuse instead: nothing is flagged, the context stays␊
|
|
4956
6885
|
// registered for managed beforeExit cleanup, and a later destroy still␊
|
|
4957
6886
|
// works. dispose() coalesces reentrancy before it can get here, so this␊
|
|
4958
|
-
// is the backstop for any other caller that manages to
|
|
6887
|
+
// is the backstop for any other caller that manages to. A handshake␊
|
|
6888
|
+
// parked between the two halves of the barrier does not reach here —␊
|
|
6889
|
+
// \`__prepareEnvCleanup\` closes one rather than skipping it.␊
|
|
4959
6890
|
throw __createLifecycleReentryError('dispose')␊
|
|
4960
6891
|
}␊
|
|
4961
6892
|
__result = __emnapiContext.destroy()␊
|
|
@@ -5086,11 +7017,82 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5086
7017
|
let __wasmEnvCleanupRan = false␊
|
|
5087
7018
|
let __wasmEnvCleanupPrepared = false␊
|
|
5088
7019
|
let __wasmEnvCleanupPreparing = false␊
|
|
7020
|
+
// The closer for a barrier parked between \`…_begin\` and \`…_finish\`, set only␊
|
|
7021
|
+
// while that window is open. \`__wasmEnvCleanupPreparing\` cannot tell that␊
|
|
7022
|
+
// apart from a purely synchronous frame, which must not be re-entered and␊
|
|
7023
|
+
// which nothing outside it can finish; this window spans real event-loop␊
|
|
7024
|
+
// turns, so a caller that cannot yield — the managed beforeExit teardown —␊
|
|
7025
|
+
// can land inside it, and can close it. See \`__prepareEnvCleanup\`.␊
|
|
7026
|
+
let __finishParkedEnvCleanup␊
|
|
7027
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
7028
|
+
// queue it leaves behind is expected rather than lost.␊
|
|
7029
|
+
let __wasmEnvCleanupYielding = false␊
|
|
7030
|
+
let __wasmEnvSettlementLossReported = false␊
|
|
5089
7031
|
let __wasmEnvCleanupDrained = false␊
|
|
5090
7032
|
let __wasmEnvCleanupDrainPromise␊
|
|
5091
7033
|
const __isPreparingEnvCleanup = () => __wasmEnvCleanupPreparing␊
|
|
7034
|
+
/**␊
|
|
7035
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
7036
|
+
* could deliver them.␊
|
|
7037
|
+
*␊
|
|
7038
|
+
* Only \`dispose()\` and the initialization rollback yield the event-loop turns␊
|
|
7039
|
+
* @emnapi/core needs to dispatch its queue. Every other caller of the barrier␊
|
|
7040
|
+
* destroys in the same turn — a raw \`Context.destroy()\`, the managed␊
|
|
7041
|
+
* beforeExit teardown — and \`Context.destroy()\` runs the threadsafe␊
|
|
7042
|
+
* function's cleanup hook, which drains that queue with a null env and␊
|
|
7043
|
+
* discards it. The promises those settlements were for then hang forever,␊
|
|
7044
|
+
* silently.␊
|
|
7045
|
+
*␊
|
|
7046
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
7047
|
+
* where throwing would take the whole teardown down with it. Destroying␊
|
|
7048
|
+
* anyway is still the right trade — the queue is already unreachable by then.␊
|
|
7049
|
+
*/␊
|
|
7050
|
+
const __reportUnreachedSettlements = () => {␊
|
|
7051
|
+
if (__wasmEnvCleanupYielding || __wasmEnvSettlementLossReported) {␊
|
|
7052
|
+
return␊
|
|
7053
|
+
}␊
|
|
7054
|
+
const __pending = __napiInstance?.exports.napi_wasm_env_cleanup_pending␊
|
|
7055
|
+
if (typeof __pending !== 'function') {␊
|
|
7056
|
+
return␊
|
|
7057
|
+
}␊
|
|
7058
|
+
let __queued␊
|
|
7059
|
+
try {␊
|
|
7060
|
+
__queued = __pending()␊
|
|
7061
|
+
} catch {␊
|
|
7062
|
+
return␊
|
|
7063
|
+
}␊
|
|
7064
|
+
if (!__queued) {␊
|
|
7065
|
+
return␊
|
|
7066
|
+
}␊
|
|
7067
|
+
__wasmEnvSettlementLossReported = true␊
|
|
7068
|
+
try {␊
|
|
7069
|
+
const __consoleHost = globalThis.console␊
|
|
7070
|
+
if (__consoleHost && typeof __consoleHost.error === 'function') {␊
|
|
7071
|
+
__consoleHost.error(␊
|
|
7072
|
+
'napi-rs: the wasm environment is being destroyed with ' +␊
|
|
7073
|
+
__queued +␊
|
|
7074
|
+
' queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose the instance instead: only dispose() yields the event-loop turns the settlements need.',␊
|
|
7075
|
+
)␊
|
|
7076
|
+
}␊
|
|
7077
|
+
} catch {}␊
|
|
7078
|
+
}␊
|
|
5092
7079
|
const __prepareEnvCleanup = () => {␊
|
|
5093
|
-
if (__wasmEnvCleanupPrepared
|
|
7080
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
7081
|
+
return␊
|
|
7082
|
+
}␊
|
|
7083
|
+
// A handshake parked between its two halves is one this frame can close,␊
|
|
7084
|
+
// and must: every caller of this is about to destroy the context, and the␊
|
|
7085
|
+
// turns the poll is waiting for will not come. Closing it runs \`…_finish\`,␊
|
|
7086
|
+
// which is the call that joins, so this degrades to exactly the single␊
|
|
7087
|
+
// call below. Leaving it open destroys the context with the barrier still␊
|
|
7088
|
+
// raised and the runtime never joined.␊
|
|
7089
|
+
const __finishParked = __finishParkedEnvCleanup␊
|
|
7090
|
+
if (__finishParked !== undefined) {␊
|
|
7091
|
+
__finishParked()␊
|
|
7092
|
+
__reportUnreachedSettlements()␊
|
|
7093
|
+
return␊
|
|
7094
|
+
}␊
|
|
7095
|
+
if (__wasmEnvCleanupPreparing) {␊
|
|
5094
7096
|
return␊
|
|
5095
7097
|
}␊
|
|
5096
7098
|
const __prepareWasmEnvCleanup =␊
|
|
@@ -5106,9 +7108,86 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5106
7108
|
__wasmEnvCleanupPreparing = false␊
|
|
5107
7109
|
}␊
|
|
5108
7110
|
__wasmEnvCleanupRan = true␊
|
|
7111
|
+
__reportUnreachedSettlements()␊
|
|
5109
7112
|
}␊
|
|
5110
7113
|
__wasmEnvCleanupPrepared = true␊
|
|
5111
7114
|
}␊
|
|
7115
|
+
/**␊
|
|
7116
|
+
* The barrier for the callers that can yield: \`__prepareEnvCleanup\` with real␊
|
|
7117
|
+
* event-loop turns in the middle.␊
|
|
7118
|
+
*␊
|
|
7119
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
7120
|
+
* async runtime has quiesced, and the work it waits for can itself be waiting␊
|
|
7121
|
+
* for a JavaScript turn from this thread. The addon's two-phase form splits␊
|
|
7122
|
+
* that: \`…_begin\` stops the runtime without joining and reports whether␊
|
|
7123
|
+
* anything is still live, \`napi_wasm_runtime_work_pending\` answers that again␊
|
|
7124
|
+
* without blocking, and \`…_finish\` joins.␊
|
|
7125
|
+
*␊
|
|
7126
|
+
* The poll is unbounded — see \`__pollWasmRuntimeWork\` — but a caller that␊
|
|
7127
|
+
* cannot yield closes the handshake itself rather than waiting for it, so␊
|
|
7128
|
+
* \`…_finish\` still runs on every teardown path. Feature-detected like every␊
|
|
7129
|
+
* other export here, and returns nothing whenever the handshake finished␊
|
|
7130
|
+
* without yielding.␊
|
|
7131
|
+
*/␊
|
|
7132
|
+
const __prepareEnvCleanupWithTurns = () => {␊
|
|
7133
|
+
if (__wasmEnvCleanupPrepared || __wasmEnvCleanupPreparing) {␊
|
|
7134
|
+
return␊
|
|
7135
|
+
}␊
|
|
7136
|
+
const __exports = __napiInstance?.exports␊
|
|
7137
|
+
const __begin = __exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
7138
|
+
const __finish = __exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
7139
|
+
if (typeof __begin !== 'function' || typeof __finish !== 'function') {␊
|
|
7140
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
7141
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
7142
|
+
__wasmEnvCleanupYielding = true␊
|
|
7143
|
+
try {␊
|
|
7144
|
+
__prepareEnvCleanup()␊
|
|
7145
|
+
} finally {␊
|
|
7146
|
+
__wasmEnvCleanupYielding = false␊
|
|
7147
|
+
}␊
|
|
7148
|
+
return␊
|
|
7149
|
+
}␊
|
|
7150
|
+
const __workPending = __exports?.napi_wasm_runtime_work_pending␊
|
|
7151
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
7152
|
+
// from one of the JavaScript handlers they run is the same no-op it is␊
|
|
7153
|
+
// inside the single call: the barrier is up and the runtime is␊
|
|
7154
|
+
// mid-teardown, and destroying between the halves would strand exactly what␊
|
|
7155
|
+
// this delivers.␊
|
|
7156
|
+
__wasmEnvCleanupPreparing = true␊
|
|
7157
|
+
let __live␊
|
|
7158
|
+
try {␊
|
|
7159
|
+
__live = __begin()␊
|
|
7160
|
+
} catch (__error) {␊
|
|
7161
|
+
__wasmEnvCleanupPreparing = false␊
|
|
7162
|
+
throw __error␊
|
|
7163
|
+
}␊
|
|
7164
|
+
__wasmEnvCleanupRan = true␊
|
|
7165
|
+
const __finishEnvCleanup = () => {␊
|
|
7166
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
7167
|
+
// Already closed by a caller that could not yield. \`…_finish\` is␊
|
|
7168
|
+
// idempotent, but the flags it lowers are not.␊
|
|
7169
|
+
return␊
|
|
7170
|
+
}␊
|
|
7171
|
+
__finishParkedEnvCleanup = undefined␊
|
|
7172
|
+
try {␊
|
|
7173
|
+
__finish()␊
|
|
7174
|
+
} finally {␊
|
|
7175
|
+
__wasmEnvCleanupPreparing = false␊
|
|
7176
|
+
}␊
|
|
7177
|
+
__wasmEnvCleanupPrepared = true␊
|
|
7178
|
+
}␊
|
|
7179
|
+
if (!__live || typeof __workPending !== 'function') {␊
|
|
7180
|
+
__finishEnvCleanup()␊
|
|
7181
|
+
return␊
|
|
7182
|
+
}␊
|
|
7183
|
+
// Publish the closer before yielding: from here until \`__finishEnvCleanup\`␊
|
|
7184
|
+
// runs, a caller that cannot yield is entitled to end this handshake.␊
|
|
7185
|
+
__finishParkedEnvCleanup = __finishEnvCleanup␊
|
|
7186
|
+
return __pollWasmRuntimeWork(__workPending).then(␊
|
|
7187
|
+
__finishEnvCleanup,␊
|
|
7188
|
+
__finishEnvCleanup,␊
|
|
7189
|
+
)␊
|
|
7190
|
+
}␊
|
|
5112
7191
|
// The barrier + settlement drain, hoisted out of the context destroyer so the␊
|
|
5113
7192
|
// drain can yield without widening the destroyer's reentry window. Both␊
|
|
5114
7193
|
// yielding paths run it — dispose() and the initialization-failure rollback.␊
|
|
@@ -5121,6 +7200,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5121
7200
|
// is enough — and dispose() stays retryable after it rejects, so marking the␊
|
|
5122
7201
|
// drain complete up front would make the retry skip it and destroy the context␊
|
|
5123
7202
|
// with the barrier's settlements still queued.␊
|
|
7203
|
+
const __drainAfterEnvCleanup = () => {␊
|
|
7204
|
+
if (!__wasmEnvCleanupRan) {␊
|
|
7205
|
+
return␊
|
|
7206
|
+
}␊
|
|
7207
|
+
const __drained = __drainWasmEnvCleanup(__napiInstance)␊
|
|
7208
|
+
if (!__drained || typeof __drained.then !== 'function') {␊
|
|
7209
|
+
__wasmEnvCleanupDrained = true␊
|
|
7210
|
+
return␊
|
|
7211
|
+
}␊
|
|
7212
|
+
return __drained.then((__value) => {␊
|
|
7213
|
+
__wasmEnvCleanupDrained = true␊
|
|
7214
|
+
return __value␊
|
|
7215
|
+
})␊
|
|
7216
|
+
}␊
|
|
5124
7217
|
const __prepareForDisposal = () => {␊
|
|
5125
7218
|
if (__wasmEnvCleanupDrained) {␊
|
|
5126
7219
|
return␊
|
|
@@ -5128,18 +7221,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5128
7221
|
if (__wasmEnvCleanupDrainPromise) {␊
|
|
5129
7222
|
return __wasmEnvCleanupDrainPromise␊
|
|
5130
7223
|
}␊
|
|
5131
|
-
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
const
|
|
5136
|
-
|
|
5137
|
-
|
|
7224
|
+
// The barrier itself can yield now, so the memo below has to cover it too:␊
|
|
7225
|
+
// a reentrant caller must join this handshake rather than start a second␊
|
|
7226
|
+
// one while the first is parked between the two halves.␊
|
|
7227
|
+
const __prepared = __prepareEnvCleanupWithTurns()␊
|
|
7228
|
+
const __settled =␊
|
|
7229
|
+
__prepared && typeof __prepared.then === 'function'␊
|
|
7230
|
+
? __prepared.then(__drainAfterEnvCleanup)␊
|
|
7231
|
+
: __drainAfterEnvCleanup()␊
|
|
7232
|
+
if (!__settled || typeof __settled.then !== 'function') {␊
|
|
5138
7233
|
return␊
|
|
5139
7234
|
}␊
|
|
5140
|
-
const __tracked =
|
|
7235
|
+
const __tracked = __settled.then(␊
|
|
5141
7236
|
(__value) => {␊
|
|
5142
|
-
__wasmEnvCleanupDrained = true␊
|
|
5143
7237
|
__wasmEnvCleanupDrainPromise = undefined␊
|
|
5144
7238
|
return __value␊
|
|
5145
7239
|
},␊
|
|
@@ -6082,6 +8176,218 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6082
8176
|
}␊
|
|
6083
8177
|
}␊
|
|
6084
8178
|
␊
|
|
8179
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
8180
|
+
// reason the async-work wait uses one: this polls the addon instead of␊
|
|
8181
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
8182
|
+
// the loop instead of yielding it.␊
|
|
8183
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
8184
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
8185
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
8186
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
8187
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
8188
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
8189
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
8190
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
8191
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
8192
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
8193
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
8194
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
8195
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
8196
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
8197
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
8198
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
8199
|
+
␊
|
|
8200
|
+
/**␊
|
|
8201
|
+
* Pacing state for one runtime-work poll.␊
|
|
8202
|
+
*␊
|
|
8203
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
8204
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
8205
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
8206
|
+
* instance must not disarm the fallback for the next one.␊
|
|
8207
|
+
*/␊
|
|
8208
|
+
function __createWasmRuntimePollPace() {␊
|
|
8209
|
+
return {␊
|
|
8210
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
8211
|
+
arrivals: 0,␊
|
|
8212
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
8213
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
8214
|
+
settleTurn: undefined,␊
|
|
8215
|
+
turnTimerDueAt: 0,␊
|
|
8216
|
+
}␊
|
|
8217
|
+
}␊
|
|
8218
|
+
␊
|
|
8219
|
+
/**␊
|
|
8220
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
8221
|
+
*␊
|
|
8222
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
8223
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
8224
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
8225
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
8226
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
8227
|
+
*␊
|
|
8228
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
8229
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
8230
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
8231
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
8232
|
+
* the rescue is how far back that newest live backup is:␊
|
|
8233
|
+
*␊
|
|
8234
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
8235
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
8236
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
8237
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
8238
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
8239
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
8240
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
8241
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
8242
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
8243
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
8244
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
8245
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
8246
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
8247
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
8248
|
+
* a throttle last let one through.␊
|
|
8249
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
8250
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
8251
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
8252
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
8253
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
8254
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
8255
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
8256
|
+
* the second.␊
|
|
8257
|
+
*␊
|
|
8258
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
8259
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
8260
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
8261
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
8262
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
8263
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
8264
|
+
*/␊
|
|
8265
|
+
function __armWasmRuntimePollStallBackup(__pace) {␊
|
|
8266
|
+
const __setTimer = globalThis.setTimeout␊
|
|
8267
|
+
if (typeof __setTimer !== 'function') {␊
|
|
8268
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
8269
|
+
// already, and that one cannot park.␊
|
|
8270
|
+
return␊
|
|
8271
|
+
}␊
|
|
8272
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
8273
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
8274
|
+
const __dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
8275
|
+
let __handle␊
|
|
8276
|
+
try {␊
|
|
8277
|
+
__handle = __setTimer(() => {␊
|
|
8278
|
+
const __settleTurn = __pace.settleTurn␊
|
|
8279
|
+
if (␊
|
|
8280
|
+
!__settleTurn ||␊
|
|
8281
|
+
__pace.turnTimerDueAt > __dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
8282
|
+
) {␊
|
|
8283
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
8284
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
8285
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
8286
|
+
return␊
|
|
8287
|
+
}␊
|
|
8288
|
+
__pace.arrivals = 0␊
|
|
8289
|
+
__pace.settleTurn = undefined␊
|
|
8290
|
+
__settleTurn()␊
|
|
8291
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
8292
|
+
} catch {␊
|
|
8293
|
+
return␊
|
|
8294
|
+
}␊
|
|
8295
|
+
if (__handle && typeof __handle.unref === 'function') {␊
|
|
8296
|
+
try {␊
|
|
8297
|
+
__handle.unref()␊
|
|
8298
|
+
} catch {}␊
|
|
8299
|
+
}␊
|
|
8300
|
+
}␊
|
|
8301
|
+
␊
|
|
8302
|
+
/**␊
|
|
8303
|
+
* One turn of the runtime-work poll.␊
|
|
8304
|
+
*␊
|
|
8305
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
8306
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
8307
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
8308
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
8309
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
8310
|
+
* ever call \`…_finish\`.␊
|
|
8311
|
+
*␊
|
|
8312
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
8313
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
8314
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
8315
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
8316
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
8317
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
8318
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
8319
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
8320
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
8321
|
+
*/␊
|
|
8322
|
+
function __yieldWasmRuntimePollTurn(__pace) {␊
|
|
8323
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
8324
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
8325
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
8326
|
+
__armWasmRuntimePollStallBackup(__pace)␊
|
|
8327
|
+
return new Promise((resolve) => {␊
|
|
8328
|
+
let __settled = false␊
|
|
8329
|
+
const __settle = () => {␊
|
|
8330
|
+
if (__settled) {␊
|
|
8331
|
+
return␊
|
|
8332
|
+
}␊
|
|
8333
|
+
__settled = true␊
|
|
8334
|
+
if (__pace.settleTurn === __settle) {␊
|
|
8335
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
8336
|
+
// due time this turn has already answered.␊
|
|
8337
|
+
__pace.settleTurn = undefined␊
|
|
8338
|
+
}␊
|
|
8339
|
+
resolve()␊
|
|
8340
|
+
}␊
|
|
8341
|
+
__scheduleTimer(() => {␊
|
|
8342
|
+
__pace.arrivals++␊
|
|
8343
|
+
__settle()␊
|
|
8344
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
8345
|
+
// Read next to the arming it describes; see␊
|
|
8346
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
8347
|
+
const __turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
8348
|
+
if (__pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
8349
|
+
__scheduleMacrotask(__settle)␊
|
|
8350
|
+
return␊
|
|
8351
|
+
}␊
|
|
8352
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
8353
|
+
// the timer never arrives.␊
|
|
8354
|
+
__pace.settleTurn = __settle␊
|
|
8355
|
+
__pace.turnTimerDueAt = __turnTimerDueAt␊
|
|
8356
|
+
})␊
|
|
8357
|
+
}␊
|
|
8358
|
+
␊
|
|
8359
|
+
/**␊
|
|
8360
|
+
* Yield event-loop turns until the addon reports its runtime work finished.␊
|
|
8361
|
+
*␊
|
|
8362
|
+
* The window between \`napi_prepare_wasm_env_cleanup_begin\` and␊
|
|
8363
|
+
* \`…_finish\` — the turns are the entire point of splitting the barrier, because␊
|
|
8364
|
+
* on a threaded artifact the work \`…_finish\` joins can itself be waiting for a␊
|
|
8365
|
+
* JavaScript turn from this very thread.␊
|
|
8366
|
+
*␊
|
|
8367
|
+
* Unbounded, for the same reason the async-work wait above is: giving up means␊
|
|
8368
|
+
* calling \`…_finish\`, which joins on this thread, and the work it would join is␊
|
|
8369
|
+
* the work waiting for a turn from this thread — so a bound does not end the␊
|
|
8370
|
+
* wait, it only moves it somewhere the JavaScript thread can no longer be␊
|
|
8371
|
+
* reached. A blocking closure that never returns keeps the disposal promise␊
|
|
8372
|
+
* pending instead. The host contract is in \`crates/async-runtime/README.md\`: a␊
|
|
8373
|
+
* blocking closure must never wait on a JavaScript turn.␊
|
|
8374
|
+
*/␊
|
|
8375
|
+
async function __pollWasmRuntimeWork(__workPending) {␊
|
|
8376
|
+
const __pace = __createWasmRuntimePollPace()␊
|
|
8377
|
+
for (;;) {␊
|
|
8378
|
+
await __yieldWasmRuntimePollTurn(__pace)␊
|
|
8379
|
+
try {␊
|
|
8380
|
+
if (!__workPending()) {␊
|
|
8381
|
+
return␊
|
|
8382
|
+
}␊
|
|
8383
|
+
} catch {␊
|
|
8384
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
8385
|
+
// reachable work left. Stop polling and finish.␊
|
|
8386
|
+
return␊
|
|
8387
|
+
}␊
|
|
8388
|
+
}␊
|
|
8389
|
+
}␊
|
|
8390
|
+
␊
|
|
6085
8391
|
// How often to re-read \`napi_wasm_async_work_pending\` while waiting. The wait␊
|
|
6086
8392
|
// ends when the addon reports zero, so this only decides how promptly disposal␊
|
|
6087
8393
|
// notices — not how long it waits.␊
|
|
@@ -6454,7 +8760,9 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6454
8760
|
// hooks unrun. Refuse instead: nothing is flagged, the context stays␊
|
|
6455
8761
|
// registered for managed beforeExit cleanup, and a later destroy still␊
|
|
6456
8762
|
// works. dispose() coalesces reentrancy before it can get here, so this␊
|
|
6457
|
-
// is the backstop for any other caller that manages to
|
|
8763
|
+
// is the backstop for any other caller that manages to. A handshake␊
|
|
8764
|
+
// parked between the two halves of the barrier does not reach here —␊
|
|
8765
|
+
// \`__prepareEnvCleanup\` closes one rather than skipping it.␊
|
|
6458
8766
|
throw __createLifecycleReentryError('dispose')␊
|
|
6459
8767
|
}␊
|
|
6460
8768
|
__disposeHosts?.()␊
|
|
@@ -6605,11 +8913,82 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6605
8913
|
let __wasmEnvCleanupRan = false␊
|
|
6606
8914
|
let __wasmEnvCleanupPrepared = false␊
|
|
6607
8915
|
let __wasmEnvCleanupPreparing = false␊
|
|
8916
|
+
// The closer for a barrier parked between \`…_begin\` and \`…_finish\`, set only␊
|
|
8917
|
+
// while that window is open. \`__wasmEnvCleanupPreparing\` cannot tell that␊
|
|
8918
|
+
// apart from a purely synchronous frame, which must not be re-entered and␊
|
|
8919
|
+
// which nothing outside it can finish; this window spans real event-loop␊
|
|
8920
|
+
// turns, so a caller that cannot yield — the managed beforeExit teardown —␊
|
|
8921
|
+
// can land inside it, and can close it. See \`__prepareEnvCleanup\`.␊
|
|
8922
|
+
let __finishParkedEnvCleanup␊
|
|
8923
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
8924
|
+
// queue it leaves behind is expected rather than lost.␊
|
|
8925
|
+
let __wasmEnvCleanupYielding = false␊
|
|
8926
|
+
let __wasmEnvSettlementLossReported = false␊
|
|
6608
8927
|
let __wasmEnvCleanupDrained = false␊
|
|
6609
8928
|
let __wasmEnvCleanupDrainPromise␊
|
|
6610
8929
|
const __isPreparingEnvCleanup = () => __wasmEnvCleanupPreparing␊
|
|
8930
|
+
/**␊
|
|
8931
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
8932
|
+
* could deliver them.␊
|
|
8933
|
+
*␊
|
|
8934
|
+
* Only \`dispose()\` and the initialization rollback yield the event-loop turns␊
|
|
8935
|
+
* @emnapi/core needs to dispatch its queue. Every other caller of the barrier␊
|
|
8936
|
+
* destroys in the same turn — a raw \`Context.destroy()\`, the managed␊
|
|
8937
|
+
* beforeExit teardown — and \`Context.destroy()\` runs the threadsafe␊
|
|
8938
|
+
* function's cleanup hook, which drains that queue with a null env and␊
|
|
8939
|
+
* discards it. The promises those settlements were for then hang forever,␊
|
|
8940
|
+
* silently.␊
|
|
8941
|
+
*␊
|
|
8942
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
8943
|
+
* where throwing would take the whole teardown down with it. Destroying␊
|
|
8944
|
+
* anyway is still the right trade — the queue is already unreachable by then.␊
|
|
8945
|
+
*/␊
|
|
8946
|
+
const __reportUnreachedSettlements = () => {␊
|
|
8947
|
+
if (__wasmEnvCleanupYielding || __wasmEnvSettlementLossReported) {␊
|
|
8948
|
+
return␊
|
|
8949
|
+
}␊
|
|
8950
|
+
const __pending = __napiInstance?.exports.napi_wasm_env_cleanup_pending␊
|
|
8951
|
+
if (typeof __pending !== 'function') {␊
|
|
8952
|
+
return␊
|
|
8953
|
+
}␊
|
|
8954
|
+
let __queued␊
|
|
8955
|
+
try {␊
|
|
8956
|
+
__queued = __pending()␊
|
|
8957
|
+
} catch {␊
|
|
8958
|
+
return␊
|
|
8959
|
+
}␊
|
|
8960
|
+
if (!__queued) {␊
|
|
8961
|
+
return␊
|
|
8962
|
+
}␊
|
|
8963
|
+
__wasmEnvSettlementLossReported = true␊
|
|
8964
|
+
try {␊
|
|
8965
|
+
const __consoleHost = globalThis.console␊
|
|
8966
|
+
if (__consoleHost && typeof __consoleHost.error === 'function') {␊
|
|
8967
|
+
__consoleHost.error(␊
|
|
8968
|
+
'napi-rs: the wasm environment is being destroyed with ' +␊
|
|
8969
|
+
__queued +␊
|
|
8970
|
+
' queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose the instance instead: only dispose() yields the event-loop turns the settlements need.',␊
|
|
8971
|
+
)␊
|
|
8972
|
+
}␊
|
|
8973
|
+
} catch {}␊
|
|
8974
|
+
}␊
|
|
6611
8975
|
const __prepareEnvCleanup = () => {␊
|
|
6612
|
-
if (__wasmEnvCleanupPrepared
|
|
8976
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
8977
|
+
return␊
|
|
8978
|
+
}␊
|
|
8979
|
+
// A handshake parked between its two halves is one this frame can close,␊
|
|
8980
|
+
// and must: every caller of this is about to destroy the context, and the␊
|
|
8981
|
+
// turns the poll is waiting for will not come. Closing it runs \`…_finish\`,␊
|
|
8982
|
+
// which is the call that joins, so this degrades to exactly the single␊
|
|
8983
|
+
// call below. Leaving it open destroys the context with the barrier still␊
|
|
8984
|
+
// raised and the runtime never joined.␊
|
|
8985
|
+
const __finishParked = __finishParkedEnvCleanup␊
|
|
8986
|
+
if (__finishParked !== undefined) {␊
|
|
8987
|
+
__finishParked()␊
|
|
8988
|
+
__reportUnreachedSettlements()␊
|
|
8989
|
+
return␊
|
|
8990
|
+
}␊
|
|
8991
|
+
if (__wasmEnvCleanupPreparing) {␊
|
|
6613
8992
|
return␊
|
|
6614
8993
|
}␊
|
|
6615
8994
|
const __prepareWasmEnvCleanup =␊
|
|
@@ -6625,9 +9004,86 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6625
9004
|
__wasmEnvCleanupPreparing = false␊
|
|
6626
9005
|
}␊
|
|
6627
9006
|
__wasmEnvCleanupRan = true␊
|
|
9007
|
+
__reportUnreachedSettlements()␊
|
|
6628
9008
|
}␊
|
|
6629
9009
|
__wasmEnvCleanupPrepared = true␊
|
|
6630
9010
|
}␊
|
|
9011
|
+
/**␊
|
|
9012
|
+
* The barrier for the callers that can yield: \`__prepareEnvCleanup\` with real␊
|
|
9013
|
+
* event-loop turns in the middle.␊
|
|
9014
|
+
*␊
|
|
9015
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
9016
|
+
* async runtime has quiesced, and the work it waits for can itself be waiting␊
|
|
9017
|
+
* for a JavaScript turn from this thread. The addon's two-phase form splits␊
|
|
9018
|
+
* that: \`…_begin\` stops the runtime without joining and reports whether␊
|
|
9019
|
+
* anything is still live, \`napi_wasm_runtime_work_pending\` answers that again␊
|
|
9020
|
+
* without blocking, and \`…_finish\` joins.␊
|
|
9021
|
+
*␊
|
|
9022
|
+
* The poll is unbounded — see \`__pollWasmRuntimeWork\` — but a caller that␊
|
|
9023
|
+
* cannot yield closes the handshake itself rather than waiting for it, so␊
|
|
9024
|
+
* \`…_finish\` still runs on every teardown path. Feature-detected like every␊
|
|
9025
|
+
* other export here, and returns nothing whenever the handshake finished␊
|
|
9026
|
+
* without yielding.␊
|
|
9027
|
+
*/␊
|
|
9028
|
+
const __prepareEnvCleanupWithTurns = () => {␊
|
|
9029
|
+
if (__wasmEnvCleanupPrepared || __wasmEnvCleanupPreparing) {␊
|
|
9030
|
+
return␊
|
|
9031
|
+
}␊
|
|
9032
|
+
const __exports = __napiInstance?.exports␊
|
|
9033
|
+
const __begin = __exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
9034
|
+
const __finish = __exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
9035
|
+
if (typeof __begin !== 'function' || typeof __finish !== 'function') {␊
|
|
9036
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
9037
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
9038
|
+
__wasmEnvCleanupYielding = true␊
|
|
9039
|
+
try {␊
|
|
9040
|
+
__prepareEnvCleanup()␊
|
|
9041
|
+
} finally {␊
|
|
9042
|
+
__wasmEnvCleanupYielding = false␊
|
|
9043
|
+
}␊
|
|
9044
|
+
return␊
|
|
9045
|
+
}␊
|
|
9046
|
+
const __workPending = __exports?.napi_wasm_runtime_work_pending␊
|
|
9047
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
9048
|
+
// from one of the JavaScript handlers they run is the same no-op it is␊
|
|
9049
|
+
// inside the single call: the barrier is up and the runtime is␊
|
|
9050
|
+
// mid-teardown, and destroying between the halves would strand exactly what␊
|
|
9051
|
+
// this delivers.␊
|
|
9052
|
+
__wasmEnvCleanupPreparing = true␊
|
|
9053
|
+
let __live␊
|
|
9054
|
+
try {␊
|
|
9055
|
+
__live = __begin()␊
|
|
9056
|
+
} catch (__error) {␊
|
|
9057
|
+
__wasmEnvCleanupPreparing = false␊
|
|
9058
|
+
throw __error␊
|
|
9059
|
+
}␊
|
|
9060
|
+
__wasmEnvCleanupRan = true␊
|
|
9061
|
+
const __finishEnvCleanup = () => {␊
|
|
9062
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
9063
|
+
// Already closed by a caller that could not yield. \`…_finish\` is␊
|
|
9064
|
+
// idempotent, but the flags it lowers are not.␊
|
|
9065
|
+
return␊
|
|
9066
|
+
}␊
|
|
9067
|
+
__finishParkedEnvCleanup = undefined␊
|
|
9068
|
+
try {␊
|
|
9069
|
+
__finish()␊
|
|
9070
|
+
} finally {␊
|
|
9071
|
+
__wasmEnvCleanupPreparing = false␊
|
|
9072
|
+
}␊
|
|
9073
|
+
__wasmEnvCleanupPrepared = true␊
|
|
9074
|
+
}␊
|
|
9075
|
+
if (!__live || typeof __workPending !== 'function') {␊
|
|
9076
|
+
__finishEnvCleanup()␊
|
|
9077
|
+
return␊
|
|
9078
|
+
}␊
|
|
9079
|
+
// Publish the closer before yielding: from here until \`__finishEnvCleanup\`␊
|
|
9080
|
+
// runs, a caller that cannot yield is entitled to end this handshake.␊
|
|
9081
|
+
__finishParkedEnvCleanup = __finishEnvCleanup␊
|
|
9082
|
+
return __pollWasmRuntimeWork(__workPending).then(␊
|
|
9083
|
+
__finishEnvCleanup,␊
|
|
9084
|
+
__finishEnvCleanup,␊
|
|
9085
|
+
)␊
|
|
9086
|
+
}␊
|
|
6631
9087
|
// The barrier + settlement drain, hoisted out of the context destroyer so the␊
|
|
6632
9088
|
// drain can yield without widening the destroyer's reentry window. Both␊
|
|
6633
9089
|
// yielding paths run it — dispose() and the initialization-failure rollback.␊
|
|
@@ -6640,6 +9096,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6640
9096
|
// is enough — and dispose() stays retryable after it rejects, so marking the␊
|
|
6641
9097
|
// drain complete up front would make the retry skip it and destroy the context␊
|
|
6642
9098
|
// with the barrier's settlements still queued.␊
|
|
9099
|
+
const __drainAfterEnvCleanup = () => {␊
|
|
9100
|
+
if (!__wasmEnvCleanupRan) {␊
|
|
9101
|
+
return␊
|
|
9102
|
+
}␊
|
|
9103
|
+
const __drained = __drainWasmEnvCleanup(__napiInstance)␊
|
|
9104
|
+
if (!__drained || typeof __drained.then !== 'function') {␊
|
|
9105
|
+
__wasmEnvCleanupDrained = true␊
|
|
9106
|
+
return␊
|
|
9107
|
+
}␊
|
|
9108
|
+
return __drained.then((__value) => {␊
|
|
9109
|
+
__wasmEnvCleanupDrained = true␊
|
|
9110
|
+
return __value␊
|
|
9111
|
+
})␊
|
|
9112
|
+
}␊
|
|
6643
9113
|
const __prepareForDisposal = () => {␊
|
|
6644
9114
|
if (__wasmEnvCleanupDrained) {␊
|
|
6645
9115
|
return␊
|
|
@@ -6647,18 +9117,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6647
9117
|
if (__wasmEnvCleanupDrainPromise) {␊
|
|
6648
9118
|
return __wasmEnvCleanupDrainPromise␊
|
|
6649
9119
|
}␊
|
|
6650
|
-
|
|
6651
|
-
|
|
6652
|
-
|
|
6653
|
-
|
|
6654
|
-
const
|
|
6655
|
-
|
|
6656
|
-
|
|
9120
|
+
// The barrier itself can yield now, so the memo below has to cover it too:␊
|
|
9121
|
+
// a reentrant caller must join this handshake rather than start a second␊
|
|
9122
|
+
// one while the first is parked between the two halves.␊
|
|
9123
|
+
const __prepared = __prepareEnvCleanupWithTurns()␊
|
|
9124
|
+
const __settled =␊
|
|
9125
|
+
__prepared && typeof __prepared.then === 'function'␊
|
|
9126
|
+
? __prepared.then(__drainAfterEnvCleanup)␊
|
|
9127
|
+
: __drainAfterEnvCleanup()␊
|
|
9128
|
+
if (!__settled || typeof __settled.then !== 'function') {␊
|
|
6657
9129
|
return␊
|
|
6658
9130
|
}␊
|
|
6659
|
-
const __tracked =
|
|
9131
|
+
const __tracked = __settled.then(␊
|
|
6660
9132
|
(__value) => {␊
|
|
6661
|
-
__wasmEnvCleanupDrained = true␊
|
|
6662
9133
|
__wasmEnvCleanupDrainPromise = undefined␊
|
|
6663
9134
|
return __value␊
|
|
6664
9135
|
},␊
|
|
@@ -7265,7 +9736,31 @@ Generated by [AVA](https://avajs.dev).
|
|
|
7265
9736
|
},␊
|
|
7266
9737
|
})␊
|
|
7267
9738
|
␊
|
|
9739
|
+
// When this wasm thread dies, raise the addon's crash flag before emnapi reports␊
|
|
9740
|
+
// it. The loader thread may be inside wasm, in a cleanup call that waits on this␊
|
|
9741
|
+
// thread; the flag is what those waits check between short slices, and once it␊
|
|
9742
|
+
// is up they trap instead of waiting for good. It is a word in the shared wasm␊
|
|
9743
|
+
// memory, and the loader posts a view of it (see␊
|
|
9744
|
+
// napi_wasm_thread_crash_flag_address) after it instantiated the wasm, or right␊
|
|
9745
|
+
// after it created this worker. No view, no flag: an addon built with an older␊
|
|
9746
|
+
// napi, or a failure before the view arrived.␊
|
|
9747
|
+
let __addonCrashFlag␊
|
|
9748
|
+
const __beforeReportError = handler.beforeReportError␊
|
|
9749
|
+
handler.beforeReportError = function (...args) {␊
|
|
9750
|
+
if (__addonCrashFlag !== undefined) {␊
|
|
9751
|
+
try {␊
|
|
9752
|
+
Atomics.store(__addonCrashFlag, 0, 1)␊
|
|
9753
|
+
} catch {}␊
|
|
9754
|
+
}␊
|
|
9755
|
+
return __beforeReportError.apply(this, args)␊
|
|
9756
|
+
}␊
|
|
9757
|
+
␊
|
|
7268
9758
|
globalThis.onmessage = function (e) {␊
|
|
9759
|
+
const data = e && e.data␊
|
|
9760
|
+
if (data && data.__napiRsAddonCrashFlag instanceof Int32Array) {␊
|
|
9761
|
+
__addonCrashFlag = data.__napiRsAddonCrashFlag␊
|
|
9762
|
+
return␊
|
|
9763
|
+
}␊
|
|
7269
9764
|
handler.handle(e)␊
|
|
7270
9765
|
}␊
|
|
7271
9766
|
`
|
|
@@ -7321,7 +9816,31 @@ Generated by [AVA](https://avajs.dev).
|
|
|
7321
9816
|
}␊
|
|
7322
9817
|
})␊
|
|
7323
9818
|
␊
|
|
9819
|
+
// When this wasm thread dies, raise the addon's crash flag before emnapi reports␊
|
|
9820
|
+
// it. The loader thread may be inside wasm, in a cleanup call that waits on this␊
|
|
9821
|
+
// thread; the flag is what those waits check between short slices, and once it␊
|
|
9822
|
+
// is up they trap instead of waiting for good. It is a word in the shared wasm␊
|
|
9823
|
+
// memory, and the loader posts a view of it (see␊
|
|
9824
|
+
// napi_wasm_thread_crash_flag_address) after it instantiated the wasm, or right␊
|
|
9825
|
+
// after it created this worker. No view, no flag: an addon built with an older␊
|
|
9826
|
+
// napi, or a failure before the view arrived.␊
|
|
9827
|
+
let __addonCrashFlag␊
|
|
9828
|
+
const __beforeReportError = handler.beforeReportError␊
|
|
9829
|
+
handler.beforeReportError = function (...args) {␊
|
|
9830
|
+
if (__addonCrashFlag !== undefined) {␊
|
|
9831
|
+
try {␊
|
|
9832
|
+
Atomics.store(__addonCrashFlag, 0, 1)␊
|
|
9833
|
+
} catch {}␊
|
|
9834
|
+
}␊
|
|
9835
|
+
return __beforeReportError.apply(this, args)␊
|
|
9836
|
+
}␊
|
|
9837
|
+
␊
|
|
7324
9838
|
globalThis.onmessage = function (e) {␊
|
|
9839
|
+
const data = e && e.data␊
|
|
9840
|
+
if (data && data.__napiRsAddonCrashFlag instanceof Int32Array) {␊
|
|
9841
|
+
__addonCrashFlag = data.__napiRsAddonCrashFlag␊
|
|
9842
|
+
return␊
|
|
9843
|
+
}␊
|
|
7325
9844
|
handler.handle(e)␊
|
|
7326
9845
|
}␊
|
|
7327
9846
|
`
|
|
@@ -7385,7 +9904,31 @@ Generated by [AVA](https://avajs.dev).
|
|
|
7385
9904
|
}␊
|
|
7386
9905
|
})␊
|
|
7387
9906
|
␊
|
|
9907
|
+
// When this wasm thread dies, raise the addon's crash flag before emnapi reports␊
|
|
9908
|
+
// it. The loader thread may be inside wasm, in a cleanup call that waits on this␊
|
|
9909
|
+
// thread; the flag is what those waits check between short slices, and once it␊
|
|
9910
|
+
// is up they trap instead of waiting for good. It is a word in the shared wasm␊
|
|
9911
|
+
// memory, and the loader posts a view of it (see␊
|
|
9912
|
+
// napi_wasm_thread_crash_flag_address) after it instantiated the wasm, or right␊
|
|
9913
|
+
// after it created this worker. No view, no flag: an addon built with an older␊
|
|
9914
|
+
// napi, or a failure before the view arrived.␊
|
|
9915
|
+
let __addonCrashFlag␊
|
|
9916
|
+
const __beforeReportError = handler.beforeReportError␊
|
|
9917
|
+
handler.beforeReportError = function (...args) {␊
|
|
9918
|
+
if (__addonCrashFlag !== undefined) {␊
|
|
9919
|
+
try {␊
|
|
9920
|
+
Atomics.store(__addonCrashFlag, 0, 1)␊
|
|
9921
|
+
} catch {}␊
|
|
9922
|
+
}␊
|
|
9923
|
+
return __beforeReportError.apply(this, args)␊
|
|
9924
|
+
}␊
|
|
9925
|
+
␊
|
|
7388
9926
|
globalThis.onmessage = function (e) {␊
|
|
9927
|
+
const data = e && e.data␊
|
|
9928
|
+
if (data && data.__napiRsAddonCrashFlag instanceof Int32Array) {␊
|
|
9929
|
+
__addonCrashFlag = data.__napiRsAddonCrashFlag␊
|
|
9930
|
+
return␊
|
|
9931
|
+
}␊
|
|
7389
9932
|
handler.handle(e)␊
|
|
7390
9933
|
}␊
|
|
7391
9934
|
`
|