@napi-rs/cli 3.10.4 → 3.10.5
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 +1174 -53
- package/dist/index.cjs +1173 -52
- package/dist/index.d.cts +19 -7
- package/dist/index.d.ts +19 -7
- package/dist/index.js +1174 -53
- package/docs/build.md +1 -0
- package/docs/wasi.md +149 -8
- package/package.json +2 -2
- package/src/api/__tests__/__snapshots__/templates.spec.ts.md +2498 -132
- package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
- package/src/api/__tests__/templates.spec.ts +266 -6
- package/src/api/build.ts +38 -1
- package/src/api/templates/load-wasi-template.ts +807 -24
- 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
|
␊
|
|
@@ -1144,6 +1547,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1144
1547
|
let __emnapiContextDestroyPromise␊
|
|
1145
1548
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
1146
1549
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
1550
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
1551
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
1552
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
1553
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
1554
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
1555
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
1556
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
1557
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
1558
|
+
let __finishParkedWasmEnvCleanup␊
|
|
1559
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
1560
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
1561
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
1562
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
1563
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
1147
1564
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
1148
1565
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
1149
1566
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -1256,7 +1673,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1256
1673
|
}␊
|
|
1257
1674
|
␊
|
|
1258
1675
|
function __prepareWasmEnvCleanup() {␊
|
|
1259
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
1676
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
1677
|
+
return␊
|
|
1678
|
+
}␊
|
|
1679
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
1680
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
1681
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
1682
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
1683
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
1684
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
1685
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
1686
|
+
// joined and the workers never drained.␊
|
|
1687
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
1688
|
+
if (finishParked !== undefined) {␊
|
|
1689
|
+
finishParked()␊
|
|
1690
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
1691
|
+
return␊
|
|
1692
|
+
}␊
|
|
1693
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
1260
1694
|
return␊
|
|
1261
1695
|
}␊
|
|
1262
1696
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -1271,10 +1705,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1271
1705
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
1272
1706
|
}␊
|
|
1273
1707
|
__emnapiWasmEnvCleanupRan = true␊
|
|
1708
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
1274
1709
|
}␊
|
|
1275
1710
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
1276
1711
|
}␊
|
|
1277
1712
|
␊
|
|
1713
|
+
/**␊
|
|
1714
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
1715
|
+
* could deliver them.␊
|
|
1716
|
+
*␊
|
|
1717
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
1718
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
1719
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
1720
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
1721
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
1722
|
+
* were for then hang forever, silently.␊
|
|
1723
|
+
*␊
|
|
1724
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
1725
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
1726
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
1727
|
+
* is already unreachable by then.␊
|
|
1728
|
+
*/␊
|
|
1729
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
1730
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
1731
|
+
return␊
|
|
1732
|
+
}␊
|
|
1733
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
1734
|
+
if (typeof pending !== 'function') {␊
|
|
1735
|
+
return␊
|
|
1736
|
+
}␊
|
|
1737
|
+
let queued␊
|
|
1738
|
+
try {␊
|
|
1739
|
+
queued = pending()␊
|
|
1740
|
+
} catch {␊
|
|
1741
|
+
return␊
|
|
1742
|
+
}␊
|
|
1743
|
+
if (!queued) {␊
|
|
1744
|
+
return␊
|
|
1745
|
+
}␊
|
|
1746
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
1747
|
+
try {␊
|
|
1748
|
+
const consoleHost = globalThis.console␊
|
|
1749
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
1750
|
+
consoleHost.error(␊
|
|
1751
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
1752
|
+
queued +␊
|
|
1753
|
+
" 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.",␊
|
|
1754
|
+
)␊
|
|
1755
|
+
}␊
|
|
1756
|
+
} catch {}␊
|
|
1757
|
+
}␊
|
|
1758
|
+
␊
|
|
1278
1759
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
1279
1760
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
1280
1761
|
// ahead of it on a faster queue.␊
|
|
@@ -1324,6 +1805,291 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1324
1805
|
}␊
|
|
1325
1806
|
}␊
|
|
1326
1807
|
␊
|
|
1808
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
1809
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
1810
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
1811
|
+
// the loop instead of yielding it.␊
|
|
1812
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
1813
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
1814
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
1815
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
1816
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
1817
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
1818
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
1819
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
1820
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
1821
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
1822
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
1823
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
1824
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
1825
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
1826
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
1827
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
1828
|
+
␊
|
|
1829
|
+
/**␊
|
|
1830
|
+
* Pacing state for one runtime-work poll.␊
|
|
1831
|
+
*␊
|
|
1832
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
1833
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
1834
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
1835
|
+
* instance must not disarm the fallback for the next one.␊
|
|
1836
|
+
*/␊
|
|
1837
|
+
function __createWasmRuntimePollPace() {␊
|
|
1838
|
+
return {␊
|
|
1839
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
1840
|
+
arrivals: 0,␊
|
|
1841
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
1842
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
1843
|
+
settleTurn: undefined,␊
|
|
1844
|
+
turnTimerDueAt: 0,␊
|
|
1845
|
+
}␊
|
|
1846
|
+
}␊
|
|
1847
|
+
␊
|
|
1848
|
+
/**␊
|
|
1849
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
1850
|
+
*␊
|
|
1851
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
1852
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
1853
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
1854
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
1855
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
1856
|
+
*␊
|
|
1857
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
1858
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
1859
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
1860
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
1861
|
+
* the rescue is how far back that newest live backup is:␊
|
|
1862
|
+
*␊
|
|
1863
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
1864
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
1865
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
1866
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
1867
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
1868
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
1869
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
1870
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
1871
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
1872
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
1873
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
1874
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
1875
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
1876
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
1877
|
+
* a throttle last let one through.␊
|
|
1878
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
1879
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
1880
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
1881
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
1882
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
1883
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
1884
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
1885
|
+
* the second.␊
|
|
1886
|
+
*␊
|
|
1887
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
1888
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
1889
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
1890
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
1891
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
1892
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
1893
|
+
*/␊
|
|
1894
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
1895
|
+
const setTimer = globalThis.setTimeout␊
|
|
1896
|
+
if (typeof setTimer !== 'function') {␊
|
|
1897
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
1898
|
+
// already, and that one cannot park.␊
|
|
1899
|
+
return␊
|
|
1900
|
+
}␊
|
|
1901
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
1902
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
1903
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
1904
|
+
let handle␊
|
|
1905
|
+
try {␊
|
|
1906
|
+
handle = setTimer(() => {␊
|
|
1907
|
+
const settleTurn = pace.settleTurn␊
|
|
1908
|
+
if (␊
|
|
1909
|
+
!settleTurn ||␊
|
|
1910
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
1911
|
+
) {␊
|
|
1912
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
1913
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
1914
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
1915
|
+
return␊
|
|
1916
|
+
}␊
|
|
1917
|
+
pace.arrivals = 0␊
|
|
1918
|
+
pace.settleTurn = undefined␊
|
|
1919
|
+
settleTurn()␊
|
|
1920
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
1921
|
+
} catch {␊
|
|
1922
|
+
return␊
|
|
1923
|
+
}␊
|
|
1924
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
1925
|
+
try {␊
|
|
1926
|
+
handle.unref()␊
|
|
1927
|
+
} catch {}␊
|
|
1928
|
+
}␊
|
|
1929
|
+
}␊
|
|
1930
|
+
␊
|
|
1931
|
+
/**␊
|
|
1932
|
+
* One turn of the runtime-work poll.␊
|
|
1933
|
+
*␊
|
|
1934
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
1935
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
1936
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
1937
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
1938
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
1939
|
+
* ever call \`…_finish\`.␊
|
|
1940
|
+
*␊
|
|
1941
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
1942
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
1943
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
1944
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
1945
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
1946
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
1947
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
1948
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
1949
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
1950
|
+
*/␊
|
|
1951
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
1952
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
1953
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
1954
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
1955
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
1956
|
+
return new Promise((resolve) => {␊
|
|
1957
|
+
let settled = false␊
|
|
1958
|
+
const settle = () => {␊
|
|
1959
|
+
if (settled) {␊
|
|
1960
|
+
return␊
|
|
1961
|
+
}␊
|
|
1962
|
+
settled = true␊
|
|
1963
|
+
if (pace.settleTurn === settle) {␊
|
|
1964
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
1965
|
+
// due time this turn has already answered.␊
|
|
1966
|
+
pace.settleTurn = undefined␊
|
|
1967
|
+
}␊
|
|
1968
|
+
resolve()␊
|
|
1969
|
+
}␊
|
|
1970
|
+
__scheduleTimer(() => {␊
|
|
1971
|
+
pace.arrivals++␊
|
|
1972
|
+
settle()␊
|
|
1973
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
1974
|
+
// Read next to the arming it describes; see␊
|
|
1975
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
1976
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
1977
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
1978
|
+
__scheduleMacrotask(settle)␊
|
|
1979
|
+
return␊
|
|
1980
|
+
}␊
|
|
1981
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
1982
|
+
// the timer never arrives.␊
|
|
1983
|
+
pace.settleTurn = settle␊
|
|
1984
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
1985
|
+
})␊
|
|
1986
|
+
}␊
|
|
1987
|
+
␊
|
|
1988
|
+
/**␊
|
|
1989
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
1990
|
+
* event-loop turns in the middle.␊
|
|
1991
|
+
*␊
|
|
1992
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
1993
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
1994
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
1995
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
1996
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
1997
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
1998
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
1999
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
2000
|
+
* entire point.␊
|
|
2001
|
+
*␊
|
|
2002
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
2003
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
2004
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
2005
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
2006
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
2007
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
2008
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
2009
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
2010
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
2011
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
2012
|
+
*␊
|
|
2013
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
2014
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
2015
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
2016
|
+
* an idle disposal synchronous.␊
|
|
2017
|
+
*/␊
|
|
2018
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
2019
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
2020
|
+
return␊
|
|
2021
|
+
}␊
|
|
2022
|
+
const exports = __napiInstance?.exports␊
|
|
2023
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
2024
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
2025
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
2026
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
2027
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
2028
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
2029
|
+
try {␊
|
|
2030
|
+
__prepareWasmEnvCleanup()␊
|
|
2031
|
+
} finally {␊
|
|
2032
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
2033
|
+
}␊
|
|
2034
|
+
return␊
|
|
2035
|
+
}␊
|
|
2036
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
2037
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
2038
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
2039
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
2040
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
2041
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
2042
|
+
let live␊
|
|
2043
|
+
try {␊
|
|
2044
|
+
live = begin()␊
|
|
2045
|
+
} catch (error) {␊
|
|
2046
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
2047
|
+
throw error␊
|
|
2048
|
+
}␊
|
|
2049
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
2050
|
+
const finishCleanup = () => {␊
|
|
2051
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
2052
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
2053
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
2054
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
2055
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
2056
|
+
return␊
|
|
2057
|
+
}␊
|
|
2058
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
2059
|
+
try {␊
|
|
2060
|
+
finish()␊
|
|
2061
|
+
} finally {␊
|
|
2062
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
2063
|
+
}␊
|
|
2064
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
2065
|
+
}␊
|
|
2066
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
2067
|
+
finishCleanup()␊
|
|
2068
|
+
return␊
|
|
2069
|
+
}␊
|
|
2070
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
2071
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
2072
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
2073
|
+
return (async () => {␊
|
|
2074
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
2075
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
2076
|
+
// what let that happen at all.␊
|
|
2077
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
2078
|
+
for (;;) {␊
|
|
2079
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
2080
|
+
try {␊
|
|
2081
|
+
if (!workPending()) {␊
|
|
2082
|
+
return␊
|
|
2083
|
+
}␊
|
|
2084
|
+
} catch {␊
|
|
2085
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
2086
|
+
// reachable work left. Stop polling and finish.␊
|
|
2087
|
+
return␊
|
|
2088
|
+
}␊
|
|
2089
|
+
}␊
|
|
2090
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
2091
|
+
}␊
|
|
2092
|
+
␊
|
|
1327
2093
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
1328
2094
|
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
1329
2095
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
@@ -1460,6 +2226,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1460
2226
|
}␊
|
|
1461
2227
|
␊
|
|
1462
2228
|
__prepareWasmEnvCleanup()␊
|
|
2229
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
2230
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
2231
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
2232
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
2233
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
2234
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
2235
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
2236
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
2237
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
2238
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
2239
|
+
// rather than skipping it.␊
|
|
2240
|
+
return␊
|
|
2241
|
+
}␊
|
|
1463
2242
|
const result = __emnapiContext.destroy()␊
|
|
1464
2243
|
if (!__isThenable(result)) {␊
|
|
1465
2244
|
__emnapiContextDestroyed = true␊
|
|
@@ -1733,11 +2512,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1733
2512
|
return __finishWasiDisposal()␊
|
|
1734
2513
|
}␊
|
|
1735
2514
|
␊
|
|
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()␊
|
|
2515
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
1741
2516
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
1742
2517
|
if (__isThenable(drainResult)) {␊
|
|
1743
2518
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -1745,6 +2520,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1745
2520
|
return __continueWasiDisposal()␊
|
|
1746
2521
|
}␊
|
|
1747
2522
|
␊
|
|
2523
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
2524
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
2525
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
2526
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
2527
|
+
// these back to back is what strands them.␊
|
|
2528
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
2529
|
+
if (__isThenable(prepareResult)) {␊
|
|
2530
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
2531
|
+
}␊
|
|
2532
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
2533
|
+
}␊
|
|
2534
|
+
␊
|
|
1748
2535
|
function __startWasiDisposal() {␊
|
|
1749
2536
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
1750
2537
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -1905,14 +2692,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1905
2692
|
// be reached without the async-work drain below running first.␊
|
|
1906
2693
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
1907
2694
|
const cleanupErrors = []␊
|
|
2695
|
+
let prepareResult␊
|
|
2696
|
+
try {␊
|
|
2697
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
2698
|
+
} catch (cleanupError) {␊
|
|
2699
|
+
cleanupErrors.push(cleanupError)␊
|
|
2700
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2701
|
+
}␊
|
|
2702
|
+
if (__isThenable(prepareResult)) {␊
|
|
2703
|
+
return Promise.resolve(prepareResult).then(␊
|
|
2704
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
2705
|
+
(cleanupError) => {␊
|
|
2706
|
+
cleanupErrors.push(cleanupError)␊
|
|
2707
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2708
|
+
},␊
|
|
2709
|
+
)␊
|
|
2710
|
+
}␊
|
|
2711
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
2712
|
+
}␊
|
|
2713
|
+
␊
|
|
2714
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
2715
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
2716
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
2717
|
+
// did not finish does.␊
|
|
2718
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
1908
2719
|
let drainResult␊
|
|
1909
|
-
let settlementsUnreached = false␊
|
|
1910
2720
|
try {␊
|
|
1911
|
-
__prepareWasmEnvCleanup()␊
|
|
1912
2721
|
drainResult = __drainWasmEnvCleanup()␊
|
|
1913
2722
|
} catch (cleanupError) {␊
|
|
1914
2723
|
cleanupErrors.push(cleanupError)␊
|
|
1915
|
-
|
|
2724
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1916
2725
|
}␊
|
|
1917
2726
|
if (__isThenable(drainResult)) {␊
|
|
1918
2727
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -1923,9 +2732,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1923
2732
|
},␊
|
|
1924
2733
|
)␊
|
|
1925
2734
|
}␊
|
|
1926
|
-
if (settlementsUnreached) {␊
|
|
1927
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1928
|
-
}␊
|
|
1929
2735
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1930
2736
|
}␊
|
|
1931
2737
|
␊
|
|
@@ -2172,6 +2978,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2172
2978
|
let __emnapiContextDestroyPromise␊
|
|
2173
2979
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
2174
2980
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
2981
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
2982
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
2983
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
2984
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
2985
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
2986
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
2987
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
2988
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
2989
|
+
let __finishParkedWasmEnvCleanup␊
|
|
2990
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
2991
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
2992
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
2993
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
2994
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
2175
2995
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
2176
2996
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
2177
2997
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -2283,73 +3103,422 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2283
3103
|
return __emnapiWasmEnvCleanupPreparing␊
|
|
2284
3104
|
}␊
|
|
2285
3105
|
␊
|
|
2286
|
-
function __prepareWasmEnvCleanup() {␊
|
|
3106
|
+
function __prepareWasmEnvCleanup() {␊
|
|
3107
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
3108
|
+
return␊
|
|
3109
|
+
}␊
|
|
3110
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
3111
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
3112
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
3113
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
3114
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
3115
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
3116
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
3117
|
+
// joined and the workers never drained.␊
|
|
3118
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
3119
|
+
if (finishParked !== undefined) {␊
|
|
3120
|
+
finishParked()␊
|
|
3121
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
3122
|
+
return␊
|
|
3123
|
+
}␊
|
|
3124
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
3125
|
+
return␊
|
|
3126
|
+
}␊
|
|
3127
|
+
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
3128
|
+
if (typeof prepare === 'function') {␊
|
|
3129
|
+
// The addon settles the promises it cancels synchronously, under a␊
|
|
3130
|
+
// non-reentrant lifecycle mutex: anything a promise hook calls from in␊
|
|
3131
|
+
// here must not reach this export again.␊
|
|
3132
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
3133
|
+
try {␊
|
|
3134
|
+
prepare()␊
|
|
3135
|
+
} finally {␊
|
|
3136
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3137
|
+
}␊
|
|
3138
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
3139
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
3140
|
+
}␊
|
|
3141
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
3142
|
+
}␊
|
|
3143
|
+
␊
|
|
3144
|
+
/**␊
|
|
3145
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
3146
|
+
* could deliver them.␊
|
|
3147
|
+
*␊
|
|
3148
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
3149
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
3150
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
3151
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
3152
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
3153
|
+
* were for then hang forever, silently.␊
|
|
3154
|
+
*␊
|
|
3155
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
3156
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
3157
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
3158
|
+
* is already unreachable by then.␊
|
|
3159
|
+
*/␊
|
|
3160
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
3161
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
3162
|
+
return␊
|
|
3163
|
+
}␊
|
|
3164
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
3165
|
+
if (typeof pending !== 'function') {␊
|
|
3166
|
+
return␊
|
|
3167
|
+
}␊
|
|
3168
|
+
let queued␊
|
|
3169
|
+
try {␊
|
|
3170
|
+
queued = pending()␊
|
|
3171
|
+
} catch {␊
|
|
3172
|
+
return␊
|
|
3173
|
+
}␊
|
|
3174
|
+
if (!queued) {␊
|
|
3175
|
+
return␊
|
|
3176
|
+
}␊
|
|
3177
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
3178
|
+
try {␊
|
|
3179
|
+
const consoleHost = globalThis.console␊
|
|
3180
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
3181
|
+
consoleHost.error(␊
|
|
3182
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
3183
|
+
queued +␊
|
|
3184
|
+
" 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.",␊
|
|
3185
|
+
)␊
|
|
3186
|
+
}␊
|
|
3187
|
+
} catch {}␊
|
|
3188
|
+
}␊
|
|
3189
|
+
␊
|
|
3190
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
3191
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
3192
|
+
// ahead of it on a faster queue.␊
|
|
3193
|
+
const __scheduleMacrotask = (function () {␊
|
|
3194
|
+
if (typeof setImmediate === 'function') {␊
|
|
3195
|
+
return function (callback) {␊
|
|
3196
|
+
setImmediate(callback)␊
|
|
3197
|
+
}␊
|
|
3198
|
+
}␊
|
|
3199
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
3200
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
3201
|
+
return function (callback) {␊
|
|
3202
|
+
const channel = new __MessageChannel()␊
|
|
3203
|
+
channel.port1.onmessage = function () {␊
|
|
3204
|
+
channel.port1.onmessage = null␊
|
|
3205
|
+
try {␊
|
|
3206
|
+
channel.port1.close()␊
|
|
3207
|
+
} catch {}␊
|
|
3208
|
+
try {␊
|
|
3209
|
+
channel.port2.close()␊
|
|
3210
|
+
} catch {}␊
|
|
3211
|
+
callback()␊
|
|
3212
|
+
}␊
|
|
3213
|
+
channel.port2.postMessage(null)␊
|
|
3214
|
+
}␊
|
|
3215
|
+
}␊
|
|
3216
|
+
return function (callback) {␊
|
|
3217
|
+
setTimeout(callback, 0)␊
|
|
3218
|
+
}␊
|
|
3219
|
+
})()␊
|
|
3220
|
+
␊
|
|
3221
|
+
// A real, *referenced* timer, for waits that must let the whole host make␊
|
|
3222
|
+
// progress between looks — the async-work drain polls the addon rather than␊
|
|
3223
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there␊
|
|
3224
|
+
// would spin the loop instead of yielding it. Falls back to the macrotask␊
|
|
3225
|
+
// scheduler on a host without timers.␊
|
|
3226
|
+
function __scheduleTimer(callback, delay) {␊
|
|
3227
|
+
const setTimer = globalThis.setTimeout␊
|
|
3228
|
+
if (typeof setTimer !== 'function') {␊
|
|
3229
|
+
__scheduleMacrotask(callback)␊
|
|
3230
|
+
return␊
|
|
3231
|
+
}␊
|
|
3232
|
+
try {␊
|
|
3233
|
+
setTimer(callback, delay)␊
|
|
3234
|
+
} catch {␊
|
|
3235
|
+
__scheduleMacrotask(callback)␊
|
|
3236
|
+
}␊
|
|
3237
|
+
}␊
|
|
3238
|
+
␊
|
|
3239
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
3240
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
3241
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
3242
|
+
// the loop instead of yielding it.␊
|
|
3243
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
3244
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
3245
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
3246
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
3247
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
3248
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
3249
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
3250
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
3251
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
3252
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
3253
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
3254
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
3255
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
3256
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
3257
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
3258
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
3259
|
+
␊
|
|
3260
|
+
/**␊
|
|
3261
|
+
* Pacing state for one runtime-work poll.␊
|
|
3262
|
+
*␊
|
|
3263
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
3264
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
3265
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
3266
|
+
* instance must not disarm the fallback for the next one.␊
|
|
3267
|
+
*/␊
|
|
3268
|
+
function __createWasmRuntimePollPace() {␊
|
|
3269
|
+
return {␊
|
|
3270
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
3271
|
+
arrivals: 0,␊
|
|
3272
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
3273
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
3274
|
+
settleTurn: undefined,␊
|
|
3275
|
+
turnTimerDueAt: 0,␊
|
|
3276
|
+
}␊
|
|
3277
|
+
}␊
|
|
3278
|
+
␊
|
|
3279
|
+
/**␊
|
|
3280
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
3281
|
+
*␊
|
|
3282
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
3283
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
3284
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
3285
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
3286
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
3287
|
+
*␊
|
|
3288
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
3289
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
3290
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
3291
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
3292
|
+
* the rescue is how far back that newest live backup is:␊
|
|
3293
|
+
*␊
|
|
3294
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
3295
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
3296
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
3297
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
3298
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
3299
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
3300
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
3301
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
3302
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
3303
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
3304
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
3305
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
3306
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
3307
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
3308
|
+
* a throttle last let one through.␊
|
|
3309
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
3310
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
3311
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
3312
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
3313
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
3314
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
3315
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
3316
|
+
* the second.␊
|
|
3317
|
+
*␊
|
|
3318
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
3319
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
3320
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
3321
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
3322
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
3323
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
3324
|
+
*/␊
|
|
3325
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
3326
|
+
const setTimer = globalThis.setTimeout␊
|
|
3327
|
+
if (typeof setTimer !== 'function') {␊
|
|
3328
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
3329
|
+
// already, and that one cannot park.␊
|
|
3330
|
+
return␊
|
|
3331
|
+
}␊
|
|
3332
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
3333
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
3334
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
3335
|
+
let handle␊
|
|
3336
|
+
try {␊
|
|
3337
|
+
handle = setTimer(() => {␊
|
|
3338
|
+
const settleTurn = pace.settleTurn␊
|
|
3339
|
+
if (␊
|
|
3340
|
+
!settleTurn ||␊
|
|
3341
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
3342
|
+
) {␊
|
|
3343
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
3344
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
3345
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
3346
|
+
return␊
|
|
3347
|
+
}␊
|
|
3348
|
+
pace.arrivals = 0␊
|
|
3349
|
+
pace.settleTurn = undefined␊
|
|
3350
|
+
settleTurn()␊
|
|
3351
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
3352
|
+
} catch {␊
|
|
3353
|
+
return␊
|
|
3354
|
+
}␊
|
|
3355
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
3356
|
+
try {␊
|
|
3357
|
+
handle.unref()␊
|
|
3358
|
+
} catch {}␊
|
|
3359
|
+
}␊
|
|
3360
|
+
}␊
|
|
3361
|
+
␊
|
|
3362
|
+
/**␊
|
|
3363
|
+
* One turn of the runtime-work poll.␊
|
|
3364
|
+
*␊
|
|
3365
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
3366
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
3367
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
3368
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
3369
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
3370
|
+
* ever call \`…_finish\`.␊
|
|
3371
|
+
*␊
|
|
3372
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
3373
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
3374
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
3375
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
3376
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
3377
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
3378
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
3379
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
3380
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
3381
|
+
*/␊
|
|
3382
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
3383
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
3384
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
3385
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
3386
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
3387
|
+
return new Promise((resolve) => {␊
|
|
3388
|
+
let settled = false␊
|
|
3389
|
+
const settle = () => {␊
|
|
3390
|
+
if (settled) {␊
|
|
3391
|
+
return␊
|
|
3392
|
+
}␊
|
|
3393
|
+
settled = true␊
|
|
3394
|
+
if (pace.settleTurn === settle) {␊
|
|
3395
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
3396
|
+
// due time this turn has already answered.␊
|
|
3397
|
+
pace.settleTurn = undefined␊
|
|
3398
|
+
}␊
|
|
3399
|
+
resolve()␊
|
|
3400
|
+
}␊
|
|
3401
|
+
__scheduleTimer(() => {␊
|
|
3402
|
+
pace.arrivals++␊
|
|
3403
|
+
settle()␊
|
|
3404
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
3405
|
+
// Read next to the arming it describes; see␊
|
|
3406
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
3407
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
3408
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
3409
|
+
__scheduleMacrotask(settle)␊
|
|
3410
|
+
return␊
|
|
3411
|
+
}␊
|
|
3412
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
3413
|
+
// the timer never arrives.␊
|
|
3414
|
+
pace.settleTurn = settle␊
|
|
3415
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
3416
|
+
})␊
|
|
3417
|
+
}␊
|
|
3418
|
+
␊
|
|
3419
|
+
/**␊
|
|
3420
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
3421
|
+
* event-loop turns in the middle.␊
|
|
3422
|
+
*␊
|
|
3423
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
3424
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
3425
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
3426
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
3427
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
3428
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
3429
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
3430
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
3431
|
+
* entire point.␊
|
|
3432
|
+
*␊
|
|
3433
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
3434
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
3435
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
3436
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
3437
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
3438
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
3439
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
3440
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
3441
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
3442
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
3443
|
+
*␊
|
|
3444
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
3445
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
3446
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
3447
|
+
* an idle disposal synchronous.␊
|
|
3448
|
+
*/␊
|
|
3449
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
2287
3450
|
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
2288
3451
|
return␊
|
|
2289
3452
|
}␊
|
|
2290
|
-
const
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
//
|
|
2295
|
-
|
|
3453
|
+
const exports = __napiInstance?.exports␊
|
|
3454
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
3455
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
3456
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
3457
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
3458
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
3459
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
2296
3460
|
try {␊
|
|
2297
|
-
|
|
3461
|
+
__prepareWasmEnvCleanup()␊
|
|
2298
3462
|
} finally {␊
|
|
2299
|
-
|
|
3463
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
2300
3464
|
}␊
|
|
2301
|
-
|
|
3465
|
+
return␊
|
|
2302
3466
|
}␊
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
3467
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
3468
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
3469
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
3470
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
3471
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
3472
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
3473
|
+
let live␊
|
|
3474
|
+
try {␊
|
|
3475
|
+
live = begin()␊
|
|
3476
|
+
} catch (error) {␊
|
|
3477
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3478
|
+
throw error␊
|
|
2314
3479
|
}␊
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
} catch {}␊
|
|
2324
|
-
try {␊
|
|
2325
|
-
channel.port2.close()␊
|
|
2326
|
-
} catch {}␊
|
|
2327
|
-
callback()␊
|
|
2328
|
-
}␊
|
|
2329
|
-
channel.port2.postMessage(null)␊
|
|
3480
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
3481
|
+
const finishCleanup = () => {␊
|
|
3482
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
3483
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
3484
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
3485
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
3486
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
3487
|
+
return␊
|
|
2330
3488
|
}␊
|
|
3489
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
3490
|
+
try {␊
|
|
3491
|
+
finish()␊
|
|
3492
|
+
} finally {␊
|
|
3493
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3494
|
+
}␊
|
|
3495
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
2331
3496
|
}␊
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
}␊
|
|
2335
|
-
})()␊
|
|
2336
|
-
␊
|
|
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)␊
|
|
3497
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
3498
|
+
finishCleanup()␊
|
|
2346
3499
|
return␊
|
|
2347
3500
|
}␊
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
3501
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
3502
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
3503
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
3504
|
+
return (async () => {␊
|
|
3505
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
3506
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
3507
|
+
// what let that happen at all.␊
|
|
3508
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
3509
|
+
for (;;) {␊
|
|
3510
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
3511
|
+
try {␊
|
|
3512
|
+
if (!workPending()) {␊
|
|
3513
|
+
return␊
|
|
3514
|
+
}␊
|
|
3515
|
+
} catch {␊
|
|
3516
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
3517
|
+
// reachable work left. Stop polling and finish.␊
|
|
3518
|
+
return␊
|
|
3519
|
+
}␊
|
|
3520
|
+
}␊
|
|
3521
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
2353
3522
|
}␊
|
|
2354
3523
|
␊
|
|
2355
3524
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
@@ -2488,6 +3657,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2488
3657
|
}␊
|
|
2489
3658
|
␊
|
|
2490
3659
|
__prepareWasmEnvCleanup()␊
|
|
3660
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
3661
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
3662
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
3663
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
3664
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
3665
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
3666
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
3667
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
3668
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
3669
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
3670
|
+
// rather than skipping it.␊
|
|
3671
|
+
return␊
|
|
3672
|
+
}␊
|
|
2491
3673
|
const result = __emnapiContext.destroy()␊
|
|
2492
3674
|
if (!__isThenable(result)) {␊
|
|
2493
3675
|
__emnapiContextDestroyed = true␊
|
|
@@ -2761,11 +3943,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2761
3943
|
return __finishWasiDisposal()␊
|
|
2762
3944
|
}␊
|
|
2763
3945
|
␊
|
|
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()␊
|
|
3946
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
2769
3947
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
2770
3948
|
if (__isThenable(drainResult)) {␊
|
|
2771
3949
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -2773,6 +3951,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2773
3951
|
return __continueWasiDisposal()␊
|
|
2774
3952
|
}␊
|
|
2775
3953
|
␊
|
|
3954
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
3955
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
3956
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
3957
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
3958
|
+
// these back to back is what strands them.␊
|
|
3959
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
3960
|
+
if (__isThenable(prepareResult)) {␊
|
|
3961
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
3962
|
+
}␊
|
|
3963
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
3964
|
+
}␊
|
|
3965
|
+
␊
|
|
2776
3966
|
function __startWasiDisposal() {␊
|
|
2777
3967
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
2778
3968
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -2933,14 +4123,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2933
4123
|
// be reached without the async-work drain below running first.␊
|
|
2934
4124
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
2935
4125
|
const cleanupErrors = []␊
|
|
4126
|
+
let prepareResult␊
|
|
4127
|
+
try {␊
|
|
4128
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
4129
|
+
} catch (cleanupError) {␊
|
|
4130
|
+
cleanupErrors.push(cleanupError)␊
|
|
4131
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4132
|
+
}␊
|
|
4133
|
+
if (__isThenable(prepareResult)) {␊
|
|
4134
|
+
return Promise.resolve(prepareResult).then(␊
|
|
4135
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
4136
|
+
(cleanupError) => {␊
|
|
4137
|
+
cleanupErrors.push(cleanupError)␊
|
|
4138
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4139
|
+
},␊
|
|
4140
|
+
)␊
|
|
4141
|
+
}␊
|
|
4142
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
4143
|
+
}␊
|
|
4144
|
+
␊
|
|
4145
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
4146
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
4147
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
4148
|
+
// did not finish does.␊
|
|
4149
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
2936
4150
|
let drainResult␊
|
|
2937
|
-
let settlementsUnreached = false␊
|
|
2938
4151
|
try {␊
|
|
2939
|
-
__prepareWasmEnvCleanup()␊
|
|
2940
4152
|
drainResult = __drainWasmEnvCleanup()␊
|
|
2941
4153
|
} catch (cleanupError) {␊
|
|
2942
4154
|
cleanupErrors.push(cleanupError)␊
|
|
2943
|
-
|
|
4155
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2944
4156
|
}␊
|
|
2945
4157
|
if (__isThenable(drainResult)) {␊
|
|
2946
4158
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -2951,9 +4163,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
2951
4163
|
},␊
|
|
2952
4164
|
)␊
|
|
2953
4165
|
}␊
|
|
2954
|
-
if (settlementsUnreached) {␊
|
|
2955
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
2956
|
-
}␊
|
|
2957
4166
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
2958
4167
|
}␊
|
|
2959
4168
|
␊
|
|
@@ -3188,6 +4397,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3188
4397
|
let __emnapiContextDestroyPromise␊
|
|
3189
4398
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
3190
4399
|
let __emnapiWasmEnvCleanupPreparing = false␊
|
|
4400
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,␊
|
|
4401
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot␊
|
|
4402
|
+
// tell those two apart on its own: it is raised both for a purely synchronous␊
|
|
4403
|
+
// frame — which must not be re-entered, and which nothing outside it can␊
|
|
4404
|
+
// finish — and across this window, which spans real event-loop turns, so a␊
|
|
4405
|
+
// caller that cannot yield can land in the middle of one. That caller can close␊
|
|
4406
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly␊
|
|
4407
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.␊
|
|
4408
|
+
let __finishParkedWasmEnvCleanup␊
|
|
4409
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
4410
|
+
// queue it leaves behind is expected rather than lost. See␊
|
|
4411
|
+
// \`__reportUnreachedWasmEnvSettlements\`.␊
|
|
4412
|
+
let __emnapiWasmEnvCleanupYielding = false␊
|
|
4413
|
+
let __emnapiWasmEnvSettlementLossReported = false␊
|
|
3191
4414
|
let __emnapiWasmEnvCleanupRan = false␊
|
|
3192
4415
|
let __emnapiWasmEnvCleanupDrained = false␊
|
|
3193
4416
|
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
@@ -3330,7 +4553,24 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3330
4553
|
}␊
|
|
3331
4554
|
␊
|
|
3332
4555
|
function __prepareWasmEnvCleanup() {␊
|
|
3333
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
4556
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
4557
|
+
return␊
|
|
4558
|
+
}␊
|
|
4559
|
+
// A handshake parked between its two halves is one this frame can close, and␊
|
|
4560
|
+
// must: every caller of this function is about to destroy the context, and␊
|
|
4561
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is␊
|
|
4562
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the␊
|
|
4563
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so␊
|
|
4564
|
+
// this degrades to exactly the single call below. Leaving it open instead␊
|
|
4565
|
+
// destroys the context with the barrier still raised, the runtime never␊
|
|
4566
|
+
// joined and the workers never drained.␊
|
|
4567
|
+
const finishParked = __finishParkedWasmEnvCleanup␊
|
|
4568
|
+
if (finishParked !== undefined) {␊
|
|
4569
|
+
finishParked()␊
|
|
4570
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
4571
|
+
return␊
|
|
4572
|
+
}␊
|
|
4573
|
+
if (__emnapiWasmEnvCleanupPreparing) {␊
|
|
3334
4574
|
return␊
|
|
3335
4575
|
}␊
|
|
3336
4576
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
@@ -3345,10 +4585,57 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3345
4585
|
__emnapiWasmEnvCleanupPreparing = false␊
|
|
3346
4586
|
}␊
|
|
3347
4587
|
__emnapiWasmEnvCleanupRan = true␊
|
|
4588
|
+
__reportUnreachedWasmEnvSettlements()␊
|
|
3348
4589
|
}␊
|
|
3349
4590
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
3350
4591
|
}␊
|
|
3351
4592
|
␊
|
|
4593
|
+
/**␊
|
|
4594
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
4595
|
+
* could deliver them.␊
|
|
4596
|
+
*␊
|
|
4597
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to␊
|
|
4598
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same␊
|
|
4599
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and␊
|
|
4600
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains␊
|
|
4601
|
+
* that queue with a null env and discards it. The promises those settlements␊
|
|
4602
|
+
* were for then hang forever, silently.␊
|
|
4603
|
+
*␊
|
|
4604
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
4605
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole␊
|
|
4606
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue␊
|
|
4607
|
+
* is already unreachable by then.␊
|
|
4608
|
+
*/␊
|
|
4609
|
+
function __reportUnreachedWasmEnvSettlements() {␊
|
|
4610
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {␊
|
|
4611
|
+
return␊
|
|
4612
|
+
}␊
|
|
4613
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
4614
|
+
if (typeof pending !== 'function') {␊
|
|
4615
|
+
return␊
|
|
4616
|
+
}␊
|
|
4617
|
+
let queued␊
|
|
4618
|
+
try {␊
|
|
4619
|
+
queued = pending()␊
|
|
4620
|
+
} catch {␊
|
|
4621
|
+
return␊
|
|
4622
|
+
}␊
|
|
4623
|
+
if (!queued) {␊
|
|
4624
|
+
return␊
|
|
4625
|
+
}␊
|
|
4626
|
+
__emnapiWasmEnvSettlementLossReported = true␊
|
|
4627
|
+
try {␊
|
|
4628
|
+
const consoleHost = globalThis.console␊
|
|
4629
|
+
if (consoleHost && typeof consoleHost.error === 'function') {␊
|
|
4630
|
+
consoleHost.error(␊
|
|
4631
|
+
"napi-rs: the wasm environment is being destroyed with " +␊
|
|
4632
|
+
queued +␊
|
|
4633
|
+
" 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.",␊
|
|
4634
|
+
)␊
|
|
4635
|
+
}␊
|
|
4636
|
+
} catch {}␊
|
|
4637
|
+
}␊
|
|
4638
|
+
␊
|
|
3352
4639
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
3353
4640
|
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
3354
4641
|
// ahead of it on a faster queue.␊
|
|
@@ -3398,6 +4685,291 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3398
4685
|
}␊
|
|
3399
4686
|
}␊
|
|
3400
4687
|
␊
|
|
4688
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
4689
|
+
// reason the async-work drain uses one: this polls the addon instead of␊
|
|
4690
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
4691
|
+
// the loop instead of yielding it.␊
|
|
4692
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
4693
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
4694
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
4695
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
4696
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
4697
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
4698
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
4699
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
4700
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
4701
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
4702
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
4703
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
4704
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
4705
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
4706
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
4707
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
4708
|
+
␊
|
|
4709
|
+
/**␊
|
|
4710
|
+
* Pacing state for one runtime-work poll.␊
|
|
4711
|
+
*␊
|
|
4712
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
4713
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
4714
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
4715
|
+
* instance must not disarm the fallback for the next one.␊
|
|
4716
|
+
*/␊
|
|
4717
|
+
function __createWasmRuntimePollPace() {␊
|
|
4718
|
+
return {␊
|
|
4719
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
4720
|
+
arrivals: 0,␊
|
|
4721
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
4722
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
4723
|
+
settleTurn: undefined,␊
|
|
4724
|
+
turnTimerDueAt: 0,␊
|
|
4725
|
+
}␊
|
|
4726
|
+
}␊
|
|
4727
|
+
␊
|
|
4728
|
+
/**␊
|
|
4729
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
4730
|
+
*␊
|
|
4731
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
4732
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
4733
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
4734
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
4735
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
4736
|
+
*␊
|
|
4737
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
4738
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
4739
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
4740
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
4741
|
+
* the rescue is how far back that newest live backup is:␊
|
|
4742
|
+
*␊
|
|
4743
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
4744
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
4745
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
4746
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
4747
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
4748
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
4749
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
4750
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
4751
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
4752
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
4753
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
4754
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
4755
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
4756
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
4757
|
+
* a throttle last let one through.␊
|
|
4758
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
4759
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
4760
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
4761
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
4762
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
4763
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
4764
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
4765
|
+
* the second.␊
|
|
4766
|
+
*␊
|
|
4767
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
4768
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
4769
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
4770
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
4771
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
4772
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
4773
|
+
*/␊
|
|
4774
|
+
function __armWasmRuntimePollStallBackup(pace) {␊
|
|
4775
|
+
const setTimer = globalThis.setTimeout␊
|
|
4776
|
+
if (typeof setTimer !== 'function') {␊
|
|
4777
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
4778
|
+
// already, and that one cannot park.␊
|
|
4779
|
+
return␊
|
|
4780
|
+
}␊
|
|
4781
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
4782
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
4783
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
4784
|
+
let handle␊
|
|
4785
|
+
try {␊
|
|
4786
|
+
handle = setTimer(() => {␊
|
|
4787
|
+
const settleTurn = pace.settleTurn␊
|
|
4788
|
+
if (␊
|
|
4789
|
+
!settleTurn ||␊
|
|
4790
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
4791
|
+
) {␊
|
|
4792
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
4793
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
4794
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
4795
|
+
return␊
|
|
4796
|
+
}␊
|
|
4797
|
+
pace.arrivals = 0␊
|
|
4798
|
+
pace.settleTurn = undefined␊
|
|
4799
|
+
settleTurn()␊
|
|
4800
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
4801
|
+
} catch {␊
|
|
4802
|
+
return␊
|
|
4803
|
+
}␊
|
|
4804
|
+
if (handle && typeof handle.unref === 'function') {␊
|
|
4805
|
+
try {␊
|
|
4806
|
+
handle.unref()␊
|
|
4807
|
+
} catch {}␊
|
|
4808
|
+
}␊
|
|
4809
|
+
}␊
|
|
4810
|
+
␊
|
|
4811
|
+
/**␊
|
|
4812
|
+
* One turn of the runtime-work poll.␊
|
|
4813
|
+
*␊
|
|
4814
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
4815
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
4816
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
4817
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
4818
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
4819
|
+
* ever call \`…_finish\`.␊
|
|
4820
|
+
*␊
|
|
4821
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
4822
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
4823
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
4824
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
4825
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
4826
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
4827
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
4828
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
4829
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
4830
|
+
*/␊
|
|
4831
|
+
function __yieldWasmRuntimePollTurn(pace) {␊
|
|
4832
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
4833
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
4834
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
4835
|
+
__armWasmRuntimePollStallBackup(pace)␊
|
|
4836
|
+
return new Promise((resolve) => {␊
|
|
4837
|
+
let settled = false␊
|
|
4838
|
+
const settle = () => {␊
|
|
4839
|
+
if (settled) {␊
|
|
4840
|
+
return␊
|
|
4841
|
+
}␊
|
|
4842
|
+
settled = true␊
|
|
4843
|
+
if (pace.settleTurn === settle) {␊
|
|
4844
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
4845
|
+
// due time this turn has already answered.␊
|
|
4846
|
+
pace.settleTurn = undefined␊
|
|
4847
|
+
}␊
|
|
4848
|
+
resolve()␊
|
|
4849
|
+
}␊
|
|
4850
|
+
__scheduleTimer(() => {␊
|
|
4851
|
+
pace.arrivals++␊
|
|
4852
|
+
settle()␊
|
|
4853
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
4854
|
+
// Read next to the arming it describes; see␊
|
|
4855
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
4856
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
4857
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
4858
|
+
__scheduleMacrotask(settle)␊
|
|
4859
|
+
return␊
|
|
4860
|
+
}␊
|
|
4861
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
4862
|
+
// the timer never arrives.␊
|
|
4863
|
+
pace.settleTurn = settle␊
|
|
4864
|
+
pace.turnTimerDueAt = turnTimerDueAt␊
|
|
4865
|
+
})␊
|
|
4866
|
+
}␊
|
|
4867
|
+
␊
|
|
4868
|
+
/**␊
|
|
4869
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real␊
|
|
4870
|
+
* event-loop turns in the middle.␊
|
|
4871
|
+
*␊
|
|
4872
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
4873
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it␊
|
|
4874
|
+
* waits on is this one, the only thread that can give a running blocking␊
|
|
4875
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait␊
|
|
4876
|
+
* for work that can never finish. The addon's two-phase form splits that:␊
|
|
4877
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is␊
|
|
4878
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again␊
|
|
4879
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the␊
|
|
4880
|
+
* entire point.␊
|
|
4881
|
+
*␊
|
|
4882
|
+
* The poll has no deadline, for the same reason the async-work drain below has␊
|
|
4883
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the␊
|
|
4884
|
+
* work it would join is the work that is waiting for a turn from this thread —␊
|
|
4885
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript␊
|
|
4886
|
+
* thread can no longer be reached. A blocking closure that never returns keeps␊
|
|
4887
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never␊
|
|
4888
|
+
* returns already keeps an *undisposed* process alive. The host contract is in␊
|
|
4889
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a␊
|
|
4890
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it␊
|
|
4891
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).␊
|
|
4892
|
+
*␊
|
|
4893
|
+
* Feature-detected like every other export in this teardown, so an addon built␊
|
|
4894
|
+
* against a napi crate that predates the split keeps the single blocking call.␊
|
|
4895
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps␊
|
|
4896
|
+
* an idle disposal synchronous.␊
|
|
4897
|
+
*/␊
|
|
4898
|
+
function __prepareWasmEnvCleanupWithTurns() {␊
|
|
4899
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {␊
|
|
4900
|
+
return␊
|
|
4901
|
+
}␊
|
|
4902
|
+
const exports = __napiInstance?.exports␊
|
|
4903
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
4904
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
4905
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {␊
|
|
4906
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
4907
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
4908
|
+
__emnapiWasmEnvCleanupYielding = true␊
|
|
4909
|
+
try {␊
|
|
4910
|
+
__prepareWasmEnvCleanup()␊
|
|
4911
|
+
} finally {␊
|
|
4912
|
+
__emnapiWasmEnvCleanupYielding = false␊
|
|
4913
|
+
}␊
|
|
4914
|
+
return␊
|
|
4915
|
+
}␊
|
|
4916
|
+
const workPending = exports?.napi_wasm_runtime_work_pending␊
|
|
4917
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
4918
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside␊
|
|
4919
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and␊
|
|
4920
|
+
// destroying between the halves would strand exactly what this delivers.␊
|
|
4921
|
+
__emnapiWasmEnvCleanupPreparing = true␊
|
|
4922
|
+
let live␊
|
|
4923
|
+
try {␊
|
|
4924
|
+
live = begin()␊
|
|
4925
|
+
} catch (error) {␊
|
|
4926
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
4927
|
+
throw error␊
|
|
4928
|
+
}␊
|
|
4929
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
4930
|
+
const finishCleanup = () => {␊
|
|
4931
|
+
if (__emnapiWasmEnvCleanupPrepared) {␊
|
|
4932
|
+
// Already closed by a caller that could not yield — the 'exit' teardown␊
|
|
4933
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`␊
|
|
4934
|
+
// is idempotent, but the flags it lowers are not: running it again here␊
|
|
4935
|
+
// would clear a \`preparing\` some later barrier had raised.␊
|
|
4936
|
+
return␊
|
|
4937
|
+
}␊
|
|
4938
|
+
__finishParkedWasmEnvCleanup = undefined␊
|
|
4939
|
+
try {␊
|
|
4940
|
+
finish()␊
|
|
4941
|
+
} finally {␊
|
|
4942
|
+
__emnapiWasmEnvCleanupPreparing = false␊
|
|
4943
|
+
}␊
|
|
4944
|
+
__emnapiWasmEnvCleanupPrepared = true␊
|
|
4945
|
+
}␊
|
|
4946
|
+
if (!live || typeof workPending !== 'function') {␊
|
|
4947
|
+
finishCleanup()␊
|
|
4948
|
+
return␊
|
|
4949
|
+
}␊
|
|
4950
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,␊
|
|
4951
|
+
// a caller that cannot yield is entitled to end this handshake itself.␊
|
|
4952
|
+
__finishParkedWasmEnvCleanup = finishCleanup␊
|
|
4953
|
+
return (async () => {␊
|
|
4954
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when␊
|
|
4955
|
+
// the addon reports its runtime work finished; the turns spent here are␊
|
|
4956
|
+
// what let that happen at all.␊
|
|
4957
|
+
const pace = __createWasmRuntimePollPace()␊
|
|
4958
|
+
for (;;) {␊
|
|
4959
|
+
await __yieldWasmRuntimePollTurn(pace)␊
|
|
4960
|
+
try {␊
|
|
4961
|
+
if (!workPending()) {␊
|
|
4962
|
+
return␊
|
|
4963
|
+
}␊
|
|
4964
|
+
} catch {␊
|
|
4965
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
4966
|
+
// reachable work left. Stop polling and finish.␊
|
|
4967
|
+
return␊
|
|
4968
|
+
}␊
|
|
4969
|
+
}␊
|
|
4970
|
+
})().then(finishCleanup, finishCleanup)␊
|
|
4971
|
+
}␊
|
|
4972
|
+
␊
|
|
3401
4973
|
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
3402
4974
|
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
3403
4975
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
@@ -3535,6 +5107,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3535
5107
|
␊
|
|
3536
5108
|
__disposeCurrentThreadHosts()␊
|
|
3537
5109
|
__prepareWasmEnvCleanup()␊
|
|
5110
|
+
if (__isPreparingWasmEnvCleanup()) {␊
|
|
5111
|
+
// Reached from inside the synchronous barrier — a promise hook one of the␊
|
|
5112
|
+
// settlements above ran, which is the reentrancy the destroy wrapper␊
|
|
5113
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight␊
|
|
5114
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy␊
|
|
5115
|
+
// is what makes the frame that *did* start the barrier skip the real one␊
|
|
5116
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.␊
|
|
5117
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the␊
|
|
5118
|
+
// moment it returns. The deferred loader carries the same backstop. A␊
|
|
5119
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one␊
|
|
5120
|
+
// rather than skipping it.␊
|
|
5121
|
+
return␊
|
|
5122
|
+
}␊
|
|
3538
5123
|
const result = __emnapiContext.destroy()␊
|
|
3539
5124
|
if (!__isThenable(result)) {␊
|
|
3540
5125
|
__emnapiContextDestroyed = true␊
|
|
@@ -3808,11 +5393,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3808
5393
|
return __finishWasiDisposal()␊
|
|
3809
5394
|
}␊
|
|
3810
5395
|
␊
|
|
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()␊
|
|
5396
|
+
function __drainWasmEnvForWasiDisposal() {␊
|
|
3816
5397
|
const drainResult = __drainWasmEnvCleanup()␊
|
|
3817
5398
|
if (__isThenable(drainResult)) {␊
|
|
3818
5399
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
@@ -3820,6 +5401,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3820
5401
|
return __continueWasiDisposal()␊
|
|
3821
5402
|
}␊
|
|
3822
5403
|
␊
|
|
5404
|
+
function __cleanUpWasmEnvForWasiDisposal() {␊
|
|
5405
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks␊
|
|
5406
|
+
// for, when the addon has one — then let the settlements it queued actually␊
|
|
5407
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of␊
|
|
5408
|
+
// these back to back is what strands them.␊
|
|
5409
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
5410
|
+
if (__isThenable(prepareResult)) {␊
|
|
5411
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)␊
|
|
5412
|
+
}␊
|
|
5413
|
+
return __drainWasmEnvForWasiDisposal()␊
|
|
5414
|
+
}␊
|
|
5415
|
+
␊
|
|
3823
5416
|
function __startWasiDisposal() {␊
|
|
3824
5417
|
// Outstanding \`napi_async_work\` goes first, while the environment is still␊
|
|
3825
5418
|
// completely live: the completion callbacks run addon code, and everything␊
|
|
@@ -3980,14 +5573,36 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3980
5573
|
// be reached without the async-work drain below running first.␊
|
|
3981
5574
|
function __rollbackWasmEnvForWasiInitialization() {␊
|
|
3982
5575
|
const cleanupErrors = []␊
|
|
5576
|
+
let prepareResult␊
|
|
5577
|
+
try {␊
|
|
5578
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()␊
|
|
5579
|
+
} catch (cleanupError) {␊
|
|
5580
|
+
cleanupErrors.push(cleanupError)␊
|
|
5581
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
5582
|
+
}␊
|
|
5583
|
+
if (__isThenable(prepareResult)) {␊
|
|
5584
|
+
return Promise.resolve(prepareResult).then(␊
|
|
5585
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),␊
|
|
5586
|
+
(cleanupError) => {␊
|
|
5587
|
+
cleanupErrors.push(cleanupError)␊
|
|
5588
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
5589
|
+
},␊
|
|
5590
|
+
)␊
|
|
5591
|
+
}␊
|
|
5592
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)␊
|
|
5593
|
+
}␊
|
|
5594
|
+
␊
|
|
5595
|
+
// The settlement drain of the rollback above, reached either straight away or␊
|
|
5596
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that␊
|
|
5597
|
+
// did not finish never gets here: it retains instead, exactly as a drain that␊
|
|
5598
|
+
// did not finish does.␊
|
|
5599
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {␊
|
|
3983
5600
|
let drainResult␊
|
|
3984
|
-
let settlementsUnreached = false␊
|
|
3985
5601
|
try {␊
|
|
3986
|
-
__prepareWasmEnvCleanup()␊
|
|
3987
5602
|
drainResult = __drainWasmEnvCleanup()␊
|
|
3988
5603
|
} catch (cleanupError) {␊
|
|
3989
5604
|
cleanupErrors.push(cleanupError)␊
|
|
3990
|
-
|
|
5605
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
3991
5606
|
}␊
|
|
3992
5607
|
if (__isThenable(drainResult)) {␊
|
|
3993
5608
|
return Promise.resolve(drainResult).then(␊
|
|
@@ -3998,9 +5613,6 @@ Generated by [AVA](https://avajs.dev).
|
|
|
3998
5613
|
},␊
|
|
3999
5614
|
)␊
|
|
4000
5615
|
}␊
|
|
4001
|
-
if (settlementsUnreached) {␊
|
|
4002
|
-
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
4003
|
-
}␊
|
|
4004
5616
|
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
4005
5617
|
}␊
|
|
4006
5618
|
␊
|
|
@@ -4567,20 +6179,232 @@ Generated by [AVA](https://avajs.dev).
|
|
|
4567
6179
|
})()␊
|
|
4568
6180
|
}␊
|
|
4569
6181
|
␊
|
|
4570
|
-
// A real, *referenced* timer for the async-work wait below, which polls the␊
|
|
4571
|
-
// addon rather than interleaving with the @emnapi/core dispatch: a zero-delay␊
|
|
4572
|
-
// macrotask there would spin the loop instead of yielding it. Falls back to the␊
|
|
4573
|
-
// macrotask scheduler on a host without timers.␊
|
|
4574
|
-
function __scheduleTimer(__callback, __delay) {␊
|
|
6182
|
+
// A real, *referenced* timer for the async-work wait below, which polls the␊
|
|
6183
|
+
// addon rather than interleaving with the @emnapi/core dispatch: a zero-delay␊
|
|
6184
|
+
// macrotask there would spin the loop instead of yielding it. Falls back to the␊
|
|
6185
|
+
// macrotask scheduler on a host without timers.␊
|
|
6186
|
+
function __scheduleTimer(__callback, __delay) {␊
|
|
6187
|
+
const __setTimer = globalThis.setTimeout␊
|
|
6188
|
+
if (typeof __setTimer !== 'function') {␊
|
|
6189
|
+
__scheduleMacrotask(__callback)␊
|
|
6190
|
+
return␊
|
|
6191
|
+
}␊
|
|
6192
|
+
try {␊
|
|
6193
|
+
__setTimer(__callback, __delay)␊
|
|
6194
|
+
} catch {␊
|
|
6195
|
+
__scheduleMacrotask(__callback)␊
|
|
6196
|
+
}␊
|
|
6197
|
+
}␊
|
|
6198
|
+
␊
|
|
6199
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
6200
|
+
// reason the async-work wait uses one: this polls the addon instead of␊
|
|
6201
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
6202
|
+
// the loop instead of yielding it.␊
|
|
6203
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
6204
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
6205
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
6206
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
6207
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
6208
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
6209
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
6210
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
6211
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
6212
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
6213
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
6214
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
6215
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
6216
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
6217
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
6218
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
6219
|
+
␊
|
|
6220
|
+
/**␊
|
|
6221
|
+
* Pacing state for one runtime-work poll.␊
|
|
6222
|
+
*␊
|
|
6223
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
6224
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
6225
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
6226
|
+
* instance must not disarm the fallback for the next one.␊
|
|
6227
|
+
*/␊
|
|
6228
|
+
function __createWasmRuntimePollPace() {␊
|
|
6229
|
+
return {␊
|
|
6230
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
6231
|
+
arrivals: 0,␊
|
|
6232
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
6233
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
6234
|
+
settleTurn: undefined,␊
|
|
6235
|
+
turnTimerDueAt: 0,␊
|
|
6236
|
+
}␊
|
|
6237
|
+
}␊
|
|
6238
|
+
␊
|
|
6239
|
+
/**␊
|
|
6240
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
6241
|
+
*␊
|
|
6242
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
6243
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
6244
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
6245
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
6246
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
6247
|
+
*␊
|
|
6248
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
6249
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
6250
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
6251
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
6252
|
+
* the rescue is how far back that newest live backup is:␊
|
|
6253
|
+
*␊
|
|
6254
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
6255
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
6256
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
6257
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
6258
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
6259
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
6260
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
6261
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
6262
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
6263
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
6264
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
6265
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
6266
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
6267
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
6268
|
+
* a throttle last let one through.␊
|
|
6269
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
6270
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
6271
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
6272
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
6273
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
6274
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
6275
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
6276
|
+
* the second.␊
|
|
6277
|
+
*␊
|
|
6278
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
6279
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
6280
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
6281
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
6282
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
6283
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
6284
|
+
*/␊
|
|
6285
|
+
function __armWasmRuntimePollStallBackup(__pace) {␊
|
|
4575
6286
|
const __setTimer = globalThis.setTimeout␊
|
|
4576
6287
|
if (typeof __setTimer !== 'function') {␊
|
|
4577
|
-
|
|
6288
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
6289
|
+
// already, and that one cannot park.␊
|
|
4578
6290
|
return␊
|
|
4579
6291
|
}␊
|
|
6292
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
6293
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
6294
|
+
const __dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
6295
|
+
let __handle␊
|
|
4580
6296
|
try {␊
|
|
4581
|
-
__setTimer(
|
|
6297
|
+
__handle = __setTimer(() => {␊
|
|
6298
|
+
const __settleTurn = __pace.settleTurn␊
|
|
6299
|
+
if (␊
|
|
6300
|
+
!__settleTurn ||␊
|
|
6301
|
+
__pace.turnTimerDueAt > __dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
6302
|
+
) {␊
|
|
6303
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
6304
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
6305
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
6306
|
+
return␊
|
|
6307
|
+
}␊
|
|
6308
|
+
__pace.arrivals = 0␊
|
|
6309
|
+
__pace.settleTurn = undefined␊
|
|
6310
|
+
__settleTurn()␊
|
|
6311
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
4582
6312
|
} catch {␊
|
|
4583
|
-
|
|
6313
|
+
return␊
|
|
6314
|
+
}␊
|
|
6315
|
+
if (__handle && typeof __handle.unref === 'function') {␊
|
|
6316
|
+
try {␊
|
|
6317
|
+
__handle.unref()␊
|
|
6318
|
+
} catch {}␊
|
|
6319
|
+
}␊
|
|
6320
|
+
}␊
|
|
6321
|
+
␊
|
|
6322
|
+
/**␊
|
|
6323
|
+
* One turn of the runtime-work poll.␊
|
|
6324
|
+
*␊
|
|
6325
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
6326
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
6327
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
6328
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
6329
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
6330
|
+
* ever call \`…_finish\`.␊
|
|
6331
|
+
*␊
|
|
6332
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
6333
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
6334
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
6335
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
6336
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
6337
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
6338
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
6339
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
6340
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
6341
|
+
*/␊
|
|
6342
|
+
function __yieldWasmRuntimePollTurn(__pace) {␊
|
|
6343
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
6344
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
6345
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
6346
|
+
__armWasmRuntimePollStallBackup(__pace)␊
|
|
6347
|
+
return new Promise((resolve) => {␊
|
|
6348
|
+
let __settled = false␊
|
|
6349
|
+
const __settle = () => {␊
|
|
6350
|
+
if (__settled) {␊
|
|
6351
|
+
return␊
|
|
6352
|
+
}␊
|
|
6353
|
+
__settled = true␊
|
|
6354
|
+
if (__pace.settleTurn === __settle) {␊
|
|
6355
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
6356
|
+
// due time this turn has already answered.␊
|
|
6357
|
+
__pace.settleTurn = undefined␊
|
|
6358
|
+
}␊
|
|
6359
|
+
resolve()␊
|
|
6360
|
+
}␊
|
|
6361
|
+
__scheduleTimer(() => {␊
|
|
6362
|
+
__pace.arrivals++␊
|
|
6363
|
+
__settle()␊
|
|
6364
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
6365
|
+
// Read next to the arming it describes; see␊
|
|
6366
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
6367
|
+
const __turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
6368
|
+
if (__pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
6369
|
+
__scheduleMacrotask(__settle)␊
|
|
6370
|
+
return␊
|
|
6371
|
+
}␊
|
|
6372
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
6373
|
+
// the timer never arrives.␊
|
|
6374
|
+
__pace.settleTurn = __settle␊
|
|
6375
|
+
__pace.turnTimerDueAt = __turnTimerDueAt␊
|
|
6376
|
+
})␊
|
|
6377
|
+
}␊
|
|
6378
|
+
␊
|
|
6379
|
+
/**␊
|
|
6380
|
+
* Yield event-loop turns until the addon reports its runtime work finished.␊
|
|
6381
|
+
*␊
|
|
6382
|
+
* The window between \`napi_prepare_wasm_env_cleanup_begin\` and␊
|
|
6383
|
+
* \`…_finish\` — the turns are the entire point of splitting the barrier, because␊
|
|
6384
|
+
* on a threaded artifact the work \`…_finish\` joins can itself be waiting for a␊
|
|
6385
|
+
* JavaScript turn from this very thread.␊
|
|
6386
|
+
*␊
|
|
6387
|
+
* Unbounded, for the same reason the async-work wait above is: giving up means␊
|
|
6388
|
+
* calling \`…_finish\`, which joins on this thread, and the work it would join is␊
|
|
6389
|
+
* the work waiting for a turn from this thread — so a bound does not end the␊
|
|
6390
|
+
* wait, it only moves it somewhere the JavaScript thread can no longer be␊
|
|
6391
|
+
* reached. A blocking closure that never returns keeps the disposal promise␊
|
|
6392
|
+
* pending instead. The host contract is in \`crates/async-runtime/README.md\`: a␊
|
|
6393
|
+
* blocking closure must never wait on a JavaScript turn.␊
|
|
6394
|
+
*/␊
|
|
6395
|
+
async function __pollWasmRuntimeWork(__workPending) {␊
|
|
6396
|
+
const __pace = __createWasmRuntimePollPace()␊
|
|
6397
|
+
for (;;) {␊
|
|
6398
|
+
await __yieldWasmRuntimePollTurn(__pace)␊
|
|
6399
|
+
try {␊
|
|
6400
|
+
if (!__workPending()) {␊
|
|
6401
|
+
return␊
|
|
6402
|
+
}␊
|
|
6403
|
+
} catch {␊
|
|
6404
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
6405
|
+
// reachable work left. Stop polling and finish.␊
|
|
6406
|
+
return␊
|
|
6407
|
+
}␊
|
|
4584
6408
|
}␊
|
|
4585
6409
|
}␊
|
|
4586
6410
|
␊
|
|
@@ -4955,7 +6779,9 @@ Generated by [AVA](https://avajs.dev).
|
|
|
4955
6779
|
// hooks unrun. Refuse instead: nothing is flagged, the context stays␊
|
|
4956
6780
|
// registered for managed beforeExit cleanup, and a later destroy still␊
|
|
4957
6781
|
// works. dispose() coalesces reentrancy before it can get here, so this␊
|
|
4958
|
-
// is the backstop for any other caller that manages to
|
|
6782
|
+
// is the backstop for any other caller that manages to. A handshake␊
|
|
6783
|
+
// parked between the two halves of the barrier does not reach here —␊
|
|
6784
|
+
// \`__prepareEnvCleanup\` closes one rather than skipping it.␊
|
|
4959
6785
|
throw __createLifecycleReentryError('dispose')␊
|
|
4960
6786
|
}␊
|
|
4961
6787
|
__result = __emnapiContext.destroy()␊
|
|
@@ -5086,11 +6912,82 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5086
6912
|
let __wasmEnvCleanupRan = false␊
|
|
5087
6913
|
let __wasmEnvCleanupPrepared = false␊
|
|
5088
6914
|
let __wasmEnvCleanupPreparing = false␊
|
|
6915
|
+
// The closer for a barrier parked between \`…_begin\` and \`…_finish\`, set only␊
|
|
6916
|
+
// while that window is open. \`__wasmEnvCleanupPreparing\` cannot tell that␊
|
|
6917
|
+
// apart from a purely synchronous frame, which must not be re-entered and␊
|
|
6918
|
+
// which nothing outside it can finish; this window spans real event-loop␊
|
|
6919
|
+
// turns, so a caller that cannot yield — the managed beforeExit teardown —␊
|
|
6920
|
+
// can land inside it, and can close it. See \`__prepareEnvCleanup\`.␊
|
|
6921
|
+
let __finishParkedEnvCleanup␊
|
|
6922
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
6923
|
+
// queue it leaves behind is expected rather than lost.␊
|
|
6924
|
+
let __wasmEnvCleanupYielding = false␊
|
|
6925
|
+
let __wasmEnvSettlementLossReported = false␊
|
|
5089
6926
|
let __wasmEnvCleanupDrained = false␊
|
|
5090
6927
|
let __wasmEnvCleanupDrainPromise␊
|
|
5091
6928
|
const __isPreparingEnvCleanup = () => __wasmEnvCleanupPreparing␊
|
|
6929
|
+
/**␊
|
|
6930
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
6931
|
+
* could deliver them.␊
|
|
6932
|
+
*␊
|
|
6933
|
+
* Only \`dispose()\` and the initialization rollback yield the event-loop turns␊
|
|
6934
|
+
* @emnapi/core needs to dispatch its queue. Every other caller of the barrier␊
|
|
6935
|
+
* destroys in the same turn — a raw \`Context.destroy()\`, the managed␊
|
|
6936
|
+
* beforeExit teardown — and \`Context.destroy()\` runs the threadsafe␊
|
|
6937
|
+
* function's cleanup hook, which drains that queue with a null env and␊
|
|
6938
|
+
* discards it. The promises those settlements were for then hang forever,␊
|
|
6939
|
+
* silently.␊
|
|
6940
|
+
*␊
|
|
6941
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
6942
|
+
* where throwing would take the whole teardown down with it. Destroying␊
|
|
6943
|
+
* anyway is still the right trade — the queue is already unreachable by then.␊
|
|
6944
|
+
*/␊
|
|
6945
|
+
const __reportUnreachedSettlements = () => {␊
|
|
6946
|
+
if (__wasmEnvCleanupYielding || __wasmEnvSettlementLossReported) {␊
|
|
6947
|
+
return␊
|
|
6948
|
+
}␊
|
|
6949
|
+
const __pending = __napiInstance?.exports.napi_wasm_env_cleanup_pending␊
|
|
6950
|
+
if (typeof __pending !== 'function') {␊
|
|
6951
|
+
return␊
|
|
6952
|
+
}␊
|
|
6953
|
+
let __queued␊
|
|
6954
|
+
try {␊
|
|
6955
|
+
__queued = __pending()␊
|
|
6956
|
+
} catch {␊
|
|
6957
|
+
return␊
|
|
6958
|
+
}␊
|
|
6959
|
+
if (!__queued) {␊
|
|
6960
|
+
return␊
|
|
6961
|
+
}␊
|
|
6962
|
+
__wasmEnvSettlementLossReported = true␊
|
|
6963
|
+
try {␊
|
|
6964
|
+
const __consoleHost = globalThis.console␊
|
|
6965
|
+
if (__consoleHost && typeof __consoleHost.error === 'function') {␊
|
|
6966
|
+
__consoleHost.error(␊
|
|
6967
|
+
'napi-rs: the wasm environment is being destroyed with ' +␊
|
|
6968
|
+
__queued +␊
|
|
6969
|
+
' 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.',␊
|
|
6970
|
+
)␊
|
|
6971
|
+
}␊
|
|
6972
|
+
} catch {}␊
|
|
6973
|
+
}␊
|
|
5092
6974
|
const __prepareEnvCleanup = () => {␊
|
|
5093
|
-
if (__wasmEnvCleanupPrepared
|
|
6975
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
6976
|
+
return␊
|
|
6977
|
+
}␊
|
|
6978
|
+
// A handshake parked between its two halves is one this frame can close,␊
|
|
6979
|
+
// and must: every caller of this is about to destroy the context, and the␊
|
|
6980
|
+
// turns the poll is waiting for will not come. Closing it runs \`…_finish\`,␊
|
|
6981
|
+
// which is the call that joins, so this degrades to exactly the single␊
|
|
6982
|
+
// call below. Leaving it open destroys the context with the barrier still␊
|
|
6983
|
+
// raised and the runtime never joined.␊
|
|
6984
|
+
const __finishParked = __finishParkedEnvCleanup␊
|
|
6985
|
+
if (__finishParked !== undefined) {␊
|
|
6986
|
+
__finishParked()␊
|
|
6987
|
+
__reportUnreachedSettlements()␊
|
|
6988
|
+
return␊
|
|
6989
|
+
}␊
|
|
6990
|
+
if (__wasmEnvCleanupPreparing) {␊
|
|
5094
6991
|
return␊
|
|
5095
6992
|
}␊
|
|
5096
6993
|
const __prepareWasmEnvCleanup =␊
|
|
@@ -5106,9 +7003,86 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5106
7003
|
__wasmEnvCleanupPreparing = false␊
|
|
5107
7004
|
}␊
|
|
5108
7005
|
__wasmEnvCleanupRan = true␊
|
|
7006
|
+
__reportUnreachedSettlements()␊
|
|
5109
7007
|
}␊
|
|
5110
7008
|
__wasmEnvCleanupPrepared = true␊
|
|
5111
7009
|
}␊
|
|
7010
|
+
/**␊
|
|
7011
|
+
* The barrier for the callers that can yield: \`__prepareEnvCleanup\` with real␊
|
|
7012
|
+
* event-loop turns in the middle.␊
|
|
7013
|
+
*␊
|
|
7014
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
7015
|
+
* async runtime has quiesced, and the work it waits for can itself be waiting␊
|
|
7016
|
+
* for a JavaScript turn from this thread. The addon's two-phase form splits␊
|
|
7017
|
+
* that: \`…_begin\` stops the runtime without joining and reports whether␊
|
|
7018
|
+
* anything is still live, \`napi_wasm_runtime_work_pending\` answers that again␊
|
|
7019
|
+
* without blocking, and \`…_finish\` joins.␊
|
|
7020
|
+
*␊
|
|
7021
|
+
* The poll is unbounded — see \`__pollWasmRuntimeWork\` — but a caller that␊
|
|
7022
|
+
* cannot yield closes the handshake itself rather than waiting for it, so␊
|
|
7023
|
+
* \`…_finish\` still runs on every teardown path. Feature-detected like every␊
|
|
7024
|
+
* other export here, and returns nothing whenever the handshake finished␊
|
|
7025
|
+
* without yielding.␊
|
|
7026
|
+
*/␊
|
|
7027
|
+
const __prepareEnvCleanupWithTurns = () => {␊
|
|
7028
|
+
if (__wasmEnvCleanupPrepared || __wasmEnvCleanupPreparing) {␊
|
|
7029
|
+
return␊
|
|
7030
|
+
}␊
|
|
7031
|
+
const __exports = __napiInstance?.exports␊
|
|
7032
|
+
const __begin = __exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
7033
|
+
const __finish = __exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
7034
|
+
if (typeof __begin !== 'function' || typeof __finish !== 'function') {␊
|
|
7035
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
7036
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
7037
|
+
__wasmEnvCleanupYielding = true␊
|
|
7038
|
+
try {␊
|
|
7039
|
+
__prepareEnvCleanup()␊
|
|
7040
|
+
} finally {␊
|
|
7041
|
+
__wasmEnvCleanupYielding = false␊
|
|
7042
|
+
}␊
|
|
7043
|
+
return␊
|
|
7044
|
+
}␊
|
|
7045
|
+
const __workPending = __exports?.napi_wasm_runtime_work_pending␊
|
|
7046
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
7047
|
+
// from one of the JavaScript handlers they run is the same no-op it is␊
|
|
7048
|
+
// inside the single call: the barrier is up and the runtime is␊
|
|
7049
|
+
// mid-teardown, and destroying between the halves would strand exactly what␊
|
|
7050
|
+
// this delivers.␊
|
|
7051
|
+
__wasmEnvCleanupPreparing = true␊
|
|
7052
|
+
let __live␊
|
|
7053
|
+
try {␊
|
|
7054
|
+
__live = __begin()␊
|
|
7055
|
+
} catch (__error) {␊
|
|
7056
|
+
__wasmEnvCleanupPreparing = false␊
|
|
7057
|
+
throw __error␊
|
|
7058
|
+
}␊
|
|
7059
|
+
__wasmEnvCleanupRan = true␊
|
|
7060
|
+
const __finishEnvCleanup = () => {␊
|
|
7061
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
7062
|
+
// Already closed by a caller that could not yield. \`…_finish\` is␊
|
|
7063
|
+
// idempotent, but the flags it lowers are not.␊
|
|
7064
|
+
return␊
|
|
7065
|
+
}␊
|
|
7066
|
+
__finishParkedEnvCleanup = undefined␊
|
|
7067
|
+
try {␊
|
|
7068
|
+
__finish()␊
|
|
7069
|
+
} finally {␊
|
|
7070
|
+
__wasmEnvCleanupPreparing = false␊
|
|
7071
|
+
}␊
|
|
7072
|
+
__wasmEnvCleanupPrepared = true␊
|
|
7073
|
+
}␊
|
|
7074
|
+
if (!__live || typeof __workPending !== 'function') {␊
|
|
7075
|
+
__finishEnvCleanup()␊
|
|
7076
|
+
return␊
|
|
7077
|
+
}␊
|
|
7078
|
+
// Publish the closer before yielding: from here until \`__finishEnvCleanup\`␊
|
|
7079
|
+
// runs, a caller that cannot yield is entitled to end this handshake.␊
|
|
7080
|
+
__finishParkedEnvCleanup = __finishEnvCleanup␊
|
|
7081
|
+
return __pollWasmRuntimeWork(__workPending).then(␊
|
|
7082
|
+
__finishEnvCleanup,␊
|
|
7083
|
+
__finishEnvCleanup,␊
|
|
7084
|
+
)␊
|
|
7085
|
+
}␊
|
|
5112
7086
|
// The barrier + settlement drain, hoisted out of the context destroyer so the␊
|
|
5113
7087
|
// drain can yield without widening the destroyer's reentry window. Both␊
|
|
5114
7088
|
// yielding paths run it — dispose() and the initialization-failure rollback.␊
|
|
@@ -5121,6 +7095,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5121
7095
|
// is enough — and dispose() stays retryable after it rejects, so marking the␊
|
|
5122
7096
|
// drain complete up front would make the retry skip it and destroy the context␊
|
|
5123
7097
|
// with the barrier's settlements still queued.␊
|
|
7098
|
+
const __drainAfterEnvCleanup = () => {␊
|
|
7099
|
+
if (!__wasmEnvCleanupRan) {␊
|
|
7100
|
+
return␊
|
|
7101
|
+
}␊
|
|
7102
|
+
const __drained = __drainWasmEnvCleanup(__napiInstance)␊
|
|
7103
|
+
if (!__drained || typeof __drained.then !== 'function') {␊
|
|
7104
|
+
__wasmEnvCleanupDrained = true␊
|
|
7105
|
+
return␊
|
|
7106
|
+
}␊
|
|
7107
|
+
return __drained.then((__value) => {␊
|
|
7108
|
+
__wasmEnvCleanupDrained = true␊
|
|
7109
|
+
return __value␊
|
|
7110
|
+
})␊
|
|
7111
|
+
}␊
|
|
5124
7112
|
const __prepareForDisposal = () => {␊
|
|
5125
7113
|
if (__wasmEnvCleanupDrained) {␊
|
|
5126
7114
|
return␊
|
|
@@ -5128,18 +7116,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
5128
7116
|
if (__wasmEnvCleanupDrainPromise) {␊
|
|
5129
7117
|
return __wasmEnvCleanupDrainPromise␊
|
|
5130
7118
|
}␊
|
|
5131
|
-
|
|
5132
|
-
|
|
5133
|
-
|
|
5134
|
-
|
|
5135
|
-
const
|
|
5136
|
-
|
|
5137
|
-
|
|
7119
|
+
// The barrier itself can yield now, so the memo below has to cover it too:␊
|
|
7120
|
+
// a reentrant caller must join this handshake rather than start a second␊
|
|
7121
|
+
// one while the first is parked between the two halves.␊
|
|
7122
|
+
const __prepared = __prepareEnvCleanupWithTurns()␊
|
|
7123
|
+
const __settled =␊
|
|
7124
|
+
__prepared && typeof __prepared.then === 'function'␊
|
|
7125
|
+
? __prepared.then(__drainAfterEnvCleanup)␊
|
|
7126
|
+
: __drainAfterEnvCleanup()␊
|
|
7127
|
+
if (!__settled || typeof __settled.then !== 'function') {␊
|
|
5138
7128
|
return␊
|
|
5139
7129
|
}␊
|
|
5140
|
-
const __tracked =
|
|
7130
|
+
const __tracked = __settled.then(␊
|
|
5141
7131
|
(__value) => {␊
|
|
5142
|
-
__wasmEnvCleanupDrained = true␊
|
|
5143
7132
|
__wasmEnvCleanupDrainPromise = undefined␊
|
|
5144
7133
|
return __value␊
|
|
5145
7134
|
},␊
|
|
@@ -6082,6 +8071,218 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6082
8071
|
}␊
|
|
6083
8072
|
}␊
|
|
6084
8073
|
␊
|
|
8074
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same␊
|
|
8075
|
+
// reason the async-work wait uses one: this polls the addon instead of␊
|
|
8076
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin␊
|
|
8077
|
+
// the loop instead of yielding it.␊
|
|
8078
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1␊
|
|
8079
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One␊
|
|
8080
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.␊
|
|
8081
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2␊
|
|
8082
|
+
// How long a parked turn's own timer must already have been due before a␊
|
|
8083
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due␊
|
|
8084
|
+
// against the event loop's clock, which is read once per iteration, while␊
|
|
8085
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two␊
|
|
8086
|
+
// drift apart by however long the loop has been inside the current iteration.␊
|
|
8087
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50␊
|
|
8088
|
+
// How long a backup itself waits. What is left of it after the slack and one␊
|
|
8089
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a␊
|
|
8090
|
+
// parked turn from the last backup armed while the host's timers still␊
|
|
8091
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on␊
|
|
8092
|
+
// \`__armWasmRuntimePollStallBackup\`.␊
|
|
8093
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200␊
|
|
8094
|
+
␊
|
|
8095
|
+
/**␊
|
|
8096
|
+
* Pacing state for one runtime-work poll.␊
|
|
8097
|
+
*␊
|
|
8098
|
+
* Per poll, never per module: whether the host's timers arrive is not a␊
|
|
8099
|
+
* property of the module. A host can lose its timers between two disposals,␊
|
|
8100
|
+
* and in the deferred shape every instance shares this module — one healthy␊
|
|
8101
|
+
* instance must not disarm the fallback for the next one.␊
|
|
8102
|
+
*/␊
|
|
8103
|
+
function __createWasmRuntimePollPace() {␊
|
|
8104
|
+
return {␊
|
|
8105
|
+
// Timers armed by *this* poll that have actually arrived.␊
|
|
8106
|
+
arrivals: 0,␊
|
|
8107
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no␊
|
|
8108
|
+
// turn is parked — and when that turn's own timer came due.␊
|
|
8109
|
+
settleTurn: undefined,␊
|
|
8110
|
+
turnTimerDueAt: 0,␊
|
|
8111
|
+
}␊
|
|
8112
|
+
}␊
|
|
8113
|
+
␊
|
|
8114
|
+
/**␊
|
|
8115
|
+
* The backup that ends a turn whose timer is never going to arrive.␊
|
|
8116
|
+
*␊
|
|
8117
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on␊
|
|
8118
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is␊
|
|
8119
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.␊
|
|
8120
|
+
* So every turn arms one of these before it yields, and each one compares due␊
|
|
8121
|
+
* times instead of measuring how long the parked turn has been waiting.␊
|
|
8122
|
+
*␊
|
|
8123
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while␊
|
|
8124
|
+
* the host's timers still worked, and a backup ends a turn only when that␊
|
|
8125
|
+
* turn's own timer was already due a whole window before the backup itself.␊
|
|
8126
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds␊
|
|
8127
|
+
* the rescue is how far back that newest live backup is:␊
|
|
8128
|
+
*␊
|
|
8129
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a␊
|
|
8130
|
+
* turn due a whole window earlier is still parked proves that turn's timer␊
|
|
8131
|
+
* was dropped rather than merely late. That same comparison is what leaves a␊
|
|
8132
|
+
* healthy host alone: there the turn's timer has already run and cleared␊
|
|
8133
|
+
* \`settleTurn\` before any backup due after it can look.␊
|
|
8134
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer␊
|
|
8135
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both␊
|
|
8136
|
+
* primitives and the macrotask wins, so such a turn can end with its own␊
|
|
8137
|
+
* timer — and the backup armed one line before it — already dead. The␊
|
|
8138
|
+
* arrival that then flips the poll onto the timer alone can itself be a␊
|
|
8139
|
+
* timer armed before the host's timers died. So the turn that parks can sit␊
|
|
8140
|
+
* two turns past the last live arm, and the newest live backup is due␊
|
|
8141
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.␊
|
|
8142
|
+
* Arming on every turn is what holds it to two, rather than however far back␊
|
|
8143
|
+
* a throttle last let one through.␊
|
|
8144
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit␊
|
|
8145
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms␊
|
|
8146
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).␊
|
|
8147
|
+
* Past that the turn stays parked and the disposal promise never settles.␊
|
|
8148
|
+
* The bound is deliberate: reaching it takes a host that drops timers␊
|
|
8149
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither␊
|
|
8150
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does␊
|
|
8151
|
+
* the second.␊
|
|
8152
|
+
*␊
|
|
8153
|
+
* The poll then goes back to arming both primitives until two fresh arrivals␊
|
|
8154
|
+
* prove the timers again. A host that stops running the timers it has␊
|
|
8155
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays␊
|
|
8156
|
+
* pending rather than wedging the thread — the same outcome as a blocking␊
|
|
8157
|
+
* closure that never returns. Unreferenced wherever the host allows it: the␊
|
|
8158
|
+
* poll's own turn timers are what keep the loop alive, never these.␊
|
|
8159
|
+
*/␊
|
|
8160
|
+
function __armWasmRuntimePollStallBackup(__pace) {␊
|
|
8161
|
+
const __setTimer = globalThis.setTimeout␊
|
|
8162
|
+
if (typeof __setTimer !== 'function') {␊
|
|
8163
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel␊
|
|
8164
|
+
// already, and that one cannot park.␊
|
|
8165
|
+
return␊
|
|
8166
|
+
}␊
|
|
8167
|
+
// Read before arming, so this never claims to be due earlier than the timer␊
|
|
8168
|
+
// actually is: a backup ends a turn only when it is provably due after it.␊
|
|
8169
|
+
const __dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS␊
|
|
8170
|
+
let __handle␊
|
|
8171
|
+
try {␊
|
|
8172
|
+
__handle = __setTimer(() => {␊
|
|
8173
|
+
const __settleTurn = __pace.settleTurn␊
|
|
8174
|
+
if (␊
|
|
8175
|
+
!__settleTurn ||␊
|
|
8176
|
+
__pace.turnTimerDueAt > __dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS␊
|
|
8177
|
+
) {␊
|
|
8178
|
+
// No turn is parked, or the parked one's timer came due too close to␊
|
|
8179
|
+
// this backup to call it dropped — it may still arrive, and the turn␊
|
|
8180
|
+
// that armed it armed a backup due a whole window after *that*.␊
|
|
8181
|
+
return␊
|
|
8182
|
+
}␊
|
|
8183
|
+
__pace.arrivals = 0␊
|
|
8184
|
+
__pace.settleTurn = undefined␊
|
|
8185
|
+
__settleTurn()␊
|
|
8186
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)␊
|
|
8187
|
+
} catch {␊
|
|
8188
|
+
return␊
|
|
8189
|
+
}␊
|
|
8190
|
+
if (__handle && typeof __handle.unref === 'function') {␊
|
|
8191
|
+
try {␊
|
|
8192
|
+
__handle.unref()␊
|
|
8193
|
+
} catch {}␊
|
|
8194
|
+
}␊
|
|
8195
|
+
}␊
|
|
8196
|
+
␊
|
|
8197
|
+
/**␊
|
|
8198
|
+
* One turn of the runtime-work poll.␊
|
|
8199
|
+
*␊
|
|
8200
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is␊
|
|
8201
|
+
* missing or throws, but not when it is present, returns a handle and never␊
|
|
8202
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a␊
|
|
8203
|
+
* host whose timers belong to an IO context that is already gone. That host␊
|
|
8204
|
+
* would park this poll forever, and the poll is unbounded, so nothing would␊
|
|
8205
|
+
* ever call \`…_finish\`.␊
|
|
8206
|
+
*␊
|
|
8207
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and␊
|
|
8208
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host␊
|
|
8209
|
+
* with working timers therefore pays the double arming for the first turn or␊
|
|
8210
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and␊
|
|
8211
|
+
* are counted — and paces on the timer alone from then on, instead of spinning␊
|
|
8212
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,␊
|
|
8213
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop␊
|
|
8214
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,␊
|
|
8215
|
+
* which ends the parked turn and puts this poll back on both.␊
|
|
8216
|
+
*/␊
|
|
8217
|
+
function __yieldWasmRuntimePollTurn(__pace) {␊
|
|
8218
|
+
// Armed before the turn yields, and by every turn: what rescues a parked␊
|
|
8219
|
+
// turn has to have been armed while the host's timers still worked, and the␊
|
|
8220
|
+
// turn that parks is the one whose own timer is already dead.␊
|
|
8221
|
+
__armWasmRuntimePollStallBackup(__pace)␊
|
|
8222
|
+
return new Promise((resolve) => {␊
|
|
8223
|
+
let __settled = false␊
|
|
8224
|
+
const __settle = () => {␊
|
|
8225
|
+
if (__settled) {␊
|
|
8226
|
+
return␊
|
|
8227
|
+
}␊
|
|
8228
|
+
__settled = true␊
|
|
8229
|
+
if (__pace.settleTurn === __settle) {␊
|
|
8230
|
+
// Nothing is parked any more: a backup running later must not read a␊
|
|
8231
|
+
// due time this turn has already answered.␊
|
|
8232
|
+
__pace.settleTurn = undefined␊
|
|
8233
|
+
}␊
|
|
8234
|
+
resolve()␊
|
|
8235
|
+
}␊
|
|
8236
|
+
__scheduleTimer(() => {␊
|
|
8237
|
+
__pace.arrivals++␊
|
|
8238
|
+
__settle()␊
|
|
8239
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)␊
|
|
8240
|
+
// Read next to the arming it describes; see␊
|
|
8241
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.␊
|
|
8242
|
+
const __turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS␊
|
|
8243
|
+
if (__pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {␊
|
|
8244
|
+
__scheduleMacrotask(__settle)␊
|
|
8245
|
+
return␊
|
|
8246
|
+
}␊
|
|
8247
|
+
// Paced by the timer alone from here; the backup is what ends this turn if␊
|
|
8248
|
+
// the timer never arrives.␊
|
|
8249
|
+
__pace.settleTurn = __settle␊
|
|
8250
|
+
__pace.turnTimerDueAt = __turnTimerDueAt␊
|
|
8251
|
+
})␊
|
|
8252
|
+
}␊
|
|
8253
|
+
␊
|
|
8254
|
+
/**␊
|
|
8255
|
+
* Yield event-loop turns until the addon reports its runtime work finished.␊
|
|
8256
|
+
*␊
|
|
8257
|
+
* The window between \`napi_prepare_wasm_env_cleanup_begin\` and␊
|
|
8258
|
+
* \`…_finish\` — the turns are the entire point of splitting the barrier, because␊
|
|
8259
|
+
* on a threaded artifact the work \`…_finish\` joins can itself be waiting for a␊
|
|
8260
|
+
* JavaScript turn from this very thread.␊
|
|
8261
|
+
*␊
|
|
8262
|
+
* Unbounded, for the same reason the async-work wait above is: giving up means␊
|
|
8263
|
+
* calling \`…_finish\`, which joins on this thread, and the work it would join is␊
|
|
8264
|
+
* the work waiting for a turn from this thread — so a bound does not end the␊
|
|
8265
|
+
* wait, it only moves it somewhere the JavaScript thread can no longer be␊
|
|
8266
|
+
* reached. A blocking closure that never returns keeps the disposal promise␊
|
|
8267
|
+
* pending instead. The host contract is in \`crates/async-runtime/README.md\`: a␊
|
|
8268
|
+
* blocking closure must never wait on a JavaScript turn.␊
|
|
8269
|
+
*/␊
|
|
8270
|
+
async function __pollWasmRuntimeWork(__workPending) {␊
|
|
8271
|
+
const __pace = __createWasmRuntimePollPace()␊
|
|
8272
|
+
for (;;) {␊
|
|
8273
|
+
await __yieldWasmRuntimePollTurn(__pace)␊
|
|
8274
|
+
try {␊
|
|
8275
|
+
if (!__workPending()) {␊
|
|
8276
|
+
return␊
|
|
8277
|
+
}␊
|
|
8278
|
+
} catch {␊
|
|
8279
|
+
// A trap is the only way this fails, and a trapped instance has no␊
|
|
8280
|
+
// reachable work left. Stop polling and finish.␊
|
|
8281
|
+
return␊
|
|
8282
|
+
}␊
|
|
8283
|
+
}␊
|
|
8284
|
+
}␊
|
|
8285
|
+
␊
|
|
6085
8286
|
// How often to re-read \`napi_wasm_async_work_pending\` while waiting. The wait␊
|
|
6086
8287
|
// ends when the addon reports zero, so this only decides how promptly disposal␊
|
|
6087
8288
|
// notices — not how long it waits.␊
|
|
@@ -6454,7 +8655,9 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6454
8655
|
// hooks unrun. Refuse instead: nothing is flagged, the context stays␊
|
|
6455
8656
|
// registered for managed beforeExit cleanup, and a later destroy still␊
|
|
6456
8657
|
// works. dispose() coalesces reentrancy before it can get here, so this␊
|
|
6457
|
-
// is the backstop for any other caller that manages to
|
|
8658
|
+
// is the backstop for any other caller that manages to. A handshake␊
|
|
8659
|
+
// parked between the two halves of the barrier does not reach here —␊
|
|
8660
|
+
// \`__prepareEnvCleanup\` closes one rather than skipping it.␊
|
|
6458
8661
|
throw __createLifecycleReentryError('dispose')␊
|
|
6459
8662
|
}␊
|
|
6460
8663
|
__disposeHosts?.()␊
|
|
@@ -6605,11 +8808,82 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6605
8808
|
let __wasmEnvCleanupRan = false␊
|
|
6606
8809
|
let __wasmEnvCleanupPrepared = false␊
|
|
6607
8810
|
let __wasmEnvCleanupPreparing = false␊
|
|
8811
|
+
// The closer for a barrier parked between \`…_begin\` and \`…_finish\`, set only␊
|
|
8812
|
+
// while that window is open. \`__wasmEnvCleanupPreparing\` cannot tell that␊
|
|
8813
|
+
// apart from a purely synchronous frame, which must not be re-entered and␊
|
|
8814
|
+
// which nothing outside it can finish; this window spans real event-loop␊
|
|
8815
|
+
// turns, so a caller that cannot yield — the managed beforeExit teardown —␊
|
|
8816
|
+
// can land inside it, and can close it. See \`__prepareEnvCleanup\`.␊
|
|
8817
|
+
let __finishParkedEnvCleanup␊
|
|
8818
|
+
// Raised while a caller that can still yield is driving the barrier, so the␊
|
|
8819
|
+
// queue it leaves behind is expected rather than lost.␊
|
|
8820
|
+
let __wasmEnvCleanupYielding = false␊
|
|
8821
|
+
let __wasmEnvSettlementLossReported = false␊
|
|
6608
8822
|
let __wasmEnvCleanupDrained = false␊
|
|
6609
8823
|
let __wasmEnvCleanupDrainPromise␊
|
|
6610
8824
|
const __isPreparingEnvCleanup = () => __wasmEnvCleanupPreparing␊
|
|
8825
|
+
/**␊
|
|
8826
|
+
* Say so when the barrier leaves settlements queued and nothing is left that␊
|
|
8827
|
+
* could deliver them.␊
|
|
8828
|
+
*␊
|
|
8829
|
+
* Only \`dispose()\` and the initialization rollback yield the event-loop turns␊
|
|
8830
|
+
* @emnapi/core needs to dispatch its queue. Every other caller of the barrier␊
|
|
8831
|
+
* destroys in the same turn — a raw \`Context.destroy()\`, the managed␊
|
|
8832
|
+
* beforeExit teardown — and \`Context.destroy()\` runs the threadsafe␊
|
|
8833
|
+
* function's cleanup hook, which drains that queue with a null env and␊
|
|
8834
|
+
* discards it. The promises those settlements were for then hang forever,␊
|
|
8835
|
+
* silently.␊
|
|
8836
|
+
*␊
|
|
8837
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,␊
|
|
8838
|
+
* where throwing would take the whole teardown down with it. Destroying␊
|
|
8839
|
+
* anyway is still the right trade — the queue is already unreachable by then.␊
|
|
8840
|
+
*/␊
|
|
8841
|
+
const __reportUnreachedSettlements = () => {␊
|
|
8842
|
+
if (__wasmEnvCleanupYielding || __wasmEnvSettlementLossReported) {␊
|
|
8843
|
+
return␊
|
|
8844
|
+
}␊
|
|
8845
|
+
const __pending = __napiInstance?.exports.napi_wasm_env_cleanup_pending␊
|
|
8846
|
+
if (typeof __pending !== 'function') {␊
|
|
8847
|
+
return␊
|
|
8848
|
+
}␊
|
|
8849
|
+
let __queued␊
|
|
8850
|
+
try {␊
|
|
8851
|
+
__queued = __pending()␊
|
|
8852
|
+
} catch {␊
|
|
8853
|
+
return␊
|
|
8854
|
+
}␊
|
|
8855
|
+
if (!__queued) {␊
|
|
8856
|
+
return␊
|
|
8857
|
+
}␊
|
|
8858
|
+
__wasmEnvSettlementLossReported = true␊
|
|
8859
|
+
try {␊
|
|
8860
|
+
const __consoleHost = globalThis.console␊
|
|
8861
|
+
if (__consoleHost && typeof __consoleHost.error === 'function') {␊
|
|
8862
|
+
__consoleHost.error(␊
|
|
8863
|
+
'napi-rs: the wasm environment is being destroyed with ' +␊
|
|
8864
|
+
__queued +␊
|
|
8865
|
+
' 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.',␊
|
|
8866
|
+
)␊
|
|
8867
|
+
}␊
|
|
8868
|
+
} catch {}␊
|
|
8869
|
+
}␊
|
|
6611
8870
|
const __prepareEnvCleanup = () => {␊
|
|
6612
|
-
if (__wasmEnvCleanupPrepared
|
|
8871
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
8872
|
+
return␊
|
|
8873
|
+
}␊
|
|
8874
|
+
// A handshake parked between its two halves is one this frame can close,␊
|
|
8875
|
+
// and must: every caller of this is about to destroy the context, and the␊
|
|
8876
|
+
// turns the poll is waiting for will not come. Closing it runs \`…_finish\`,␊
|
|
8877
|
+
// which is the call that joins, so this degrades to exactly the single␊
|
|
8878
|
+
// call below. Leaving it open destroys the context with the barrier still␊
|
|
8879
|
+
// raised and the runtime never joined.␊
|
|
8880
|
+
const __finishParked = __finishParkedEnvCleanup␊
|
|
8881
|
+
if (__finishParked !== undefined) {␊
|
|
8882
|
+
__finishParked()␊
|
|
8883
|
+
__reportUnreachedSettlements()␊
|
|
8884
|
+
return␊
|
|
8885
|
+
}␊
|
|
8886
|
+
if (__wasmEnvCleanupPreparing) {␊
|
|
6613
8887
|
return␊
|
|
6614
8888
|
}␊
|
|
6615
8889
|
const __prepareWasmEnvCleanup =␊
|
|
@@ -6625,9 +8899,86 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6625
8899
|
__wasmEnvCleanupPreparing = false␊
|
|
6626
8900
|
}␊
|
|
6627
8901
|
__wasmEnvCleanupRan = true␊
|
|
8902
|
+
__reportUnreachedSettlements()␊
|
|
6628
8903
|
}␊
|
|
6629
8904
|
__wasmEnvCleanupPrepared = true␊
|
|
6630
8905
|
}␊
|
|
8906
|
+
/**␊
|
|
8907
|
+
* The barrier for the callers that can yield: \`__prepareEnvCleanup\` with real␊
|
|
8908
|
+
* event-loop turns in the middle.␊
|
|
8909
|
+
*␊
|
|
8910
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's␊
|
|
8911
|
+
* async runtime has quiesced, and the work it waits for can itself be waiting␊
|
|
8912
|
+
* for a JavaScript turn from this thread. The addon's two-phase form splits␊
|
|
8913
|
+
* that: \`…_begin\` stops the runtime without joining and reports whether␊
|
|
8914
|
+
* anything is still live, \`napi_wasm_runtime_work_pending\` answers that again␊
|
|
8915
|
+
* without blocking, and \`…_finish\` joins.␊
|
|
8916
|
+
*␊
|
|
8917
|
+
* The poll is unbounded — see \`__pollWasmRuntimeWork\` — but a caller that␊
|
|
8918
|
+
* cannot yield closes the handshake itself rather than waiting for it, so␊
|
|
8919
|
+
* \`…_finish\` still runs on every teardown path. Feature-detected like every␊
|
|
8920
|
+
* other export here, and returns nothing whenever the handshake finished␊
|
|
8921
|
+
* without yielding.␊
|
|
8922
|
+
*/␊
|
|
8923
|
+
const __prepareEnvCleanupWithTurns = () => {␊
|
|
8924
|
+
if (__wasmEnvCleanupPrepared || __wasmEnvCleanupPreparing) {␊
|
|
8925
|
+
return␊
|
|
8926
|
+
}␊
|
|
8927
|
+
const __exports = __napiInstance?.exports␊
|
|
8928
|
+
const __begin = __exports?.napi_prepare_wasm_env_cleanup_begin␊
|
|
8929
|
+
const __finish = __exports?.napi_prepare_wasm_env_cleanup_finish␊
|
|
8930
|
+
if (typeof __begin !== 'function' || typeof __finish !== 'function') {␊
|
|
8931
|
+
// No split to use. The settlement drain still follows this, so the queue␊
|
|
8932
|
+
// the single call leaves behind is expected rather than lost.␊
|
|
8933
|
+
__wasmEnvCleanupYielding = true␊
|
|
8934
|
+
try {␊
|
|
8935
|
+
__prepareEnvCleanup()␊
|
|
8936
|
+
} finally {␊
|
|
8937
|
+
__wasmEnvCleanupYielding = false␊
|
|
8938
|
+
}␊
|
|
8939
|
+
return␊
|
|
8940
|
+
}␊
|
|
8941
|
+
const __workPending = __exports?.napi_wasm_runtime_work_pending␊
|
|
8942
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`␊
|
|
8943
|
+
// from one of the JavaScript handlers they run is the same no-op it is␊
|
|
8944
|
+
// inside the single call: the barrier is up and the runtime is␊
|
|
8945
|
+
// mid-teardown, and destroying between the halves would strand exactly what␊
|
|
8946
|
+
// this delivers.␊
|
|
8947
|
+
__wasmEnvCleanupPreparing = true␊
|
|
8948
|
+
let __live␊
|
|
8949
|
+
try {␊
|
|
8950
|
+
__live = __begin()␊
|
|
8951
|
+
} catch (__error) {␊
|
|
8952
|
+
__wasmEnvCleanupPreparing = false␊
|
|
8953
|
+
throw __error␊
|
|
8954
|
+
}␊
|
|
8955
|
+
__wasmEnvCleanupRan = true␊
|
|
8956
|
+
const __finishEnvCleanup = () => {␊
|
|
8957
|
+
if (__wasmEnvCleanupPrepared) {␊
|
|
8958
|
+
// Already closed by a caller that could not yield. \`…_finish\` is␊
|
|
8959
|
+
// idempotent, but the flags it lowers are not.␊
|
|
8960
|
+
return␊
|
|
8961
|
+
}␊
|
|
8962
|
+
__finishParkedEnvCleanup = undefined␊
|
|
8963
|
+
try {␊
|
|
8964
|
+
__finish()␊
|
|
8965
|
+
} finally {␊
|
|
8966
|
+
__wasmEnvCleanupPreparing = false␊
|
|
8967
|
+
}␊
|
|
8968
|
+
__wasmEnvCleanupPrepared = true␊
|
|
8969
|
+
}␊
|
|
8970
|
+
if (!__live || typeof __workPending !== 'function') {␊
|
|
8971
|
+
__finishEnvCleanup()␊
|
|
8972
|
+
return␊
|
|
8973
|
+
}␊
|
|
8974
|
+
// Publish the closer before yielding: from here until \`__finishEnvCleanup\`␊
|
|
8975
|
+
// runs, a caller that cannot yield is entitled to end this handshake.␊
|
|
8976
|
+
__finishParkedEnvCleanup = __finishEnvCleanup␊
|
|
8977
|
+
return __pollWasmRuntimeWork(__workPending).then(␊
|
|
8978
|
+
__finishEnvCleanup,␊
|
|
8979
|
+
__finishEnvCleanup,␊
|
|
8980
|
+
)␊
|
|
8981
|
+
}␊
|
|
6631
8982
|
// The barrier + settlement drain, hoisted out of the context destroyer so the␊
|
|
6632
8983
|
// drain can yield without widening the destroyer's reentry window. Both␊
|
|
6633
8984
|
// yielding paths run it — dispose() and the initialization-failure rollback.␊
|
|
@@ -6640,6 +8991,20 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6640
8991
|
// is enough — and dispose() stays retryable after it rejects, so marking the␊
|
|
6641
8992
|
// drain complete up front would make the retry skip it and destroy the context␊
|
|
6642
8993
|
// with the barrier's settlements still queued.␊
|
|
8994
|
+
const __drainAfterEnvCleanup = () => {␊
|
|
8995
|
+
if (!__wasmEnvCleanupRan) {␊
|
|
8996
|
+
return␊
|
|
8997
|
+
}␊
|
|
8998
|
+
const __drained = __drainWasmEnvCleanup(__napiInstance)␊
|
|
8999
|
+
if (!__drained || typeof __drained.then !== 'function') {␊
|
|
9000
|
+
__wasmEnvCleanupDrained = true␊
|
|
9001
|
+
return␊
|
|
9002
|
+
}␊
|
|
9003
|
+
return __drained.then((__value) => {␊
|
|
9004
|
+
__wasmEnvCleanupDrained = true␊
|
|
9005
|
+
return __value␊
|
|
9006
|
+
})␊
|
|
9007
|
+
}␊
|
|
6643
9008
|
const __prepareForDisposal = () => {␊
|
|
6644
9009
|
if (__wasmEnvCleanupDrained) {␊
|
|
6645
9010
|
return␊
|
|
@@ -6647,18 +9012,19 @@ Generated by [AVA](https://avajs.dev).
|
|
|
6647
9012
|
if (__wasmEnvCleanupDrainPromise) {␊
|
|
6648
9013
|
return __wasmEnvCleanupDrainPromise␊
|
|
6649
9014
|
}␊
|
|
6650
|
-
|
|
6651
|
-
|
|
6652
|
-
|
|
6653
|
-
|
|
6654
|
-
const
|
|
6655
|
-
|
|
6656
|
-
|
|
9015
|
+
// The barrier itself can yield now, so the memo below has to cover it too:␊
|
|
9016
|
+
// a reentrant caller must join this handshake rather than start a second␊
|
|
9017
|
+
// one while the first is parked between the two halves.␊
|
|
9018
|
+
const __prepared = __prepareEnvCleanupWithTurns()␊
|
|
9019
|
+
const __settled =␊
|
|
9020
|
+
__prepared && typeof __prepared.then === 'function'␊
|
|
9021
|
+
? __prepared.then(__drainAfterEnvCleanup)␊
|
|
9022
|
+
: __drainAfterEnvCleanup()␊
|
|
9023
|
+
if (!__settled || typeof __settled.then !== 'function') {␊
|
|
6657
9024
|
return␊
|
|
6658
9025
|
}␊
|
|
6659
|
-
const __tracked =
|
|
9026
|
+
const __tracked = __settled.then(␊
|
|
6660
9027
|
(__value) => {␊
|
|
6661
|
-
__wasmEnvCleanupDrained = true␊
|
|
6662
9028
|
__wasmEnvCleanupDrainPromise = undefined␊
|
|
6663
9029
|
return __value␊
|
|
6664
9030
|
},␊
|