@oxc-minify/binding-wasm32-wasi 0.151.0 → 0.153.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -125,6 +125,20 @@ let __emnapiContextDestroyed = false
125
125
  let __emnapiContextDestroyPromise
126
126
  let __emnapiWasmEnvCleanupPrepared = false
127
127
  let __emnapiWasmEnvCleanupPreparing = false
128
+ // The closer for a barrier that is parked between `…_begin` and `…_finish`,
129
+ // set only while that window is open. `__emnapiWasmEnvCleanupPreparing` cannot
130
+ // tell those two apart on its own: it is raised both for a purely synchronous
131
+ // frame — which must not be re-entered, and which nothing outside it can
132
+ // finish — and across this window, which spans real event-loop turns, so a
133
+ // caller that cannot yield can land in the middle of one. That caller can close
134
+ // this window, because `…_finish` is idempotent and joins, which is exactly
135
+ // what the single call does. See `__prepareWasmEnvCleanup`.
136
+ let __finishParkedWasmEnvCleanup
137
+ // Raised while a caller that can still yield is driving the barrier, so the
138
+ // queue it leaves behind is expected rather than lost. See
139
+ // `__reportUnreachedWasmEnvSettlements`.
140
+ let __emnapiWasmEnvCleanupYielding = false
141
+ let __emnapiWasmEnvSettlementLossReported = false
128
142
  let __emnapiWasmEnvCleanupRan = false
129
143
  let __emnapiWasmEnvCleanupDrained = false
130
144
  let __emnapiWasmEnvCleanupDrainPromise
@@ -237,7 +251,24 @@ function __isPreparingWasmEnvCleanup() {
237
251
  }
238
252
 
239
253
  function __prepareWasmEnvCleanup() {
240
- if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
254
+ if (__emnapiWasmEnvCleanupPrepared) {
255
+ return
256
+ }
257
+ // A handshake parked between its two halves is one this frame can close, and
258
+ // must: every caller of this function is about to destroy the context, and
259
+ // the turns the poll is waiting for will not come — an 'exit' teardown is
260
+ // the last thing the process runs, and `Context.destroy()` takes the
261
+ // environment away. Closing it here runs `…_finish`, which is the call that joins, so
262
+ // this degrades to exactly the single call below. Leaving it open instead
263
+ // destroys the context with the barrier still raised, the runtime never
264
+ // joined and the workers never drained.
265
+ const finishParked = __finishParkedWasmEnvCleanup
266
+ if (finishParked !== undefined) {
267
+ finishParked()
268
+ __reportUnreachedWasmEnvSettlements()
269
+ return
270
+ }
271
+ if (__emnapiWasmEnvCleanupPreparing) {
241
272
  return
242
273
  }
243
274
  const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
@@ -252,10 +283,57 @@ function __prepareWasmEnvCleanup() {
252
283
  __emnapiWasmEnvCleanupPreparing = false
253
284
  }
254
285
  __emnapiWasmEnvCleanupRan = true
286
+ __reportUnreachedWasmEnvSettlements()
255
287
  }
256
288
  __emnapiWasmEnvCleanupPrepared = true
257
289
  }
258
290
 
291
+ /**
292
+ * Say so when the barrier leaves settlements queued and nothing is left that
293
+ * could deliver them.
294
+ *
295
+ * Only the disposal chain yields the event-loop turns @emnapi/core needs to
296
+ * dispatch its queue. Every other caller of the barrier destroys in the same
297
+ * turn — a raw `Context.destroy()`, the 'exit' teardown — and
298
+ * `Context.destroy()` runs the threadsafe function's cleanup hook, which drains
299
+ * that queue with a null env and discards it. The promises those settlements
300
+ * were for then hang forever, silently.
301
+ *
302
+ * Loud, once, and never throwing: this runs from inside `Context.destroy()`,
303
+ * emnapi's own beforeExit destroy included, where throwing would take the whole
304
+ * teardown down with it. Destroying anyway is still the right trade — the queue
305
+ * is already unreachable by then.
306
+ */
307
+ function __reportUnreachedWasmEnvSettlements() {
308
+ if (__emnapiWasmEnvCleanupYielding || __emnapiWasmEnvSettlementLossReported) {
309
+ return
310
+ }
311
+ const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending
312
+ if (typeof pending !== 'function') {
313
+ return
314
+ }
315
+ let queued
316
+ try {
317
+ queued = pending()
318
+ } catch {
319
+ return
320
+ }
321
+ if (!queued) {
322
+ return
323
+ }
324
+ __emnapiWasmEnvSettlementLossReported = true
325
+ try {
326
+ const consoleHost = globalThis.console
327
+ if (consoleHost && typeof consoleHost.error === 'function') {
328
+ consoleHost.error(
329
+ "napi-rs: the wasm environment is being destroyed with " +
330
+ queued +
331
+ " 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.",
332
+ )
333
+ }
334
+ } catch {}
335
+ }
336
+
259
337
  // Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch
260
338
  // on, so the drain turns below interleave with that dispatch instead of racing
261
339
  // ahead of it on a faster queue.
@@ -305,6 +383,291 @@ function __scheduleTimer(callback, delay) {
305
383
  }
306
384
  }
307
385
 
386
+ // A real, referenced timer rather than a zero-delay macrotask, for the same
387
+ // reason the async-work drain uses one: this polls the addon instead of
388
+ // interleaving with the @emnapi/core dispatch, so a zero-delay turn would spin
389
+ // the loop instead of yielding it.
390
+ const __WASM_RUNTIME_WORK_POLL_INTERVAL_MS = 1
391
+ // Arrivals it takes before the poll paces on the host's timers alone. One
392
+ // proves nothing: a timer armed before the host's timers stopped still fires.
393
+ const __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS = 2
394
+ // How long a parked turn's own timer must already have been due before a
395
+ // backup that runs calls it dropped. Slack, not a deadline: a timer is due
396
+ // against the event loop's clock, which is read once per iteration, while
397
+ // these are `Date.now()` readings taken part-way through one, so the two
398
+ // drift apart by however long the loop has been inside the current iteration.
399
+ const __WASM_RUNTIME_WORK_POLL_STALL_MS = 50
400
+ // How long a backup itself waits. What is left of it after the slack and one
401
+ // interval — 149 ms — has to cover the *two* poll turns that can separate a
402
+ // parked turn from the last backup armed while the host's timers still
403
+ // worked, so the ceiling on a single turn is half of it. See the invariant on
404
+ // `__armWasmRuntimePollStallBackup`.
405
+ const __WASM_RUNTIME_WORK_POLL_BACKUP_MS = 200
406
+
407
+ /**
408
+ * Pacing state for one runtime-work poll.
409
+ *
410
+ * Per poll, never per module: whether the host's timers arrive is not a
411
+ * property of the module. A host can lose its timers between two disposals,
412
+ * and in the deferred shape every instance shares this module — one healthy
413
+ * instance must not disarm the fallback for the next one.
414
+ */
415
+ function __createWasmRuntimePollPace() {
416
+ return {
417
+ // Timers armed by *this* poll that have actually arrived.
418
+ arrivals: 0,
419
+ // The turn waiting on a timer alone *right now* — undefined whenever no
420
+ // turn is parked — and when that turn's own timer came due.
421
+ settleTurn: undefined,
422
+ turnTimerDueAt: 0,
423
+ }
424
+ }
425
+
426
+ /**
427
+ * The backup that ends a turn whose timer is never going to arrive.
428
+ *
429
+ * Once the poll paces on the timer alone it has nothing left to fall back on
430
+ * if the host's timers stop mid-poll: the turn that armed the dead timer is
431
+ * the turn that parks, and a parked poll schedules nothing that could notice.
432
+ * So every turn arms one of these before it yields, and each one compares due
433
+ * times instead of measuring how long the parked turn has been waiting.
434
+ *
435
+ * Invariant: a parked turn is ended by the newest backup that was armed while
436
+ * the host's timers still worked, and a backup ends a turn only when that
437
+ * turn's own timer was already due a whole window before the backup itself.
438
+ * Neither half turns on how far apart the arms happen to fall — what bounds
439
+ * the rescue is how far back that newest live backup is:
440
+ *
441
+ * - *Ends it.* Hosts run timers in due order, so a backup that runs while a
442
+ * turn due a whole window earlier is still parked proves that turn's timer
443
+ * was dropped rather than merely late. That same comparison is what leaves a
444
+ * healthy host alone: there the turn's timer has already run and cleared
445
+ * `settleTurn` before any backup due after it can look.
446
+ * - *Two turns back, not one.* A turn that ended does not prove its own timer
447
+ * arrived: until `…_TRUSTED_ARRIVALS` is reached every turn arms both
448
+ * primitives and the macrotask wins, so such a turn can end with its own
449
+ * timer — and the backup armed one line before it — already dead. The
450
+ * arrival that then flips the poll onto the timer alone can itself be a
451
+ * timer armed before the host's timers died. So the turn that parks can sit
452
+ * two turns past the last live arm, and the newest live backup is due
453
+ * `…_BACKUP_MS` less *two* turn lengths after that turn's own timer.
454
+ * Arming on every turn is what holds it to two, rather than however far back
455
+ * a throttle last let one through.
456
+ * - *Ceiling.* Coverage therefore holds while two consecutive poll turns fit
457
+ * inside `…_BACKUP_MS` less the slack and one interval: 149 ms, so 74 ms
458
+ * per turn (measured: a 74 ms turn is still rescued, a 75 ms one parks).
459
+ * Past that the turn stays parked and the disposal promise never settles.
460
+ * The bound is deliberate: reaching it takes a host that drops timers
461
+ * mid-poll *and* keeps every poll turn busy for more than 74 ms, and neither
462
+ * Node nor WebContainer — the hosts that run the threaded artifact — does
463
+ * the second.
464
+ *
465
+ * The poll then goes back to arming both primitives until two fresh arrivals
466
+ * prove the timers again. A host that stops running the timers it has
467
+ * *already* accepted leaves nothing to fire, and the disposal promise stays
468
+ * pending rather than wedging the thread — the same outcome as a blocking
469
+ * closure that never returns. Unreferenced wherever the host allows it: the
470
+ * poll's own turn timers are what keep the loop alive, never these.
471
+ */
472
+ function __armWasmRuntimePollStallBackup(pace) {
473
+ const setTimer = globalThis.setTimeout
474
+ if (typeof setTimer !== 'function') {
475
+ // Nothing to back up: `__scheduleTimer` is on the macrotask channel
476
+ // already, and that one cannot park.
477
+ return
478
+ }
479
+ // Read before arming, so this never claims to be due earlier than the timer
480
+ // actually is: a backup ends a turn only when it is provably due after it.
481
+ const dueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_BACKUP_MS
482
+ let handle
483
+ try {
484
+ handle = setTimer(() => {
485
+ const settleTurn = pace.settleTurn
486
+ if (
487
+ !settleTurn ||
488
+ pace.turnTimerDueAt > dueAt - __WASM_RUNTIME_WORK_POLL_STALL_MS
489
+ ) {
490
+ // No turn is parked, or the parked one's timer came due too close to
491
+ // this backup to call it dropped — it may still arrive, and the turn
492
+ // that armed it armed a backup due a whole window after *that*.
493
+ return
494
+ }
495
+ pace.arrivals = 0
496
+ pace.settleTurn = undefined
497
+ settleTurn()
498
+ }, __WASM_RUNTIME_WORK_POLL_BACKUP_MS)
499
+ } catch {
500
+ return
501
+ }
502
+ if (handle && typeof handle.unref === 'function') {
503
+ try {
504
+ handle.unref()
505
+ } catch {}
506
+ }
507
+ }
508
+
509
+ /**
510
+ * One turn of the runtime-work poll.
511
+ *
512
+ * `__scheduleTimer` falls back to the macrotask scheduler when `setTimeout` is
513
+ * missing or throws, but not when it is present, returns a handle and never
514
+ * fires — fake timers in a test suite that disposes from an `afterEach`, or a
515
+ * host whose timers belong to an IO context that is already gone. That host
516
+ * would park this poll forever, and the poll is unbounded, so nothing would
517
+ * ever call `…_finish`.
518
+ *
519
+ * Arm both primitives until timers armed by this poll have arrived twice, and
520
+ * let whichever lands first end the turn; the loser resolves nothing. A host
521
+ * with working timers therefore pays the double arming for the first turn or
522
+ * two — the macrotask wins the race, but the timers behind it still arrive and
523
+ * are counted — and paces on the timer alone from then on, instead of spinning
524
+ * the loop on a zero-delay queue. A host whose timers never arrive keeps both,
525
+ * and the macrotask is what keeps the poll moving. A host whose timers stop
526
+ * after proving themselves is caught by `__armWasmRuntimePollStallBackup`,
527
+ * which ends the parked turn and puts this poll back on both.
528
+ */
529
+ function __yieldWasmRuntimePollTurn(pace) {
530
+ // Armed before the turn yields, and by every turn: what rescues a parked
531
+ // turn has to have been armed while the host's timers still worked, and the
532
+ // turn that parks is the one whose own timer is already dead.
533
+ __armWasmRuntimePollStallBackup(pace)
534
+ return new Promise((resolve) => {
535
+ let settled = false
536
+ const settle = () => {
537
+ if (settled) {
538
+ return
539
+ }
540
+ settled = true
541
+ if (pace.settleTurn === settle) {
542
+ // Nothing is parked any more: a backup running later must not read a
543
+ // due time this turn has already answered.
544
+ pace.settleTurn = undefined
545
+ }
546
+ resolve()
547
+ }
548
+ __scheduleTimer(() => {
549
+ pace.arrivals++
550
+ settle()
551
+ }, __WASM_RUNTIME_WORK_POLL_INTERVAL_MS)
552
+ // Read next to the arming it describes; see
553
+ // `__armWasmRuntimePollStallBackup` for what the two due times mean.
554
+ const turnTimerDueAt = Date.now() + __WASM_RUNTIME_WORK_POLL_INTERVAL_MS
555
+ if (pace.arrivals < __WASM_RUNTIME_WORK_POLL_TRUSTED_ARRIVALS) {
556
+ __scheduleMacrotask(settle)
557
+ return
558
+ }
559
+ // Paced by the timer alone from here; the backup is what ends this turn if
560
+ // the timer never arrives.
561
+ pace.settleTurn = settle
562
+ pace.turnTimerDueAt = turnTimerDueAt
563
+ })
564
+ }
565
+
566
+ /**
567
+ * The barrier for callers that can yield: `__prepareWasmEnvCleanup` with real
568
+ * event-loop turns in the middle.
569
+ *
570
+ * `napi_prepare_wasm_env_cleanup` waits — it returns only once the addon's
571
+ * async runtime has quiesced, and on `wasm32-wasip1-threads` the thread it
572
+ * waits on is this one, the only thread that can give a running blocking
573
+ * closure the JavaScript turn *it* is waiting for. A single call there can wait
574
+ * for work that can never finish. The addon's two-phase form splits that:
575
+ * `…_begin` stops the runtime without joining and reports whether anything is
576
+ * still live, `napi_wasm_runtime_work_pending` answers that question again
577
+ * without blocking, and `…_finish` joins. The turns yielded in between are the
578
+ * entire point.
579
+ *
580
+ * The poll has no deadline, for the same reason the async-work drain below has
581
+ * none: giving up means calling `…_finish`, which joins on this thread, and the
582
+ * work it would join is the work that is waiting for a turn from this thread —
583
+ * so a bound does not end the wait, it only moves it somewhere the JavaScript
584
+ * thread can no longer be reached. A blocking closure that never returns keeps
585
+ * the disposal promise pending instead, exactly as a task whose `execute` never
586
+ * returns already keeps an *undisposed* process alive. The host contract is in
587
+ * `crates/async-runtime/README.md`: a blocking closure must never wait on a
588
+ * JavaScript turn. The process-exit path still blocks in `…_finish`, because it
589
+ * has no turns left to give (see `__prepareWasmEnvCleanup`).
590
+ *
591
+ * Feature-detected like every other export in this teardown, so an addon built
592
+ * against a napi crate that predates the split keeps the single blocking call.
593
+ * Returns nothing whenever the handshake finished without yielding, which keeps
594
+ * an idle disposal synchronous.
595
+ */
596
+ function __prepareWasmEnvCleanupWithTurns() {
597
+ if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
598
+ return
599
+ }
600
+ const exports = __napiInstance?.exports
601
+ const begin = exports?.napi_prepare_wasm_env_cleanup_begin
602
+ const finish = exports?.napi_prepare_wasm_env_cleanup_finish
603
+ if (typeof begin !== 'function' || typeof finish !== 'function') {
604
+ // No split to use. The settlement drain still follows this, so the queue
605
+ // the single call leaves behind is expected rather than lost.
606
+ __emnapiWasmEnvCleanupYielding = true
607
+ try {
608
+ __prepareWasmEnvCleanup()
609
+ } finally {
610
+ __emnapiWasmEnvCleanupYielding = false
611
+ }
612
+ return
613
+ }
614
+ const workPending = exports?.napi_wasm_runtime_work_pending
615
+ // The in-flight flag stays raised across the turns below, so a `destroy()`
616
+ // from one of the JavaScript handlers they run is the same no-op it is inside
617
+ // the single call: the barrier is up and the runtime is mid-teardown, and
618
+ // destroying between the halves would strand exactly what this delivers.
619
+ __emnapiWasmEnvCleanupPreparing = true
620
+ let live
621
+ try {
622
+ live = begin()
623
+ } catch (error) {
624
+ __emnapiWasmEnvCleanupPreparing = false
625
+ throw error
626
+ }
627
+ __emnapiWasmEnvCleanupRan = true
628
+ const finishCleanup = () => {
629
+ if (__emnapiWasmEnvCleanupPrepared) {
630
+ // Already closed by a caller that could not yield — the 'exit' teardown
631
+ // reached `__prepareWasmEnvCleanup` while this poll was parked. `…_finish`
632
+ // is idempotent, but the flags it lowers are not: running it again here
633
+ // would clear a `preparing` some later barrier had raised.
634
+ return
635
+ }
636
+ __finishParkedWasmEnvCleanup = undefined
637
+ try {
638
+ finish()
639
+ } finally {
640
+ __emnapiWasmEnvCleanupPreparing = false
641
+ }
642
+ __emnapiWasmEnvCleanupPrepared = true
643
+ }
644
+ if (!live || typeof workPending !== 'function') {
645
+ finishCleanup()
646
+ return
647
+ }
648
+ // Publish the closer before yielding: from here until `finishCleanup` runs,
649
+ // a caller that cannot yield is entitled to end this handshake itself.
650
+ __finishParkedWasmEnvCleanup = finishCleanup
651
+ return (async () => {
652
+ // Unbounded, exactly like the async-work drain below. The wait ends when
653
+ // the addon reports its runtime work finished; the turns spent here are
654
+ // what let that happen at all.
655
+ const pace = __createWasmRuntimePollPace()
656
+ for (;;) {
657
+ await __yieldWasmRuntimePollTurn(pace)
658
+ try {
659
+ if (!workPending()) {
660
+ return
661
+ }
662
+ } catch {
663
+ // A trap is the only way this fails, and a trapped instance has no
664
+ // reachable work left. Stop polling and finish.
665
+ return
666
+ }
667
+ }
668
+ })().then(finishCleanup, finishCleanup)
669
+ }
670
+
308
671
  // Turns to wait for while the addon still reports queued settlements. Reaching
309
672
  // zero is the only success. A counter still nonzero at this bound rejects the
310
673
  // disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
@@ -441,6 +804,19 @@ function __destroyEmnapiContext() {
441
804
  }
442
805
 
443
806
  __prepareWasmEnvCleanup()
807
+ if (__isPreparingWasmEnvCleanup()) {
808
+ // Reached from inside the synchronous barrier — a promise hook one of the
809
+ // settlements above ran, which is the reentrancy the destroy wrapper
810
+ // exists for. `Context.destroy()` below would hit that wrapper's in-flight
811
+ // no-op and answer `undefined`, and recording that as a completed destroy
812
+ // is what makes the frame that *did* start the barrier skip the real one
813
+ // afterwards, leaving the context retained with its cleanup hooks unrun.
814
+ // Refuse instead: nothing is flagged, and that frame destroys for real the
815
+ // moment it returns. The deferred loader carries the same backstop. A
816
+ // parked handshake cannot get here — `__prepareWasmEnvCleanup` closes one
817
+ // rather than skipping it.
818
+ return
819
+ }
444
820
  const result = __emnapiContext.destroy()
445
821
  if (!__isThenable(result)) {
446
822
  __emnapiContextDestroyed = true
@@ -714,11 +1090,7 @@ function __continueWasiDisposal() {
714
1090
  return __finishWasiDisposal()
715
1091
  }
716
1092
 
717
- function __cleanUpWasmEnvForWasiDisposal() {
718
- // Run the pre-teardown barrier, then let the settlements it queued actually
719
- // reach JavaScript, and only then destroy the environment. Doing these two
720
- // back to back is what strands them.
721
- __prepareWasmEnvCleanup()
1093
+ function __drainWasmEnvForWasiDisposal() {
722
1094
  const drainResult = __drainWasmEnvCleanup()
723
1095
  if (__isThenable(drainResult)) {
724
1096
  return Promise.resolve(drainResult).then(__continueWasiDisposal)
@@ -726,6 +1098,18 @@ function __cleanUpWasmEnvForWasiDisposal() {
726
1098
  return __continueWasiDisposal()
727
1099
  }
728
1100
 
1101
+ function __cleanUpWasmEnvForWasiDisposal() {
1102
+ // Run the pre-teardown barrier — yielding the turns its two-phase form asks
1103
+ // for, when the addon has one — then let the settlements it queued actually
1104
+ // reach JavaScript, and only then destroy the environment. Doing any two of
1105
+ // these back to back is what strands them.
1106
+ const prepareResult = __prepareWasmEnvCleanupWithTurns()
1107
+ if (__isThenable(prepareResult)) {
1108
+ return Promise.resolve(prepareResult).then(__drainWasmEnvForWasiDisposal)
1109
+ }
1110
+ return __drainWasmEnvForWasiDisposal()
1111
+ }
1112
+
729
1113
  function __startWasiDisposal() {
730
1114
  // Outstanding `napi_async_work` goes first, while the environment is still
731
1115
  // completely live: the completion callbacks run addon code, and everything
@@ -886,14 +1270,36 @@ function __rollbackWasiInitialization() {
886
1270
  // be reached without the async-work drain below running first.
887
1271
  function __rollbackWasmEnvForWasiInitialization() {
888
1272
  const cleanupErrors = []
1273
+ let prepareResult
1274
+ try {
1275
+ prepareResult = __prepareWasmEnvCleanupWithTurns()
1276
+ } catch (cleanupError) {
1277
+ cleanupErrors.push(cleanupError)
1278
+ return __retainFailedWasiRollback(cleanupErrors)
1279
+ }
1280
+ if (__isThenable(prepareResult)) {
1281
+ return Promise.resolve(prepareResult).then(
1282
+ () => __drainWasmEnvForWasiRollback(cleanupErrors),
1283
+ (cleanupError) => {
1284
+ cleanupErrors.push(cleanupError)
1285
+ return __retainFailedWasiRollback(cleanupErrors)
1286
+ },
1287
+ )
1288
+ }
1289
+ return __drainWasmEnvForWasiRollback(cleanupErrors)
1290
+ }
1291
+
1292
+ // The settlement drain of the rollback above, reached either straight away or
1293
+ // after the barrier's two-phase form has yielded its turns. A barrier that
1294
+ // did not finish never gets here: it retains instead, exactly as a drain that
1295
+ // did not finish does.
1296
+ function __drainWasmEnvForWasiRollback(cleanupErrors) {
889
1297
  let drainResult
890
- let settlementsUnreached = false
891
1298
  try {
892
- __prepareWasmEnvCleanup()
893
1299
  drainResult = __drainWasmEnvCleanup()
894
1300
  } catch (cleanupError) {
895
1301
  cleanupErrors.push(cleanupError)
896
- settlementsUnreached = true
1302
+ return __retainFailedWasiRollback(cleanupErrors)
897
1303
  }
898
1304
  if (__isThenable(drainResult)) {
899
1305
  return Promise.resolve(drainResult).then(
@@ -904,9 +1310,6 @@ function __rollbackWasiInitialization() {
904
1310
  },
905
1311
  )
906
1312
  }
907
- if (settlementsUnreached) {
908
- return __retainFailedWasiRollback(cleanupErrors)
909
- }
910
1313
  return __destroyContextForWasiRollback(cleanupErrors)
911
1314
  }
912
1315
 
@@ -938,6 +1341,36 @@ function __rollbackWasiInitialization() {
938
1341
  return __rollbackWasmEnvForWasiInitialization()
939
1342
  }
940
1343
 
1344
+ // A view of the addon's crash flag, one word of the shared wasm memory, for the
1345
+ // pool workers: they raise it when their wasm thread dies, and the shutdown
1346
+ // waits inside the cleanup calls on this thread then trap instead of waiting on
1347
+ // the dead thread for good. Undefined for an addon built with an older napi.
1348
+ let __wasiAddonCrashFlag
1349
+
1350
+ function __captureWasiAddonCrashFlag(instance) {
1351
+ try {
1352
+ const getAddress = instance.exports.napi_wasm_thread_crash_flag_address
1353
+ if (typeof getAddress !== 'function') {
1354
+ return
1355
+ }
1356
+ const address = getAddress() >>> 0
1357
+ const buffer = __sharedMemory.buffer
1358
+ if (address === 0 || address % 4 !== 0 || address + 4 > buffer.byteLength) {
1359
+ return
1360
+ }
1361
+ __wasiAddonCrashFlag = new Int32Array(buffer, address, 1)
1362
+ } catch {}
1363
+ }
1364
+
1365
+ function __shareWasiAddonCrashFlag(worker) {
1366
+ if (__wasiAddonCrashFlag === undefined) {
1367
+ return
1368
+ }
1369
+ try {
1370
+ worker.postMessage({ __napiRsAddonCrashFlag: __wasiAddonCrashFlag })
1371
+ } catch {}
1372
+ }
1373
+
941
1374
  let __wasiModule
942
1375
  let __napiModule
943
1376
 
@@ -968,6 +1401,7 @@ try {
968
1401
  type: 'module',
969
1402
  })
970
1403
  __wasiWorkers.add(worker)
1404
+ __shareWasiAddonCrashFlag(worker)
971
1405
 
972
1406
 
973
1407
  return worker
@@ -983,6 +1417,10 @@ try {
983
1417
  },
984
1418
  beforeInit({ instance }) {
985
1419
  __napiInstance = instance
1420
+ __captureWasiAddonCrashFlag(instance)
1421
+ for (const worker of __wasiWorkers) {
1422
+ __shareWasiAddonCrashFlag(worker)
1423
+ }
986
1424
  for (const name of Object.keys(instance.exports)) {
987
1425
  if (name.startsWith('__napi_register__')) {
988
1426
  instance.exports[name]()