@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
|
@@ -163,6 +163,20 @@ let __emnapiContextDestroyed = false
|
|
|
163
163
|
let __emnapiContextDestroyPromise
|
|
164
164
|
let __emnapiWasmEnvCleanupPrepared = false
|
|
165
165
|
let __emnapiWasmEnvCleanupPreparing = false
|
|
166
|
+
// The closer for a barrier that is parked between \`…_begin\` and \`…_finish\`,
|
|
167
|
+
// set only while that window is open. \`__emnapiWasmEnvCleanupPreparing\` cannot
|
|
168
|
+
// tell those two apart on its own: it is raised both for a purely synchronous
|
|
169
|
+
// frame — which must not be re-entered, and which nothing outside it can
|
|
170
|
+
// finish — and across this window, which spans real event-loop turns, so a
|
|
171
|
+
// caller that cannot yield can land in the middle of one. That caller can close
|
|
172
|
+
// this window, because \`…_finish\` is idempotent and joins, which is exactly
|
|
173
|
+
// what the single call does. See \`__prepareWasmEnvCleanup\`.
|
|
174
|
+
let __finishParkedWasmEnvCleanup
|
|
175
|
+
// Raised while a caller that can still yield is driving the barrier, so the
|
|
176
|
+
// queue it leaves behind is expected rather than lost. See
|
|
177
|
+
// \`__reportUnreachedWasmEnvSettlements\`.
|
|
178
|
+
let __emnapiWasmEnvCleanupYielding = false
|
|
179
|
+
let __emnapiWasmEnvSettlementLossReported = false
|
|
166
180
|
let __emnapiWasmEnvCleanupRan = false
|
|
167
181
|
let __emnapiWasmEnvCleanupDrained = false
|
|
168
182
|
let __emnapiWasmEnvCleanupDrainPromise
|
|
@@ -242,7 +256,24 @@ function __isPreparingWasmEnvCleanup() {
|
|
|
242
256
|
}
|
|
243
257
|
|
|
244
258
|
function __prepareWasmEnvCleanup() {
|
|
245
|
-
if (__emnapiWasmEnvCleanupPrepared
|
|
259
|
+
if (__emnapiWasmEnvCleanupPrepared) {
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
// A handshake parked between its two halves is one this frame can close, and
|
|
263
|
+
// must: every caller of this function is about to destroy the context, and
|
|
264
|
+
// the turns the poll is waiting for will not come — an 'exit' teardown is
|
|
265
|
+
// the last thing the process runs, and \`Context.destroy()\` takes the
|
|
266
|
+
// environment away. Closing it here runs \`…_finish\`, which is the call that joins, so
|
|
267
|
+
// this degrades to exactly the single call below. Leaving it open instead
|
|
268
|
+
// destroys the context with the barrier still raised, the runtime never
|
|
269
|
+
// joined and the workers never drained.
|
|
270
|
+
const finishParked = __finishParkedWasmEnvCleanup
|
|
271
|
+
if (finishParked !== undefined) {
|
|
272
|
+
finishParked()
|
|
273
|
+
__reportUnreachedWasmEnvSettlements()
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
if (__emnapiWasmEnvCleanupPreparing) {
|
|
246
277
|
return
|
|
247
278
|
}
|
|
248
279
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
|
|
@@ -257,10 +288,57 @@ function __prepareWasmEnvCleanup() {
|
|
|
257
288
|
__emnapiWasmEnvCleanupPreparing = false
|
|
258
289
|
}
|
|
259
290
|
__emnapiWasmEnvCleanupRan = true
|
|
291
|
+
__reportUnreachedWasmEnvSettlements()
|
|
260
292
|
}
|
|
261
293
|
__emnapiWasmEnvCleanupPrepared = true
|
|
262
294
|
}
|
|
263
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Say so when the barrier leaves settlements queued and nothing is left that
|
|
298
|
+
* could deliver them.
|
|
299
|
+
*
|
|
300
|
+
* Only the disposal chain yields the event-loop turns @emnapi/core needs to
|
|
301
|
+
* dispatch its queue. Every other caller of the barrier destroys in the same
|
|
302
|
+
* turn — a raw \`Context.destroy()\`, the 'exit' teardown — and
|
|
303
|
+
* \`Context.destroy()\` runs the threadsafe function's cleanup hook, which drains
|
|
304
|
+
* that queue with a null env and discards it. The promises those settlements
|
|
305
|
+
* were for then hang forever, silently.
|
|
306
|
+
*
|
|
307
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,
|
|
308
|
+
* emnapi's own beforeExit destroy included, where throwing would take the whole
|
|
309
|
+
* teardown down with it. Destroying anyway is still the right trade — the queue
|
|
310
|
+
* is already unreachable by then.
|
|
311
|
+
*/
|
|
312
|
+
function __reportUnreachedWasmEnvSettlements() {
|
|
313
|
+
if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
|
|
317
|
+
if (typeof pending !== 'function') {
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
let queued
|
|
321
|
+
try {
|
|
322
|
+
queued = pending()
|
|
323
|
+
} catch {
|
|
324
|
+
return
|
|
325
|
+
}
|
|
326
|
+
if (!queued) {
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
__emnapiWasmEnvSettlementLossReported = true
|
|
330
|
+
try {
|
|
331
|
+
const consoleHost = globalThis.console
|
|
332
|
+
if (consoleHost && typeof consoleHost.error === 'function') {
|
|
333
|
+
consoleHost.error(
|
|
334
|
+
"napi-rs: the wasm environment is being destroyed with " +
|
|
335
|
+
queued +
|
|
336
|
+
" queued promise settlement(s). Context.destroy() discards them, so those promises never settle. Dispose with binding[Symbol.for('${WASI_DISPOSE_SYMBOL}')]() instead: only it yields the event-loop turns the settlements need.",
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
} catch {}
|
|
340
|
+
}
|
|
341
|
+
|
|
264
342
|
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
|
|
265
343
|
// on, so the drain turns below interleave with that dispatch instead of racing
|
|
266
344
|
// ahead of it on a faster queue.
|
|
@@ -310,6 +388,291 @@ function __scheduleTimer(callback, delay) {
|
|
|
310
388
|
}
|
|
311
389
|
}
|
|
312
390
|
|
|
391
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same
|
|
392
|
+
// reason the async-work drain uses one: this polls the addon instead of
|
|
393
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
|
|
394
|
+
// the loop instead of yielding it.
|
|
395
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
|
|
396
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One
|
|
397
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.
|
|
398
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
|
|
399
|
+
// How long a parked turn's own timer must already have been due before a
|
|
400
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due
|
|
401
|
+
// against the event loop's clock, which is read once per iteration, while
|
|
402
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two
|
|
403
|
+
// drift apart by however long the loop has been inside the current iteration.
|
|
404
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
|
|
405
|
+
// How long a backup itself waits. What is left of it after the slack and one
|
|
406
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a
|
|
407
|
+
// parked turn from the last backup armed while the host's timers still
|
|
408
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on
|
|
409
|
+
// \`__armWasmRuntimePollStallBackup\`.
|
|
410
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Pacing state for one runtime-work poll.
|
|
414
|
+
*
|
|
415
|
+
* Per poll, never per module: whether the host's timers arrive is not a
|
|
416
|
+
* property of the module. A host can lose its timers between two disposals,
|
|
417
|
+
* and in the deferred shape every instance shares this module — one healthy
|
|
418
|
+
* instance must not disarm the fallback for the next one.
|
|
419
|
+
*/
|
|
420
|
+
function __createWasmRuntimePollPace() {
|
|
421
|
+
return {
|
|
422
|
+
// Timers armed by *this* poll that have actually arrived.
|
|
423
|
+
arrivals: 0,
|
|
424
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no
|
|
425
|
+
// turn is parked — and when that turn's own timer came due.
|
|
426
|
+
settleTurn: undefined,
|
|
427
|
+
turnTimerDueAt: 0,
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* The backup that ends a turn whose timer is never going to arrive.
|
|
433
|
+
*
|
|
434
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on
|
|
435
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is
|
|
436
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.
|
|
437
|
+
* So every turn arms one of these before it yields, and each one compares due
|
|
438
|
+
* times instead of measuring how long the parked turn has been waiting.
|
|
439
|
+
*
|
|
440
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while
|
|
441
|
+
* the host's timers still worked, and a backup ends a turn only when that
|
|
442
|
+
* turn's own timer was already due a whole window before the backup itself.
|
|
443
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds
|
|
444
|
+
* the rescue is how far back that newest live backup is:
|
|
445
|
+
*
|
|
446
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a
|
|
447
|
+
* turn due a whole window earlier is still parked proves that turn's timer
|
|
448
|
+
* was dropped rather than merely late. That same comparison is what leaves a
|
|
449
|
+
* healthy host alone: there the turn's timer has already run and cleared
|
|
450
|
+
* \`settleTurn\` before any backup due after it can look.
|
|
451
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer
|
|
452
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both
|
|
453
|
+
* primitives and the macrotask wins, so such a turn can end with its own
|
|
454
|
+
* timer — and the backup armed one line before it — already dead. The
|
|
455
|
+
* arrival that then flips the poll onto the timer alone can itself be a
|
|
456
|
+
* timer armed before the host's timers died. So the turn that parks can sit
|
|
457
|
+
* two turns past the last live arm, and the newest live backup is due
|
|
458
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.
|
|
459
|
+
* Arming on every turn is what holds it to two, rather than however far back
|
|
460
|
+
* a throttle last let one through.
|
|
461
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
|
|
462
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms
|
|
463
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
|
|
464
|
+
* Past that the turn stays parked and the disposal promise never settles.
|
|
465
|
+
* The bound is deliberate: reaching it takes a host that drops timers
|
|
466
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
|
|
467
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does
|
|
468
|
+
* the second.
|
|
469
|
+
*
|
|
470
|
+
* The poll then goes back to arming both primitives until two fresh arrivals
|
|
471
|
+
* prove the timers again. A host that stops running the timers it has
|
|
472
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays
|
|
473
|
+
* pending rather than wedging the thread — the same outcome as a blocking
|
|
474
|
+
* closure that never returns. Unreferenced wherever the host allows it: the
|
|
475
|
+
* poll's own turn timers are what keep the loop alive, never these.
|
|
476
|
+
*/
|
|
477
|
+
function __armWasmRuntimePollStallBackup(pace) {
|
|
478
|
+
const setTimer = globalThis.setTimeout
|
|
479
|
+
if (typeof setTimer !== 'function') {
|
|
480
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel
|
|
481
|
+
// already, and that one cannot park.
|
|
482
|
+
return
|
|
483
|
+
}
|
|
484
|
+
// Read before arming, so this never claims to be due earlier than the timer
|
|
485
|
+
// actually is: a backup ends a turn only when it is provably due after it.
|
|
486
|
+
const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
|
|
487
|
+
let handle
|
|
488
|
+
try {
|
|
489
|
+
handle = setTimer(() => {
|
|
490
|
+
const settleTurn = pace.settleTurn
|
|
491
|
+
if (
|
|
492
|
+
!settleTurn ||
|
|
493
|
+
pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
|
|
494
|
+
) {
|
|
495
|
+
// No turn is parked, or the parked one's timer came due too close to
|
|
496
|
+
// this backup to call it dropped — it may still arrive, and the turn
|
|
497
|
+
// that armed it armed a backup due a whole window after *that*.
|
|
498
|
+
return
|
|
499
|
+
}
|
|
500
|
+
pace.arrivals = 0
|
|
501
|
+
pace.settleTurn = undefined
|
|
502
|
+
settleTurn()
|
|
503
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
|
|
504
|
+
} catch {
|
|
505
|
+
return
|
|
506
|
+
}
|
|
507
|
+
if (handle && typeof handle.unref === 'function') {
|
|
508
|
+
try {
|
|
509
|
+
handle.unref()
|
|
510
|
+
} catch {}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* One turn of the runtime-work poll.
|
|
516
|
+
*
|
|
517
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is
|
|
518
|
+
* missing or throws, but not when it is present, returns a handle and never
|
|
519
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a
|
|
520
|
+
* host whose timers belong to an IO context that is already gone. That host
|
|
521
|
+
* would park this poll forever, and the poll is unbounded, so nothing would
|
|
522
|
+
* ever call \`…_finish\`.
|
|
523
|
+
*
|
|
524
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and
|
|
525
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host
|
|
526
|
+
* with working timers therefore pays the double arming for the first turn or
|
|
527
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and
|
|
528
|
+
* are counted — and paces on the timer alone from then on, instead of spinning
|
|
529
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,
|
|
530
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop
|
|
531
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,
|
|
532
|
+
* which ends the parked turn and puts this poll back on both.
|
|
533
|
+
*/
|
|
534
|
+
function __yieldWasmRuntimePollTurn(pace) {
|
|
535
|
+
// Armed before the turn yields, and by every turn: what rescues a parked
|
|
536
|
+
// turn has to have been armed while the host's timers still worked, and the
|
|
537
|
+
// turn that parks is the one whose own timer is already dead.
|
|
538
|
+
__armWasmRuntimePollStallBackup(pace)
|
|
539
|
+
return new Promise((resolve) => {
|
|
540
|
+
let settled = false
|
|
541
|
+
const settle = () => {
|
|
542
|
+
if (settled) {
|
|
543
|
+
return
|
|
544
|
+
}
|
|
545
|
+
settled = true
|
|
546
|
+
if (pace.settleTurn === settle) {
|
|
547
|
+
// Nothing is parked any more: a backup running later must not read a
|
|
548
|
+
// due time this turn has already answered.
|
|
549
|
+
pace.settleTurn = undefined
|
|
550
|
+
}
|
|
551
|
+
resolve()
|
|
552
|
+
}
|
|
553
|
+
__scheduleTimer(() => {
|
|
554
|
+
pace.arrivals++
|
|
555
|
+
settle()
|
|
556
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
|
|
557
|
+
// Read next to the arming it describes; see
|
|
558
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.
|
|
559
|
+
const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
|
|
560
|
+
if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
|
|
561
|
+
__scheduleMacrotask(settle)
|
|
562
|
+
return
|
|
563
|
+
}
|
|
564
|
+
// Paced by the timer alone from here; the backup is what ends this turn if
|
|
565
|
+
// the timer never arrives.
|
|
566
|
+
pace.settleTurn = settle
|
|
567
|
+
pace.turnTimerDueAt = turnTimerDueAt
|
|
568
|
+
})
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* The barrier for callers that can yield: \`__prepareWasmEnvCleanup\` with real
|
|
573
|
+
* event-loop turns in the middle.
|
|
574
|
+
*
|
|
575
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's
|
|
576
|
+
* async runtime has quiesced, and on \`wasm32-wasip1-threads\` the thread it
|
|
577
|
+
* waits on is this one, the only thread that can give a running blocking
|
|
578
|
+
* closure the JavaScript turn *it* is waiting for. A single call there can wait
|
|
579
|
+
* for work that can never finish. The addon's two-phase form splits that:
|
|
580
|
+
* \`…_begin\` stops the runtime without joining and reports whether anything is
|
|
581
|
+
* still live, \`napi_wasm_runtime_work_pending\` answers that question again
|
|
582
|
+
* without blocking, and \`…_finish\` joins. The turns yielded in between are the
|
|
583
|
+
* entire point.
|
|
584
|
+
*
|
|
585
|
+
* The poll has no deadline, for the same reason the async-work drain below has
|
|
586
|
+
* none: giving up means calling \`…_finish\`, which joins on this thread, and the
|
|
587
|
+
* work it would join is the work that is waiting for a turn from this thread —
|
|
588
|
+
* so a bound does not end the wait, it only moves it somewhere the JavaScript
|
|
589
|
+
* thread can no longer be reached. A blocking closure that never returns keeps
|
|
590
|
+
* the disposal promise pending instead, exactly as a task whose \`execute\` never
|
|
591
|
+
* returns already keeps an *undisposed* process alive. The host contract is in
|
|
592
|
+
* \`crates/async-runtime/README.md\`: a blocking closure must never wait on a
|
|
593
|
+
* JavaScript turn. The process-exit path still blocks in \`…_finish\`, because it
|
|
594
|
+
* has no turns left to give (see \`__prepareWasmEnvCleanup\`).
|
|
595
|
+
*
|
|
596
|
+
* Feature-detected like every other export in this teardown, so an addon built
|
|
597
|
+
* against a napi crate that predates the split keeps the single blocking call.
|
|
598
|
+
* Returns nothing whenever the handshake finished without yielding, which keeps
|
|
599
|
+
* an idle disposal synchronous.
|
|
600
|
+
*/
|
|
601
|
+
function __prepareWasmEnvCleanupWithTurns() {
|
|
602
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
|
|
603
|
+
return
|
|
604
|
+
}
|
|
605
|
+
const exports = __napiInstance?.exports
|
|
606
|
+
const begin = exports?.napi_prepare_wasm_env_cleanup_begin
|
|
607
|
+
const finish = exports?.napi_prepare_wasm_env_cleanup_finish
|
|
608
|
+
if (typeof begin !== 'function' || typeof finish !== 'function') {
|
|
609
|
+
// No split to use. The settlement drain still follows this, so the queue
|
|
610
|
+
// the single call leaves behind is expected rather than lost.
|
|
611
|
+
__emnapiWasmEnvCleanupYielding = true
|
|
612
|
+
try {
|
|
613
|
+
__prepareWasmEnvCleanup()
|
|
614
|
+
} finally {
|
|
615
|
+
__emnapiWasmEnvCleanupYielding = false
|
|
616
|
+
}
|
|
617
|
+
return
|
|
618
|
+
}
|
|
619
|
+
const workPending = exports?.napi_wasm_runtime_work_pending
|
|
620
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`
|
|
621
|
+
// from one of the JavaScript handlers they run is the same no-op it is inside
|
|
622
|
+
// the single call: the barrier is up and the runtime is mid-teardown, and
|
|
623
|
+
// destroying between the halves would strand exactly what this delivers.
|
|
624
|
+
__emnapiWasmEnvCleanupPreparing = true
|
|
625
|
+
let live
|
|
626
|
+
try {
|
|
627
|
+
live = begin()
|
|
628
|
+
} catch (error) {
|
|
629
|
+
__emnapiWasmEnvCleanupPreparing = false
|
|
630
|
+
throw error
|
|
631
|
+
}
|
|
632
|
+
__emnapiWasmEnvCleanupRan = true
|
|
633
|
+
const finishCleanup = () => {
|
|
634
|
+
if (__emnapiWasmEnvCleanupPrepared) {
|
|
635
|
+
// Already closed by a caller that could not yield — the 'exit' teardown
|
|
636
|
+
// reached \`__prepareWasmEnvCleanup\` while this poll was parked. \`…_finish\`
|
|
637
|
+
// is idempotent, but the flags it lowers are not: running it again here
|
|
638
|
+
// would clear a \`preparing\` some later barrier had raised.
|
|
639
|
+
return
|
|
640
|
+
}
|
|
641
|
+
__finishParkedWasmEnvCleanup = undefined
|
|
642
|
+
try {
|
|
643
|
+
finish()
|
|
644
|
+
} finally {
|
|
645
|
+
__emnapiWasmEnvCleanupPreparing = false
|
|
646
|
+
}
|
|
647
|
+
__emnapiWasmEnvCleanupPrepared = true
|
|
648
|
+
}
|
|
649
|
+
if (!live || typeof workPending !== 'function') {
|
|
650
|
+
finishCleanup()
|
|
651
|
+
return
|
|
652
|
+
}
|
|
653
|
+
// Publish the closer before yielding: from here until \`finishCleanup\` runs,
|
|
654
|
+
// a caller that cannot yield is entitled to end this handshake itself.
|
|
655
|
+
__finishParkedWasmEnvCleanup = finishCleanup
|
|
656
|
+
return (async () => {
|
|
657
|
+
// Unbounded, exactly like the async-work drain below. The wait ends when
|
|
658
|
+
// the addon reports its runtime work finished; the turns spent here are
|
|
659
|
+
// what let that happen at all.
|
|
660
|
+
const pace = __createWasmRuntimePollPace()
|
|
661
|
+
for (;;) {
|
|
662
|
+
await __yieldWasmRuntimePollTurn(pace)
|
|
663
|
+
try {
|
|
664
|
+
if (!workPending()) {
|
|
665
|
+
return
|
|
666
|
+
}
|
|
667
|
+
} catch {
|
|
668
|
+
// A trap is the only way this fails, and a trapped instance has no
|
|
669
|
+
// reachable work left. Stop polling and finish.
|
|
670
|
+
return
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
})().then(finishCleanup, finishCleanup)
|
|
674
|
+
}
|
|
675
|
+
|
|
313
676
|
// Turns to wait for while the addon still reports queued settlements. Reaching
|
|
314
677
|
// zero is the only success. A counter still nonzero at this bound rejects the
|
|
315
678
|
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than
|
|
@@ -447,6 +810,19 @@ function __destroyEmnapiContext() {
|
|
|
447
810
|
|
|
448
811
|
${disposeCurrentThreadHosts}\
|
|
449
812
|
__prepareWasmEnvCleanup()
|
|
813
|
+
if (__isPreparingWasmEnvCleanup()) {
|
|
814
|
+
// Reached from inside the synchronous barrier — a promise hook one of the
|
|
815
|
+
// settlements above ran, which is the reentrancy the destroy wrapper
|
|
816
|
+
// exists for. \`Context.destroy()\` below would hit that wrapper's in-flight
|
|
817
|
+
// no-op and answer \`undefined\`, and recording that as a completed destroy
|
|
818
|
+
// is what makes the frame that *did* start the barrier skip the real one
|
|
819
|
+
// afterwards, leaving the context retained with its cleanup hooks unrun.
|
|
820
|
+
// Refuse instead: nothing is flagged, and that frame destroys for real the
|
|
821
|
+
// moment it returns. The deferred loader carries the same backstop. A
|
|
822
|
+
// parked handshake cannot get here — \`__prepareWasmEnvCleanup\` closes one
|
|
823
|
+
// rather than skipping it.
|
|
824
|
+
return
|
|
825
|
+
}
|
|
450
826
|
const result = __emnapiContext.destroy()
|
|
451
827
|
if (!__isThenable(result)) {
|
|
452
828
|
__emnapiContextDestroyed = true
|
|
@@ -720,11 +1096,7 @@ function __continueWasiDisposal() {
|
|
|
720
1096
|
return __finishWasiDisposal()
|
|
721
1097
|
}
|
|
722
1098
|
|
|
723
|
-
function
|
|
724
|
-
// Run the pre-teardown barrier, then let the settlements it queued actually
|
|
725
|
-
// reach JavaScript, and only then destroy the environment. Doing these two
|
|
726
|
-
// back to back is what strands them.
|
|
727
|
-
__prepareWasmEnvCleanup()
|
|
1099
|
+
function __drainWasmEnvForWasiDisposal() {
|
|
728
1100
|
const drainResult = __drainWasmEnvCleanup()
|
|
729
1101
|
if (__isThenable(drainResult)) {
|
|
730
1102
|
return Promise.resolve(drainResult).then(__continueWasiDisposal)
|
|
@@ -732,6 +1104,18 @@ function __cleanUpWasmEnvForWasiDisposal() {
|
|
|
732
1104
|
return __continueWasiDisposal()
|
|
733
1105
|
}
|
|
734
1106
|
|
|
1107
|
+
function __cleanUpWasmEnvForWasiDisposal() {
|
|
1108
|
+
// Run the pre-teardown barrier — yielding the turns its two-phase form asks
|
|
1109
|
+
// for, when the addon has one — then let the settlements it queued actually
|
|
1110
|
+
// reach JavaScript, and only then destroy the environment. Doing any two of
|
|
1111
|
+
// these back to back is what strands them.
|
|
1112
|
+
const prepareResult = __prepareWasmEnvCleanupWithTurns()
|
|
1113
|
+
if (__isThenable(prepareResult)) {
|
|
1114
|
+
return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)
|
|
1115
|
+
}
|
|
1116
|
+
return __drainWasmEnvForWasiDisposal()
|
|
1117
|
+
}
|
|
1118
|
+
|
|
735
1119
|
function __startWasiDisposal() {
|
|
736
1120
|
// Outstanding \`napi_async_work\` goes first, while the environment is still
|
|
737
1121
|
// completely live: the completion callbacks run addon code, and everything
|
|
@@ -892,14 +1276,36 @@ function __rollbackWasiInitialization() {
|
|
|
892
1276
|
// be reached without the async-work drain below running first.
|
|
893
1277
|
function __rollbackWasmEnvForWasiInitialization() {
|
|
894
1278
|
const cleanupErrors = []
|
|
1279
|
+
let prepareResult
|
|
1280
|
+
try {
|
|
1281
|
+
prepareResult = __prepareWasmEnvCleanupWithTurns()
|
|
1282
|
+
} catch (cleanupError) {
|
|
1283
|
+
cleanupErrors.push(cleanupError)
|
|
1284
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1285
|
+
}
|
|
1286
|
+
if (__isThenable(prepareResult)) {
|
|
1287
|
+
return Promise.resolve(prepareResult).then(
|
|
1288
|
+
() => __drainWasmEnvForWasiRollback(cleanupErrors),
|
|
1289
|
+
(cleanupError) => {
|
|
1290
|
+
cleanupErrors.push(cleanupError)
|
|
1291
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1292
|
+
},
|
|
1293
|
+
)
|
|
1294
|
+
}
|
|
1295
|
+
return __drainWasmEnvForWasiRollback(cleanupErrors)
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// The settlement drain of the rollback above, reached either straight away or
|
|
1299
|
+
// after the barrier's two-phase form has yielded its turns. A barrier that
|
|
1300
|
+
// did not finish never gets here: it retains instead, exactly as a drain that
|
|
1301
|
+
// did not finish does.
|
|
1302
|
+
function __drainWasmEnvForWasiRollback(cleanupErrors) {
|
|
895
1303
|
let drainResult
|
|
896
|
-
let settlementsUnreached = false
|
|
897
1304
|
try {
|
|
898
|
-
__prepareWasmEnvCleanup()
|
|
899
1305
|
drainResult = __drainWasmEnvCleanup()
|
|
900
1306
|
} catch (cleanupError) {
|
|
901
1307
|
cleanupErrors.push(cleanupError)
|
|
902
|
-
|
|
1308
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
903
1309
|
}
|
|
904
1310
|
if (__isThenable(drainResult)) {
|
|
905
1311
|
return Promise.resolve(drainResult).then(
|
|
@@ -910,9 +1316,6 @@ function __rollbackWasiInitialization() {
|
|
|
910
1316
|
},
|
|
911
1317
|
)
|
|
912
1318
|
}
|
|
913
|
-
if (settlementsUnreached) {
|
|
914
|
-
return __retainFailedWasiRollback(cleanupErrors)
|
|
915
|
-
}
|
|
916
1319
|
return __destroyContextForWasiRollback(cleanupErrors)
|
|
917
1320
|
}
|
|
918
1321
|
|
|
@@ -1716,6 +2119,218 @@ function __scheduleTimer(__callback, __delay) {
|
|
|
1716
2119
|
}
|
|
1717
2120
|
}
|
|
1718
2121
|
|
|
2122
|
+
// A real, referenced timer rather than a zero-delay macrotask, for the same
|
|
2123
|
+
// reason the async-work wait uses one: this polls the addon instead of
|
|
2124
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
|
|
2125
|
+
// the loop instead of yielding it.
|
|
2126
|
+
const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
|
|
2127
|
+
// Arrivals it takes before the poll paces on the host's timers alone. One
|
|
2128
|
+
// proves nothing: a timer armed before the host's timers stopped still fires.
|
|
2129
|
+
const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
|
|
2130
|
+
// How long a parked turn's own timer must already have been due before a
|
|
2131
|
+
// backup that runs calls it dropped. Slack, not a deadline: a timer is due
|
|
2132
|
+
// against the event loop's clock, which is read once per iteration, while
|
|
2133
|
+
// these are \`Date.now()\` readings taken part-way through one, so the two
|
|
2134
|
+
// drift apart by however long the loop has been inside the current iteration.
|
|
2135
|
+
const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
|
|
2136
|
+
// How long a backup itself waits. What is left of it after the slack and one
|
|
2137
|
+
// interval — 149 ms — has to cover the *two* poll turns that can separate a
|
|
2138
|
+
// parked turn from the last backup armed while the host's timers still
|
|
2139
|
+
// worked, so the ceiling on a single turn is half of it. See the invariant on
|
|
2140
|
+
// \`__armWasmRuntimePollStallBackup\`.
|
|
2141
|
+
const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
|
|
2142
|
+
|
|
2143
|
+
/**
|
|
2144
|
+
* Pacing state for one runtime-work poll.
|
|
2145
|
+
*
|
|
2146
|
+
* Per poll, never per module: whether the host's timers arrive is not a
|
|
2147
|
+
* property of the module. A host can lose its timers between two disposals,
|
|
2148
|
+
* and in the deferred shape every instance shares this module — one healthy
|
|
2149
|
+
* instance must not disarm the fallback for the next one.
|
|
2150
|
+
*/
|
|
2151
|
+
function __createWasmRuntimePollPace() {
|
|
2152
|
+
return {
|
|
2153
|
+
// Timers armed by *this* poll that have actually arrived.
|
|
2154
|
+
arrivals: 0,
|
|
2155
|
+
// The turn waiting on a timer alone *right now* — undefined whenever no
|
|
2156
|
+
// turn is parked — and when that turn's own timer came due.
|
|
2157
|
+
settleTurn: undefined,
|
|
2158
|
+
turnTimerDueAt: 0,
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
/**
|
|
2163
|
+
* The backup that ends a turn whose timer is never going to arrive.
|
|
2164
|
+
*
|
|
2165
|
+
* Once the poll paces on the timer alone it has nothing left to fall back on
|
|
2166
|
+
* if the host's timers stop mid-poll: the turn that armed the dead timer is
|
|
2167
|
+
* the turn that parks, and a parked poll schedules nothing that could notice.
|
|
2168
|
+
* So every turn arms one of these before it yields, and each one compares due
|
|
2169
|
+
* times instead of measuring how long the parked turn has been waiting.
|
|
2170
|
+
*
|
|
2171
|
+
* Invariant: a parked turn is ended by the newest backup that was armed while
|
|
2172
|
+
* the host's timers still worked, and a backup ends a turn only when that
|
|
2173
|
+
* turn's own timer was already due a whole window before the backup itself.
|
|
2174
|
+
* Neither half turns on how far apart the arms happen to fall — what bounds
|
|
2175
|
+
* the rescue is how far back that newest live backup is:
|
|
2176
|
+
*
|
|
2177
|
+
* - *Ends it.* Hosts run timers in due order, so a backup that runs while a
|
|
2178
|
+
* turn due a whole window earlier is still parked proves that turn's timer
|
|
2179
|
+
* was dropped rather than merely late. That same comparison is what leaves a
|
|
2180
|
+
* healthy host alone: there the turn's timer has already run and cleared
|
|
2181
|
+
* \`settleTurn\` before any backup due after it can look.
|
|
2182
|
+
* - *Two turns back, not one.* A turn that ended does not prove its own timer
|
|
2183
|
+
* arrived: until \`…_TRUSTED_ARRIVALS\` is reached every turn arms both
|
|
2184
|
+
* primitives and the macrotask wins, so such a turn can end with its own
|
|
2185
|
+
* timer — and the backup armed one line before it — already dead. The
|
|
2186
|
+
* arrival that then flips the poll onto the timer alone can itself be a
|
|
2187
|
+
* timer armed before the host's timers died. So the turn that parks can sit
|
|
2188
|
+
* two turns past the last live arm, and the newest live backup is due
|
|
2189
|
+
* \`…_BACKUP_MS\` less *two* turn lengths after that turn's own timer.
|
|
2190
|
+
* Arming on every turn is what holds it to two, rather than however far back
|
|
2191
|
+
* a throttle last let one through.
|
|
2192
|
+
* - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
|
|
2193
|
+
* inside \`…_BACKUP_MS\` less the slack and one interval: 149 ms, so 74 ms
|
|
2194
|
+
* per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
|
|
2195
|
+
* Past that the turn stays parked and the disposal promise never settles.
|
|
2196
|
+
* The bound is deliberate: reaching it takes a host that drops timers
|
|
2197
|
+
* mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
|
|
2198
|
+
* Node nor WebContainer — the hosts that run the threaded artifact — does
|
|
2199
|
+
* the second.
|
|
2200
|
+
*
|
|
2201
|
+
* The poll then goes back to arming both primitives until two fresh arrivals
|
|
2202
|
+
* prove the timers again. A host that stops running the timers it has
|
|
2203
|
+
* *already* accepted leaves nothing to fire, and the disposal promise stays
|
|
2204
|
+
* pending rather than wedging the thread — the same outcome as a blocking
|
|
2205
|
+
* closure that never returns. Unreferenced wherever the host allows it: the
|
|
2206
|
+
* poll's own turn timers are what keep the loop alive, never these.
|
|
2207
|
+
*/
|
|
2208
|
+
function __armWasmRuntimePollStallBackup(__pace) {
|
|
2209
|
+
const __setTimer = globalThis.setTimeout
|
|
2210
|
+
if (typeof __setTimer !== 'function') {
|
|
2211
|
+
// Nothing to back up: \`__scheduleTimer\` is on the macrotask channel
|
|
2212
|
+
// already, and that one cannot park.
|
|
2213
|
+
return
|
|
2214
|
+
}
|
|
2215
|
+
// Read before arming, so this never claims to be due earlier than the timer
|
|
2216
|
+
// actually is: a backup ends a turn only when it is provably due after it.
|
|
2217
|
+
const __dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
|
|
2218
|
+
let __handle
|
|
2219
|
+
try {
|
|
2220
|
+
__handle = __setTimer(() => {
|
|
2221
|
+
const __settleTurn = __pace.settleTurn
|
|
2222
|
+
if (
|
|
2223
|
+
!__settleTurn ||
|
|
2224
|
+
__pace.turnTimerDueAt > __dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
|
|
2225
|
+
) {
|
|
2226
|
+
// No turn is parked, or the parked one's timer came due too close to
|
|
2227
|
+
// this backup to call it dropped — it may still arrive, and the turn
|
|
2228
|
+
// that armed it armed a backup due a whole window after *that*.
|
|
2229
|
+
return
|
|
2230
|
+
}
|
|
2231
|
+
__pace.arrivals = 0
|
|
2232
|
+
__pace.settleTurn = undefined
|
|
2233
|
+
__settleTurn()
|
|
2234
|
+
}, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
|
|
2235
|
+
} catch {
|
|
2236
|
+
return
|
|
2237
|
+
}
|
|
2238
|
+
if (__handle && typeof __handle.unref === 'function') {
|
|
2239
|
+
try {
|
|
2240
|
+
__handle.unref()
|
|
2241
|
+
} catch {}
|
|
2242
|
+
}
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
/**
|
|
2246
|
+
* One turn of the runtime-work poll.
|
|
2247
|
+
*
|
|
2248
|
+
* \`__scheduleTimer\` falls back to the macrotask scheduler when \`setTimeout\` is
|
|
2249
|
+
* missing or throws, but not when it is present, returns a handle and never
|
|
2250
|
+
* fires — fake timers in a test suite that disposes from an \`afterEach\`, or a
|
|
2251
|
+
* host whose timers belong to an IO context that is already gone. That host
|
|
2252
|
+
* would park this poll forever, and the poll is unbounded, so nothing would
|
|
2253
|
+
* ever call \`…_finish\`.
|
|
2254
|
+
*
|
|
2255
|
+
* Arm both primitives until timers armed by this poll have arrived twice, and
|
|
2256
|
+
* let whichever lands first end the turn; the loser resolves nothing. A host
|
|
2257
|
+
* with working timers therefore pays the double arming for the first turn or
|
|
2258
|
+
* two — the macrotask wins the race, but the timers behind it still arrive and
|
|
2259
|
+
* are counted — and paces on the timer alone from then on, instead of spinning
|
|
2260
|
+
* the loop on a zero-delay queue. A host whose timers never arrive keeps both,
|
|
2261
|
+
* and the macrotask is what keeps the poll moving. A host whose timers stop
|
|
2262
|
+
* after proving themselves is caught by \`__armWasmRuntimePollStallBackup\`,
|
|
2263
|
+
* which ends the parked turn and puts this poll back on both.
|
|
2264
|
+
*/
|
|
2265
|
+
function __yieldWasmRuntimePollTurn(__pace) {
|
|
2266
|
+
// Armed before the turn yields, and by every turn: what rescues a parked
|
|
2267
|
+
// turn has to have been armed while the host's timers still worked, and the
|
|
2268
|
+
// turn that parks is the one whose own timer is already dead.
|
|
2269
|
+
__armWasmRuntimePollStallBackup(__pace)
|
|
2270
|
+
return new Promise((resolve) => {
|
|
2271
|
+
let __settled = false
|
|
2272
|
+
const __settle = () => {
|
|
2273
|
+
if (__settled) {
|
|
2274
|
+
return
|
|
2275
|
+
}
|
|
2276
|
+
__settled = true
|
|
2277
|
+
if (__pace.settleTurn === __settle) {
|
|
2278
|
+
// Nothing is parked any more: a backup running later must not read a
|
|
2279
|
+
// due time this turn has already answered.
|
|
2280
|
+
__pace.settleTurn = undefined
|
|
2281
|
+
}
|
|
2282
|
+
resolve()
|
|
2283
|
+
}
|
|
2284
|
+
__scheduleTimer(() => {
|
|
2285
|
+
__pace.arrivals++
|
|
2286
|
+
__settle()
|
|
2287
|
+
}, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
|
|
2288
|
+
// Read next to the arming it describes; see
|
|
2289
|
+
// \`__armWasmRuntimePollStallBackup\` for what the two due times mean.
|
|
2290
|
+
const __turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
|
|
2291
|
+
if (__pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
|
|
2292
|
+
__scheduleMacrotask(__settle)
|
|
2293
|
+
return
|
|
2294
|
+
}
|
|
2295
|
+
// Paced by the timer alone from here; the backup is what ends this turn if
|
|
2296
|
+
// the timer never arrives.
|
|
2297
|
+
__pace.settleTurn = __settle
|
|
2298
|
+
__pace.turnTimerDueAt = __turnTimerDueAt
|
|
2299
|
+
})
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
/**
|
|
2303
|
+
* Yield event-loop turns until the addon reports its runtime work finished.
|
|
2304
|
+
*
|
|
2305
|
+
* The window between \`napi_prepare_wasm_env_cleanup_begin\` and
|
|
2306
|
+
* \`…_finish\` — the turns are the entire point of splitting the barrier, because
|
|
2307
|
+
* on a threaded artifact the work \`…_finish\` joins can itself be waiting for a
|
|
2308
|
+
* JavaScript turn from this very thread.
|
|
2309
|
+
*
|
|
2310
|
+
* Unbounded, for the same reason the async-work wait above is: giving up means
|
|
2311
|
+
* calling \`…_finish\`, which joins on this thread, and the work it would join is
|
|
2312
|
+
* the work waiting for a turn from this thread — so a bound does not end the
|
|
2313
|
+
* wait, it only moves it somewhere the JavaScript thread can no longer be
|
|
2314
|
+
* reached. A blocking closure that never returns keeps the disposal promise
|
|
2315
|
+
* pending instead. The host contract is in \`crates/async-runtime/README.md\`: a
|
|
2316
|
+
* blocking closure must never wait on a JavaScript turn.
|
|
2317
|
+
*/
|
|
2318
|
+
async function __pollWasmRuntimeWork(__workPending) {
|
|
2319
|
+
const __pace = __createWasmRuntimePollPace()
|
|
2320
|
+
for (;;) {
|
|
2321
|
+
await __yieldWasmRuntimePollTurn(__pace)
|
|
2322
|
+
try {
|
|
2323
|
+
if (!__workPending()) {
|
|
2324
|
+
return
|
|
2325
|
+
}
|
|
2326
|
+
} catch {
|
|
2327
|
+
// A trap is the only way this fails, and a trapped instance has no
|
|
2328
|
+
// reachable work left. Stop polling and finish.
|
|
2329
|
+
return
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
|
|
1719
2334
|
// How often to re-read \`napi_wasm_async_work_pending\` while waiting. The wait
|
|
1720
2335
|
// ends when the addon reports zero, so this only decides how promptly disposal
|
|
1721
2336
|
// notices — not how long it waits.
|
|
@@ -2087,7 +2702,9 @@ ${managedHostDisposeParam}) {
|
|
|
2087
2702
|
// hooks unrun. Refuse instead: nothing is flagged, the context stays
|
|
2088
2703
|
// registered for managed beforeExit cleanup, and a later destroy still
|
|
2089
2704
|
// works. dispose() coalesces reentrancy before it can get here, so this
|
|
2090
|
-
// is the backstop for any other caller that manages to.
|
|
2705
|
+
// is the backstop for any other caller that manages to. A handshake
|
|
2706
|
+
// parked between the two halves of the barrier does not reach here —
|
|
2707
|
+
// \`__prepareEnvCleanup\` closes one rather than skipping it.
|
|
2091
2708
|
throw __createLifecycleReentryError('dispose')
|
|
2092
2709
|
}
|
|
2093
2710
|
${managedHostDisposeCall}\
|
|
@@ -2220,11 +2837,82 @@ ${instanceHostState}\
|
|
|
2220
2837
|
let __wasmEnvCleanupRan = false
|
|
2221
2838
|
let __wasmEnvCleanupPrepared = false
|
|
2222
2839
|
let __wasmEnvCleanupPreparing = false
|
|
2840
|
+
// The closer for a barrier parked between \`…_begin\` and \`…_finish\`, set only
|
|
2841
|
+
// while that window is open. \`__wasmEnvCleanupPreparing\` cannot tell that
|
|
2842
|
+
// apart from a purely synchronous frame, which must not be re-entered and
|
|
2843
|
+
// which nothing outside it can finish; this window spans real event-loop
|
|
2844
|
+
// turns, so a caller that cannot yield — the managed beforeExit teardown —
|
|
2845
|
+
// can land inside it, and can close it. See \`__prepareEnvCleanup\`.
|
|
2846
|
+
let __finishParkedEnvCleanup
|
|
2847
|
+
// Raised while a caller that can still yield is driving the barrier, so the
|
|
2848
|
+
// queue it leaves behind is expected rather than lost.
|
|
2849
|
+
let __wasmEnvCleanupYielding = false
|
|
2850
|
+
let __wasmEnvSettlementLossReported = false
|
|
2223
2851
|
let __wasmEnvCleanupDrained = false
|
|
2224
2852
|
let __wasmEnvCleanupDrainPromise
|
|
2225
2853
|
const __isPreparingEnvCleanup = () => __wasmEnvCleanupPreparing
|
|
2854
|
+
/**
|
|
2855
|
+
* Say so when the barrier leaves settlements queued and nothing is left that
|
|
2856
|
+
* could deliver them.
|
|
2857
|
+
*
|
|
2858
|
+
* Only \`dispose()\` and the initialization rollback yield the event-loop turns
|
|
2859
|
+
* @emnapi/core needs to dispatch its queue. Every other caller of the barrier
|
|
2860
|
+
* destroys in the same turn — a raw \`Context.destroy()\`, the managed
|
|
2861
|
+
* beforeExit teardown — and \`Context.destroy()\` runs the threadsafe
|
|
2862
|
+
* function's cleanup hook, which drains that queue with a null env and
|
|
2863
|
+
* discards it. The promises those settlements were for then hang forever,
|
|
2864
|
+
* silently.
|
|
2865
|
+
*
|
|
2866
|
+
* Loud, once, and never throwing: this runs from inside \`Context.destroy()\`,
|
|
2867
|
+
* where throwing would take the whole teardown down with it. Destroying
|
|
2868
|
+
* anyway is still the right trade — the queue is already unreachable by then.
|
|
2869
|
+
*/
|
|
2870
|
+
const __reportUnreachedSettlements = () => {
|
|
2871
|
+
if (__wasmEnvCleanupYielding || __wasmEnvSettlementLossReported) {
|
|
2872
|
+
return
|
|
2873
|
+
}
|
|
2874
|
+
const __pending = __napiInstance?.exports.napi_wasm_env_cleanup_pending
|
|
2875
|
+
if (typeof __pending !== 'function') {
|
|
2876
|
+
return
|
|
2877
|
+
}
|
|
2878
|
+
let __queued
|
|
2879
|
+
try {
|
|
2880
|
+
__queued = __pending()
|
|
2881
|
+
} catch {
|
|
2882
|
+
return
|
|
2883
|
+
}
|
|
2884
|
+
if (!__queued) {
|
|
2885
|
+
return
|
|
2886
|
+
}
|
|
2887
|
+
__wasmEnvSettlementLossReported = true
|
|
2888
|
+
try {
|
|
2889
|
+
const __consoleHost = globalThis.console
|
|
2890
|
+
if (__consoleHost && typeof __consoleHost.error === 'function') {
|
|
2891
|
+
__consoleHost.error(
|
|
2892
|
+
'napi-rs: the wasm environment is being destroyed with ' +
|
|
2893
|
+
__queued +
|
|
2894
|
+
' 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.',
|
|
2895
|
+
)
|
|
2896
|
+
}
|
|
2897
|
+
} catch {}
|
|
2898
|
+
}
|
|
2226
2899
|
const __prepareEnvCleanup = () => {
|
|
2227
|
-
if (__wasmEnvCleanupPrepared
|
|
2900
|
+
if (__wasmEnvCleanupPrepared) {
|
|
2901
|
+
return
|
|
2902
|
+
}
|
|
2903
|
+
// A handshake parked between its two halves is one this frame can close,
|
|
2904
|
+
// and must: every caller of this is about to destroy the context, and the
|
|
2905
|
+
// turns the poll is waiting for will not come. Closing it runs \`…_finish\`,
|
|
2906
|
+
// which is the call that joins, so this degrades to exactly the single
|
|
2907
|
+
// call below. Leaving it open destroys the context with the barrier still
|
|
2908
|
+
// raised and the runtime never joined.
|
|
2909
|
+
const __finishParked = __finishParkedEnvCleanup
|
|
2910
|
+
if (__finishParked !== undefined) {
|
|
2911
|
+
__finishParked()
|
|
2912
|
+
__reportUnreachedSettlements()
|
|
2913
|
+
return
|
|
2914
|
+
}
|
|
2915
|
+
if (__wasmEnvCleanupPreparing) {
|
|
2228
2916
|
return
|
|
2229
2917
|
}
|
|
2230
2918
|
const __prepareWasmEnvCleanup =
|
|
@@ -2240,9 +2928,86 @@ ${instanceHostState}\
|
|
|
2240
2928
|
__wasmEnvCleanupPreparing = false
|
|
2241
2929
|
}
|
|
2242
2930
|
__wasmEnvCleanupRan = true
|
|
2931
|
+
__reportUnreachedSettlements()
|
|
2243
2932
|
}
|
|
2244
2933
|
__wasmEnvCleanupPrepared = true
|
|
2245
2934
|
}
|
|
2935
|
+
/**
|
|
2936
|
+
* The barrier for the callers that can yield: \`__prepareEnvCleanup\` with real
|
|
2937
|
+
* event-loop turns in the middle.
|
|
2938
|
+
*
|
|
2939
|
+
* \`napi_prepare_wasm_env_cleanup\` waits — it returns only once the addon's
|
|
2940
|
+
* async runtime has quiesced, and the work it waits for can itself be waiting
|
|
2941
|
+
* for a JavaScript turn from this thread. The addon's two-phase form splits
|
|
2942
|
+
* that: \`…_begin\` stops the runtime without joining and reports whether
|
|
2943
|
+
* anything is still live, \`napi_wasm_runtime_work_pending\` answers that again
|
|
2944
|
+
* without blocking, and \`…_finish\` joins.
|
|
2945
|
+
*
|
|
2946
|
+
* The poll is unbounded — see \`__pollWasmRuntimeWork\` — but a caller that
|
|
2947
|
+
* cannot yield closes the handshake itself rather than waiting for it, so
|
|
2948
|
+
* \`…_finish\` still runs on every teardown path. Feature-detected like every
|
|
2949
|
+
* other export here, and returns nothing whenever the handshake finished
|
|
2950
|
+
* without yielding.
|
|
2951
|
+
*/
|
|
2952
|
+
const __prepareEnvCleanupWithTurns = () => {
|
|
2953
|
+
if (__wasmEnvCleanupPrepared || __wasmEnvCleanupPreparing) {
|
|
2954
|
+
return
|
|
2955
|
+
}
|
|
2956
|
+
const __exports = __napiInstance?.exports
|
|
2957
|
+
const __begin = __exports?.napi_prepare_wasm_env_cleanup_begin
|
|
2958
|
+
const __finish = __exports?.napi_prepare_wasm_env_cleanup_finish
|
|
2959
|
+
if (typeof __begin !== 'function' || typeof __finish !== 'function') {
|
|
2960
|
+
// No split to use. The settlement drain still follows this, so the queue
|
|
2961
|
+
// the single call leaves behind is expected rather than lost.
|
|
2962
|
+
__wasmEnvCleanupYielding = true
|
|
2963
|
+
try {
|
|
2964
|
+
__prepareEnvCleanup()
|
|
2965
|
+
} finally {
|
|
2966
|
+
__wasmEnvCleanupYielding = false
|
|
2967
|
+
}
|
|
2968
|
+
return
|
|
2969
|
+
}
|
|
2970
|
+
const __workPending = __exports?.napi_wasm_runtime_work_pending
|
|
2971
|
+
// The in-flight flag stays raised across the turns below, so a \`destroy()\`
|
|
2972
|
+
// from one of the JavaScript handlers they run is the same no-op it is
|
|
2973
|
+
// inside the single call: the barrier is up and the runtime is
|
|
2974
|
+
// mid-teardown, and destroying between the halves would strand exactly what
|
|
2975
|
+
// this delivers.
|
|
2976
|
+
__wasmEnvCleanupPreparing = true
|
|
2977
|
+
let __live
|
|
2978
|
+
try {
|
|
2979
|
+
__live = __begin()
|
|
2980
|
+
} catch (__error) {
|
|
2981
|
+
__wasmEnvCleanupPreparing = false
|
|
2982
|
+
throw __error
|
|
2983
|
+
}
|
|
2984
|
+
__wasmEnvCleanupRan = true
|
|
2985
|
+
const __finishEnvCleanup = () => {
|
|
2986
|
+
if (__wasmEnvCleanupPrepared) {
|
|
2987
|
+
// Already closed by a caller that could not yield. \`…_finish\` is
|
|
2988
|
+
// idempotent, but the flags it lowers are not.
|
|
2989
|
+
return
|
|
2990
|
+
}
|
|
2991
|
+
__finishParkedEnvCleanup = undefined
|
|
2992
|
+
try {
|
|
2993
|
+
__finish()
|
|
2994
|
+
} finally {
|
|
2995
|
+
__wasmEnvCleanupPreparing = false
|
|
2996
|
+
}
|
|
2997
|
+
__wasmEnvCleanupPrepared = true
|
|
2998
|
+
}
|
|
2999
|
+
if (!__live || typeof __workPending !== 'function') {
|
|
3000
|
+
__finishEnvCleanup()
|
|
3001
|
+
return
|
|
3002
|
+
}
|
|
3003
|
+
// Publish the closer before yielding: from here until \`__finishEnvCleanup\`
|
|
3004
|
+
// runs, a caller that cannot yield is entitled to end this handshake.
|
|
3005
|
+
__finishParkedEnvCleanup = __finishEnvCleanup
|
|
3006
|
+
return __pollWasmRuntimeWork(__workPending).then(
|
|
3007
|
+
__finishEnvCleanup,
|
|
3008
|
+
__finishEnvCleanup,
|
|
3009
|
+
)
|
|
3010
|
+
}
|
|
2246
3011
|
// The barrier + settlement drain, hoisted out of the context destroyer so the
|
|
2247
3012
|
// drain can yield without widening the destroyer's reentry window. Both
|
|
2248
3013
|
// yielding paths run it — dispose() and the initialization-failure rollback.
|
|
@@ -2255,6 +3020,20 @@ ${instanceHostState}\
|
|
|
2255
3020
|
// is enough — and dispose() stays retryable after it rejects, so marking the
|
|
2256
3021
|
// drain complete up front would make the retry skip it and destroy the context
|
|
2257
3022
|
// with the barrier's settlements still queued.
|
|
3023
|
+
const __drainAfterEnvCleanup = () => {
|
|
3024
|
+
if (!__wasmEnvCleanupRan) {
|
|
3025
|
+
return
|
|
3026
|
+
}
|
|
3027
|
+
const __drained = __drainWasmEnvCleanup(__napiInstance)
|
|
3028
|
+
if (!__drained || typeof __drained.then !== 'function') {
|
|
3029
|
+
__wasmEnvCleanupDrained = true
|
|
3030
|
+
return
|
|
3031
|
+
}
|
|
3032
|
+
return __drained.then((__value) => {
|
|
3033
|
+
__wasmEnvCleanupDrained = true
|
|
3034
|
+
return __value
|
|
3035
|
+
})
|
|
3036
|
+
}
|
|
2258
3037
|
const __prepareForDisposal = () => {
|
|
2259
3038
|
if (__wasmEnvCleanupDrained) {
|
|
2260
3039
|
return
|
|
@@ -2262,18 +3041,19 @@ ${instanceHostState}\
|
|
|
2262
3041
|
if (__wasmEnvCleanupDrainPromise) {
|
|
2263
3042
|
return __wasmEnvCleanupDrainPromise
|
|
2264
3043
|
}
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
const
|
|
2270
|
-
|
|
2271
|
-
|
|
3044
|
+
// The barrier itself can yield now, so the memo below has to cover it too:
|
|
3045
|
+
// a reentrant caller must join this handshake rather than start a second
|
|
3046
|
+
// one while the first is parked between the two halves.
|
|
3047
|
+
const __prepared = __prepareEnvCleanupWithTurns()
|
|
3048
|
+
const __settled =
|
|
3049
|
+
__prepared && typeof __prepared.then === 'function'
|
|
3050
|
+
? __prepared.then(__drainAfterEnvCleanup)
|
|
3051
|
+
: __drainAfterEnvCleanup()
|
|
3052
|
+
if (!__settled || typeof __settled.then !== 'function') {
|
|
2272
3053
|
return
|
|
2273
3054
|
}
|
|
2274
|
-
const __tracked =
|
|
3055
|
+
const __tracked = __settled.then(
|
|
2275
3056
|
(__value) => {
|
|
2276
|
-
__wasmEnvCleanupDrained = true
|
|
2277
3057
|
__wasmEnvCleanupDrainPromise = undefined
|
|
2278
3058
|
return __value
|
|
2279
3059
|
},
|
|
@@ -3183,7 +3963,10 @@ function __disposeWasiBindingAtExit() {
|
|
|
3183
3963
|
// settlements the way __startWasiDisposal does — the process is leaving and
|
|
3184
3964
|
// those promises have no observer left anyway. Run the synchronous teardown
|
|
3185
3965
|
// directly. Every step is idempotent, which also makes this the synchronous
|
|
3186
|
-
// finish for a disposal that is still waiting for its drain
|
|
3966
|
+
// finish for a disposal that is still waiting for its drain — and, through
|
|
3967
|
+
// __prepareWasmEnvCleanup, for one still parked between the two halves of
|
|
3968
|
+
// the environment cleanup barrier: there are no turns left to poll with, so
|
|
3969
|
+
// this closes that handshake with \`…_finish\`, which joins.
|
|
3187
3970
|
try {
|
|
3188
3971
|
__destroyEmnapiContext()
|
|
3189
3972
|
} catch {}
|