@napi-rs/cli 3.8.3 → 3.8.4
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/README.md +7 -4
- package/dist/cli.js +501 -15
- package/dist/index.cjs +501 -15
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +501 -15
- package/package.json +1 -1
- package/src/api/__tests__/__snapshots__/templates.spec.ts.md +768 -9
- package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
- package/src/api/__tests__/templates.spec.ts +85 -1
- package/src/api/templates/load-wasi-template.ts +499 -13
|
@@ -56,9 +56,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
56
56
|
let __emnapiContextDestroyed = false␊
|
|
57
57
|
let __emnapiContextDestroyPromise␊
|
|
58
58
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
59
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
60
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
61
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
59
62
|
let __wasiDisposed = false␊
|
|
60
63
|
let __wasiDisposePromise␊
|
|
61
64
|
let __completeWasiDisposal = function() {}␊
|
|
65
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
66
|
+
// that stopped short of destroying the context. See␊
|
|
67
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
68
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
62
69
|
␊
|
|
63
70
|
function __isThenable(value) {␊
|
|
64
71
|
return (␊
|
|
@@ -129,10 +136,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
129
136
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
130
137
|
if (typeof prepare === 'function') {␊
|
|
131
138
|
prepare()␊
|
|
139
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
132
140
|
}␊
|
|
133
141
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
134
142
|
}␊
|
|
135
143
|
␊
|
|
144
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
145
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
146
|
+
// ahead of it on a faster queue.␊
|
|
147
|
+
const __scheduleMacrotask = (function () {␊
|
|
148
|
+
if (typeof setImmediate === 'function') {␊
|
|
149
|
+
return function (callback) {␊
|
|
150
|
+
setImmediate(callback)␊
|
|
151
|
+
}␊
|
|
152
|
+
}␊
|
|
153
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
154
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
155
|
+
return function (callback) {␊
|
|
156
|
+
const channel = new __MessageChannel()␊
|
|
157
|
+
channel.port1.onmessage = function () {␊
|
|
158
|
+
channel.port1.onmessage = null␊
|
|
159
|
+
try {␊
|
|
160
|
+
channel.port1.close()␊
|
|
161
|
+
} catch {}␊
|
|
162
|
+
try {␊
|
|
163
|
+
channel.port2.close()␊
|
|
164
|
+
} catch {}␊
|
|
165
|
+
callback()␊
|
|
166
|
+
}␊
|
|
167
|
+
channel.port2.postMessage(null)␊
|
|
168
|
+
}␊
|
|
169
|
+
}␊
|
|
170
|
+
return function (callback) {␊
|
|
171
|
+
setTimeout(callback, 0)␊
|
|
172
|
+
}␊
|
|
173
|
+
})()␊
|
|
174
|
+
␊
|
|
175
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
176
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
177
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
178
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
179
|
+
// bounded either way.␊
|
|
180
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
181
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
182
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
183
|
+
// call made on this thread (two), plus a margin.␊
|
|
184
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
185
|
+
␊
|
|
186
|
+
/**␊
|
|
187
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
188
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
189
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
190
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
191
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
192
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
193
|
+
*␊
|
|
194
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
195
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
196
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
197
|
+
* macrotask run.␊
|
|
198
|
+
*␊
|
|
199
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
200
|
+
* synchronous in the common case.␊
|
|
201
|
+
*␊
|
|
202
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
203
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
204
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
205
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
206
|
+
* the context with the barrier's settlements still queued.␊
|
|
207
|
+
*␊
|
|
208
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
209
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
210
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
211
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
212
|
+
* flag unset and disposal retryable.␊
|
|
213
|
+
*/␊
|
|
214
|
+
function __drainWasmEnvCleanup() {␊
|
|
215
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
216
|
+
return␊
|
|
217
|
+
}␊
|
|
218
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
219
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
220
|
+
}␊
|
|
221
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
222
|
+
const observable = typeof pending === 'function'␊
|
|
223
|
+
if (observable) {␊
|
|
224
|
+
let queued␊
|
|
225
|
+
try {␊
|
|
226
|
+
queued = pending()␊
|
|
227
|
+
} catch {␊
|
|
228
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
229
|
+
return␊
|
|
230
|
+
}␊
|
|
231
|
+
if (!queued) {␊
|
|
232
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
233
|
+
return␊
|
|
234
|
+
}␊
|
|
235
|
+
}␊
|
|
236
|
+
const limit = observable␊
|
|
237
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
238
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
239
|
+
const drainPromise = (async () => {␊
|
|
240
|
+
let queued = 0␊
|
|
241
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
242
|
+
await new Promise((resolve) => {␊
|
|
243
|
+
__scheduleMacrotask(resolve)␊
|
|
244
|
+
})␊
|
|
245
|
+
if (!observable) {␊
|
|
246
|
+
continue␊
|
|
247
|
+
}␊
|
|
248
|
+
try {␊
|
|
249
|
+
queued = pending()␊
|
|
250
|
+
} catch {␊
|
|
251
|
+
return␊
|
|
252
|
+
}␊
|
|
253
|
+
if (!queued) {␊
|
|
254
|
+
return␊
|
|
255
|
+
}␊
|
|
256
|
+
}␊
|
|
257
|
+
if (!observable) {␊
|
|
258
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
259
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
260
|
+
// finishing the drain.␊
|
|
261
|
+
return␊
|
|
262
|
+
}␊
|
|
263
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
264
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
265
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
266
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
267
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
268
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
269
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
270
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
271
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
272
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
273
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
274
|
+
const drainError = new Error(␊
|
|
275
|
+
'the wasm environment still reports ' +␊
|
|
276
|
+
queued +␊
|
|
277
|
+
' queued settlement(s) after ' +␊
|
|
278
|
+
limit +␊
|
|
279
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
280
|
+
)␊
|
|
281
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
282
|
+
throw drainError␊
|
|
283
|
+
})().then(␊
|
|
284
|
+
(value) => {␊
|
|
285
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
286
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
287
|
+
// queued rejects above and must stay repeatable.␊
|
|
288
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
289
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
290
|
+
return value␊
|
|
291
|
+
},␊
|
|
292
|
+
(error) => {␊
|
|
293
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
294
|
+
throw error␊
|
|
295
|
+
},␊
|
|
296
|
+
)␊
|
|
297
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
298
|
+
return drainPromise␊
|
|
299
|
+
}␊
|
|
300
|
+
␊
|
|
136
301
|
function __destroyEmnapiContext() {␊
|
|
137
302
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
138
303
|
__emnapiContextDestroyed = true␊
|
|
@@ -210,7 +375,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
210
375
|
return __completeWasiDisposal()␊
|
|
211
376
|
}␊
|
|
212
377
|
␊
|
|
213
|
-
function
|
|
378
|
+
function __continueWasiDisposal() {␊
|
|
214
379
|
const destroyResult = __destroyEmnapiContext()␊
|
|
215
380
|
if (__isThenable(destroyResult)) {␊
|
|
216
381
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -218,6 +383,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
218
383
|
return __finishWasiDisposal()␊
|
|
219
384
|
}␊
|
|
220
385
|
␊
|
|
386
|
+
function __startWasiDisposal() {␊
|
|
387
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
388
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
389
|
+
// back to back is what strands them.␊
|
|
390
|
+
__prepareWasmEnvCleanup()␊
|
|
391
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
392
|
+
if (__isThenable(drainResult)) {␊
|
|
393
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
394
|
+
}␊
|
|
395
|
+
return __continueWasiDisposal()␊
|
|
396
|
+
}␊
|
|
397
|
+
␊
|
|
221
398
|
/**␊
|
|
222
399
|
* Disposes this generated WASI binding.␊
|
|
223
400
|
*␊
|
|
@@ -289,8 +466,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
289
466
|
return cleanupErrors␊
|
|
290
467
|
}␊
|
|
291
468
|
␊
|
|
292
|
-
function
|
|
293
|
-
const cleanupErrors = []␊
|
|
469
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
294
470
|
let destroyResult␊
|
|
295
471
|
try {␊
|
|
296
472
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -308,6 +484,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
308
484
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
309
485
|
}␊
|
|
310
486
|
␊
|
|
487
|
+
/**␊
|
|
488
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
489
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
490
|
+
*/␊
|
|
491
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
492
|
+
try {␊
|
|
493
|
+
__retainWasiRollbackForRetry()␊
|
|
494
|
+
} catch (cleanupError) {␊
|
|
495
|
+
cleanupErrors.push(cleanupError)␊
|
|
496
|
+
}␊
|
|
497
|
+
return cleanupErrors␊
|
|
498
|
+
}␊
|
|
499
|
+
␊
|
|
500
|
+
/**␊
|
|
501
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
502
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
503
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
504
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
505
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
506
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
507
|
+
*␊
|
|
508
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
509
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
510
|
+
*␊
|
|
511
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
512
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
513
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
514
|
+
* two trades, and not because of what it saves:␊
|
|
515
|
+
*␊
|
|
516
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
517
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
518
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
519
|
+
* forever with nothing left that could ever settle it.␊
|
|
520
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
521
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
522
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
523
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
524
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
525
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
526
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
527
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
528
|
+
* what makes that retained record useless.␊
|
|
529
|
+
*␊
|
|
530
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
531
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
532
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
533
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
534
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
535
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
536
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
537
|
+
*/␊
|
|
538
|
+
function __rollbackWasiInitialization() {␊
|
|
539
|
+
const cleanupErrors = []␊
|
|
540
|
+
let drainResult␊
|
|
541
|
+
let settlementsUnreached = false␊
|
|
542
|
+
try {␊
|
|
543
|
+
__prepareWasmEnvCleanup()␊
|
|
544
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
545
|
+
} catch (cleanupError) {␊
|
|
546
|
+
cleanupErrors.push(cleanupError)␊
|
|
547
|
+
settlementsUnreached = true␊
|
|
548
|
+
}␊
|
|
549
|
+
if (__isThenable(drainResult)) {␊
|
|
550
|
+
return Promise.resolve(drainResult).then(␊
|
|
551
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
552
|
+
(cleanupError) => {␊
|
|
553
|
+
cleanupErrors.push(cleanupError)␊
|
|
554
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
555
|
+
},␊
|
|
556
|
+
)␊
|
|
557
|
+
}␊
|
|
558
|
+
if (settlementsUnreached) {␊
|
|
559
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
560
|
+
}␊
|
|
561
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
562
|
+
}␊
|
|
563
|
+
␊
|
|
311
564
|
let __wasiModule␊
|
|
312
565
|
let __napiModule␊
|
|
313
566
|
␊
|
|
@@ -411,9 +664,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
411
664
|
let __emnapiContextDestroyed = false␊
|
|
412
665
|
let __emnapiContextDestroyPromise␊
|
|
413
666
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
667
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
668
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
669
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
414
670
|
let __wasiDisposed = false␊
|
|
415
671
|
let __wasiDisposePromise␊
|
|
416
672
|
let __completeWasiDisposal = function() {}␊
|
|
673
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
674
|
+
// that stopped short of destroying the context. See␊
|
|
675
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
676
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
417
677
|
␊
|
|
418
678
|
function __isThenable(value) {␊
|
|
419
679
|
return (␊
|
|
@@ -484,10 +744,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
484
744
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
485
745
|
if (typeof prepare === 'function') {␊
|
|
486
746
|
prepare()␊
|
|
747
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
487
748
|
}␊
|
|
488
749
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
489
750
|
}␊
|
|
490
751
|
␊
|
|
752
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
753
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
754
|
+
// ahead of it on a faster queue.␊
|
|
755
|
+
const __scheduleMacrotask = (function () {␊
|
|
756
|
+
if (typeof setImmediate === 'function') {␊
|
|
757
|
+
return function (callback) {␊
|
|
758
|
+
setImmediate(callback)␊
|
|
759
|
+
}␊
|
|
760
|
+
}␊
|
|
761
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
762
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
763
|
+
return function (callback) {␊
|
|
764
|
+
const channel = new __MessageChannel()␊
|
|
765
|
+
channel.port1.onmessage = function () {␊
|
|
766
|
+
channel.port1.onmessage = null␊
|
|
767
|
+
try {␊
|
|
768
|
+
channel.port1.close()␊
|
|
769
|
+
} catch {}␊
|
|
770
|
+
try {␊
|
|
771
|
+
channel.port2.close()␊
|
|
772
|
+
} catch {}␊
|
|
773
|
+
callback()␊
|
|
774
|
+
}␊
|
|
775
|
+
channel.port2.postMessage(null)␊
|
|
776
|
+
}␊
|
|
777
|
+
}␊
|
|
778
|
+
return function (callback) {␊
|
|
779
|
+
setTimeout(callback, 0)␊
|
|
780
|
+
}␊
|
|
781
|
+
})()␊
|
|
782
|
+
␊
|
|
783
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
784
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
785
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
786
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
787
|
+
// bounded either way.␊
|
|
788
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
789
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
790
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
791
|
+
// call made on this thread (two), plus a margin.␊
|
|
792
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
793
|
+
␊
|
|
794
|
+
/**␊
|
|
795
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
796
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
797
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
798
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
799
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
800
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
801
|
+
*␊
|
|
802
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
803
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
804
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
805
|
+
* macrotask run.␊
|
|
806
|
+
*␊
|
|
807
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
808
|
+
* synchronous in the common case.␊
|
|
809
|
+
*␊
|
|
810
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
811
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
812
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
813
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
814
|
+
* the context with the barrier's settlements still queued.␊
|
|
815
|
+
*␊
|
|
816
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
817
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
818
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
819
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
820
|
+
* flag unset and disposal retryable.␊
|
|
821
|
+
*/␊
|
|
822
|
+
function __drainWasmEnvCleanup() {␊
|
|
823
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
824
|
+
return␊
|
|
825
|
+
}␊
|
|
826
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
827
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
828
|
+
}␊
|
|
829
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
830
|
+
const observable = typeof pending === 'function'␊
|
|
831
|
+
if (observable) {␊
|
|
832
|
+
let queued␊
|
|
833
|
+
try {␊
|
|
834
|
+
queued = pending()␊
|
|
835
|
+
} catch {␊
|
|
836
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
837
|
+
return␊
|
|
838
|
+
}␊
|
|
839
|
+
if (!queued) {␊
|
|
840
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
841
|
+
return␊
|
|
842
|
+
}␊
|
|
843
|
+
}␊
|
|
844
|
+
const limit = observable␊
|
|
845
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
846
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
847
|
+
const drainPromise = (async () => {␊
|
|
848
|
+
let queued = 0␊
|
|
849
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
850
|
+
await new Promise((resolve) => {␊
|
|
851
|
+
__scheduleMacrotask(resolve)␊
|
|
852
|
+
})␊
|
|
853
|
+
if (!observable) {␊
|
|
854
|
+
continue␊
|
|
855
|
+
}␊
|
|
856
|
+
try {␊
|
|
857
|
+
queued = pending()␊
|
|
858
|
+
} catch {␊
|
|
859
|
+
return␊
|
|
860
|
+
}␊
|
|
861
|
+
if (!queued) {␊
|
|
862
|
+
return␊
|
|
863
|
+
}␊
|
|
864
|
+
}␊
|
|
865
|
+
if (!observable) {␊
|
|
866
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
867
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
868
|
+
// finishing the drain.␊
|
|
869
|
+
return␊
|
|
870
|
+
}␊
|
|
871
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
872
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
873
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
874
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
875
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
876
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
877
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
878
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
879
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
880
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
881
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
882
|
+
const drainError = new Error(␊
|
|
883
|
+
'the wasm environment still reports ' +␊
|
|
884
|
+
queued +␊
|
|
885
|
+
' queued settlement(s) after ' +␊
|
|
886
|
+
limit +␊
|
|
887
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
888
|
+
)␊
|
|
889
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
890
|
+
throw drainError␊
|
|
891
|
+
})().then(␊
|
|
892
|
+
(value) => {␊
|
|
893
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
894
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
895
|
+
// queued rejects above and must stay repeatable.␊
|
|
896
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
897
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
898
|
+
return value␊
|
|
899
|
+
},␊
|
|
900
|
+
(error) => {␊
|
|
901
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
902
|
+
throw error␊
|
|
903
|
+
},␊
|
|
904
|
+
)␊
|
|
905
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
906
|
+
return drainPromise␊
|
|
907
|
+
}␊
|
|
908
|
+
␊
|
|
491
909
|
function __destroyEmnapiContext() {␊
|
|
492
910
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
493
911
|
__emnapiContextDestroyed = true␊
|
|
@@ -565,7 +983,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
565
983
|
return __completeWasiDisposal()␊
|
|
566
984
|
}␊
|
|
567
985
|
␊
|
|
568
|
-
function
|
|
986
|
+
function __continueWasiDisposal() {␊
|
|
569
987
|
const destroyResult = __destroyEmnapiContext()␊
|
|
570
988
|
if (__isThenable(destroyResult)) {␊
|
|
571
989
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -573,6 +991,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
573
991
|
return __finishWasiDisposal()␊
|
|
574
992
|
}␊
|
|
575
993
|
␊
|
|
994
|
+
function __startWasiDisposal() {␊
|
|
995
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
996
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
997
|
+
// back to back is what strands them.␊
|
|
998
|
+
__prepareWasmEnvCleanup()␊
|
|
999
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
1000
|
+
if (__isThenable(drainResult)) {␊
|
|
1001
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
1002
|
+
}␊
|
|
1003
|
+
return __continueWasiDisposal()␊
|
|
1004
|
+
}␊
|
|
1005
|
+
␊
|
|
576
1006
|
/**␊
|
|
577
1007
|
* Disposes this generated WASI binding.␊
|
|
578
1008
|
*␊
|
|
@@ -644,8 +1074,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
644
1074
|
return cleanupErrors␊
|
|
645
1075
|
}␊
|
|
646
1076
|
␊
|
|
647
|
-
function
|
|
648
|
-
const cleanupErrors = []␊
|
|
1077
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
649
1078
|
let destroyResult␊
|
|
650
1079
|
try {␊
|
|
651
1080
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -663,6 +1092,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
663
1092
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
664
1093
|
}␊
|
|
665
1094
|
␊
|
|
1095
|
+
/**␊
|
|
1096
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
1097
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
1098
|
+
*/␊
|
|
1099
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
1100
|
+
try {␊
|
|
1101
|
+
__retainWasiRollbackForRetry()␊
|
|
1102
|
+
} catch (cleanupError) {␊
|
|
1103
|
+
cleanupErrors.push(cleanupError)␊
|
|
1104
|
+
}␊
|
|
1105
|
+
return cleanupErrors␊
|
|
1106
|
+
}␊
|
|
1107
|
+
␊
|
|
1108
|
+
/**␊
|
|
1109
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
1110
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
1111
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
1112
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
1113
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
1114
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
1115
|
+
*␊
|
|
1116
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
1117
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
1118
|
+
*␊
|
|
1119
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
1120
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
1121
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
1122
|
+
* two trades, and not because of what it saves:␊
|
|
1123
|
+
*␊
|
|
1124
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
1125
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
1126
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
1127
|
+
* forever with nothing left that could ever settle it.␊
|
|
1128
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
1129
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
1130
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
1131
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
1132
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
1133
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
1134
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
1135
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
1136
|
+
* what makes that retained record useless.␊
|
|
1137
|
+
*␊
|
|
1138
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
1139
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
1140
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
1141
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
1142
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
1143
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
1144
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
1145
|
+
*/␊
|
|
1146
|
+
function __rollbackWasiInitialization() {␊
|
|
1147
|
+
const cleanupErrors = []␊
|
|
1148
|
+
let drainResult␊
|
|
1149
|
+
let settlementsUnreached = false␊
|
|
1150
|
+
try {␊
|
|
1151
|
+
__prepareWasmEnvCleanup()␊
|
|
1152
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
1153
|
+
} catch (cleanupError) {␊
|
|
1154
|
+
cleanupErrors.push(cleanupError)␊
|
|
1155
|
+
settlementsUnreached = true␊
|
|
1156
|
+
}␊
|
|
1157
|
+
if (__isThenable(drainResult)) {␊
|
|
1158
|
+
return Promise.resolve(drainResult).then(␊
|
|
1159
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
1160
|
+
(cleanupError) => {␊
|
|
1161
|
+
cleanupErrors.push(cleanupError)␊
|
|
1162
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1163
|
+
},␊
|
|
1164
|
+
)␊
|
|
1165
|
+
}␊
|
|
1166
|
+
if (settlementsUnreached) {␊
|
|
1167
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1168
|
+
}␊
|
|
1169
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1170
|
+
}␊
|
|
1171
|
+
␊
|
|
666
1172
|
let __wasiModule␊
|
|
667
1173
|
let __napiModule␊
|
|
668
1174
|
␊
|
|
@@ -785,9 +1291,16 @@ Generated by [AVA](https://avajs.dev).
|
|
|
785
1291
|
let __emnapiContextDestroyed = false␊
|
|
786
1292
|
let __emnapiContextDestroyPromise␊
|
|
787
1293
|
let __emnapiWasmEnvCleanupPrepared = false␊
|
|
1294
|
+
let __emnapiWasmEnvCleanupRan = false␊
|
|
1295
|
+
let __emnapiWasmEnvCleanupDrained = false␊
|
|
1296
|
+
let __emnapiWasmEnvCleanupDrainPromise␊
|
|
788
1297
|
let __wasiDisposed = false␊
|
|
789
1298
|
let __wasiDisposePromise␊
|
|
790
1299
|
let __completeWasiDisposal = function() {}␊
|
|
1300
|
+
// Overridden by loader flavors that have a last-resort reclaim for a rollback␊
|
|
1301
|
+
// that stopped short of destroying the context. See␊
|
|
1302
|
+
// \`__rollbackWasiInitialization\`.␊
|
|
1303
|
+
let __retainWasiRollbackForRetry = function() {}␊
|
|
791
1304
|
␊
|
|
792
1305
|
function __isThenable(value) {␊
|
|
793
1306
|
return (␊
|
|
@@ -858,10 +1371,168 @@ Generated by [AVA](https://avajs.dev).
|
|
|
858
1371
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup␊
|
|
859
1372
|
if (typeof prepare === 'function') {␊
|
|
860
1373
|
prepare()␊
|
|
1374
|
+
__emnapiWasmEnvCleanupRan = true␊
|
|
861
1375
|
}␊
|
|
862
1376
|
__emnapiWasmEnvCleanupPrepared = true␊
|
|
863
1377
|
}␊
|
|
864
1378
|
␊
|
|
1379
|
+
// Mirror the primitive @emnapi/core schedules its threadsafe-function dispatch␊
|
|
1380
|
+
// on, so the drain turns below interleave with that dispatch instead of racing␊
|
|
1381
|
+
// ahead of it on a faster queue.␊
|
|
1382
|
+
const __scheduleMacrotask = (function () {␊
|
|
1383
|
+
if (typeof setImmediate === 'function') {␊
|
|
1384
|
+
return function (callback) {␊
|
|
1385
|
+
setImmediate(callback)␊
|
|
1386
|
+
}␊
|
|
1387
|
+
}␊
|
|
1388
|
+
const __MessageChannel = globalThis.MessageChannel␊
|
|
1389
|
+
if (typeof __MessageChannel === 'function') {␊
|
|
1390
|
+
return function (callback) {␊
|
|
1391
|
+
const channel = new __MessageChannel()␊
|
|
1392
|
+
channel.port1.onmessage = function () {␊
|
|
1393
|
+
channel.port1.onmessage = null␊
|
|
1394
|
+
try {␊
|
|
1395
|
+
channel.port1.close()␊
|
|
1396
|
+
} catch {}␊
|
|
1397
|
+
try {␊
|
|
1398
|
+
channel.port2.close()␊
|
|
1399
|
+
} catch {}␊
|
|
1400
|
+
callback()␊
|
|
1401
|
+
}␊
|
|
1402
|
+
channel.port2.postMessage(null)␊
|
|
1403
|
+
}␊
|
|
1404
|
+
}␊
|
|
1405
|
+
return function (callback) {␊
|
|
1406
|
+
setTimeout(callback, 0)␊
|
|
1407
|
+
}␊
|
|
1408
|
+
})()␊
|
|
1409
|
+
␊
|
|
1410
|
+
// Turns to wait for while the addon still reports queued settlements. Reaching␊
|
|
1411
|
+
// zero is the only success. A counter still nonzero at this bound rejects the␊
|
|
1412
|
+
// disposal as retryable (\`ERR_NAPI_WASI_CLEANUP_PENDING\`) rather than␊
|
|
1413
|
+
// destroying the context over a still-queued settlement — the wait stays␊
|
|
1414
|
+
// bounded either way.␊
|
|
1415
|
+
const __WASM_ENV_CLEANUP_DRAIN_TURNS = 128␊
|
|
1416
|
+
// Without \`napi_wasm_env_cleanup_pending\` the queue is not observable. Fall␊
|
|
1417
|
+
// back to the number of turns @emnapi/core needs to coalesce and dispatch a␊
|
|
1418
|
+
// call made on this thread (two), plus a margin.␊
|
|
1419
|
+
const __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS = 4␊
|
|
1420
|
+
␊
|
|
1421
|
+
/**␊
|
|
1422
|
+
* \`napi_prepare_wasm_env_cleanup\` only *queues* the promise settlements of the␊
|
|
1423
|
+
* tasks it cancelled: \`napi_call_threadsafe_function\` appends to the␊
|
|
1424
|
+
* threadsafe-function queue, and @emnapi/core dispatches that queue from a␊
|
|
1425
|
+
* macrotask — two coalescing turns later, even for a call made on this very␊
|
|
1426
|
+
* thread. \`Context.destroy()\` then runs the threadsafe function's cleanup hook,␊
|
|
1427
|
+
* which drains the queue with a null env and *discards* whatever is still in it.␊
|
|
1428
|
+
*␊
|
|
1429
|
+
* So destroying without yielding first strands exactly the promises the barrier␊
|
|
1430
|
+
* exists to settle. Yield real event-loop turns until the addon reports the␊
|
|
1431
|
+
* queue empty; microtask checkpoints cannot help, no number of them lets a␊
|
|
1432
|
+
* macrotask run.␊
|
|
1433
|
+
*␊
|
|
1434
|
+
* Returns nothing when there is nothing to wait for, which keeps disposal␊
|
|
1435
|
+
* synchronous in the common case.␊
|
|
1436
|
+
*␊
|
|
1437
|
+
* The "already drained" flag is set only once a wait has actually finished.␊
|
|
1438
|
+
* Scheduling a macrotask can fail — a host-provided or patched \`setImmediate\`␊
|
|
1439
|
+
* that throws is enough — and a disposal that rejects stays retryable, so␊
|
|
1440
|
+
* marking the drain complete up front would make the retry skip it and destroy␊
|
|
1441
|
+
* the context with the barrier's settlements still queued.␊
|
|
1442
|
+
*␊
|
|
1443
|
+
* A wait that runs out of turns with the counter still nonzero rejects with␊
|
|
1444
|
+
* \`ERR_NAPI_WASI_CLEANUP_PENDING\` for the same reason: at that point␊
|
|
1445
|
+
* "finished" is indistinguishable from the stranding above, and destroying␊
|
|
1446
|
+
* would discard the very settlement the wait was for. The rejection leaves the␊
|
|
1447
|
+
* flag unset and disposal retryable.␊
|
|
1448
|
+
*/␊
|
|
1449
|
+
function __drainWasmEnvCleanup() {␊
|
|
1450
|
+
if (__emnapiWasmEnvCleanupDrained || !__emnapiWasmEnvCleanupRan) {␊
|
|
1451
|
+
return␊
|
|
1452
|
+
}␊
|
|
1453
|
+
if (__emnapiWasmEnvCleanupDrainPromise) {␊
|
|
1454
|
+
return __emnapiWasmEnvCleanupDrainPromise␊
|
|
1455
|
+
}␊
|
|
1456
|
+
const pending = __napiInstance?.exports?.napi_wasm_env_cleanup_pending␊
|
|
1457
|
+
const observable = typeof pending === 'function'␊
|
|
1458
|
+
if (observable) {␊
|
|
1459
|
+
let queued␊
|
|
1460
|
+
try {␊
|
|
1461
|
+
queued = pending()␊
|
|
1462
|
+
} catch {␊
|
|
1463
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1464
|
+
return␊
|
|
1465
|
+
}␊
|
|
1466
|
+
if (!queued) {␊
|
|
1467
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1468
|
+
return␊
|
|
1469
|
+
}␊
|
|
1470
|
+
}␊
|
|
1471
|
+
const limit = observable␊
|
|
1472
|
+
? __WASM_ENV_CLEANUP_DRAIN_TURNS␊
|
|
1473
|
+
: __WASM_ENV_CLEANUP_BLIND_DRAIN_TURNS␊
|
|
1474
|
+
const drainPromise = (async () => {␊
|
|
1475
|
+
let queued = 0␊
|
|
1476
|
+
for (let turn = 0; turn < limit; turn++) {␊
|
|
1477
|
+
await new Promise((resolve) => {␊
|
|
1478
|
+
__scheduleMacrotask(resolve)␊
|
|
1479
|
+
})␊
|
|
1480
|
+
if (!observable) {␊
|
|
1481
|
+
continue␊
|
|
1482
|
+
}␊
|
|
1483
|
+
try {␊
|
|
1484
|
+
queued = pending()␊
|
|
1485
|
+
} catch {␊
|
|
1486
|
+
return␊
|
|
1487
|
+
}␊
|
|
1488
|
+
if (!queued) {␊
|
|
1489
|
+
return␊
|
|
1490
|
+
}␊
|
|
1491
|
+
}␊
|
|
1492
|
+
if (!observable) {␊
|
|
1493
|
+
// Blind wait: without \`napi_wasm_env_cleanup_pending\` the bound IS the␊
|
|
1494
|
+
// contract — there is nothing to consult, so finishing the turns is␊
|
|
1495
|
+
// finishing the drain.␊
|
|
1496
|
+
return␊
|
|
1497
|
+
}␊
|
|
1498
|
+
// The counter is still nonzero after every turn the bound allows. The wait␊
|
|
1499
|
+
// stays bounded — but claiming success here would be indistinguishable from␊
|
|
1500
|
+
// the stranding this drain exists to prevent: disposal would go on to␊
|
|
1501
|
+
// destroy the context, whose cleanup hook discards the still-queued␊
|
|
1502
|
+
// settlement with a null env, and the promise it was for hangs forever.␊
|
|
1503
|
+
// Reject instead, as a retryable cleanup failure: the drained flag stays␊
|
|
1504
|
+
// unset, dispose() (and the rollback) decline to destroy, and a later␊
|
|
1505
|
+
// dispose() runs the drain again — by which time the queue has usually been␊
|
|
1506
|
+
// delivered. A counter that is somehow stuck nonzero therefore costs each␊
|
|
1507
|
+
// attempt at most another bounded wait and a rejection, never a stranded␊
|
|
1508
|
+
// promise; the process-exit teardown still reclaims the context.␊
|
|
1509
|
+
const drainError = new Error(␊
|
|
1510
|
+
'the wasm environment still reports ' +␊
|
|
1511
|
+
queued +␊
|
|
1512
|
+
' queued settlement(s) after ' +␊
|
|
1513
|
+
limit +␊
|
|
1514
|
+
' event-loop turns; the context was not destroyed - retry dispose() to wait for the queue again',␊
|
|
1515
|
+
)␊
|
|
1516
|
+
drainError.code = 'ERR_NAPI_WASI_CLEANUP_PENDING'␊
|
|
1517
|
+
throw drainError␊
|
|
1518
|
+
})().then(␊
|
|
1519
|
+
(value) => {␊
|
|
1520
|
+
// Set only when the wait actually finished AND the queue was seen empty␊
|
|
1521
|
+
// (or is unobservable): a drain that timed out with settlements still␊
|
|
1522
|
+
// queued rejects above and must stay repeatable.␊
|
|
1523
|
+
__emnapiWasmEnvCleanupDrained = true␊
|
|
1524
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
1525
|
+
return value␊
|
|
1526
|
+
},␊
|
|
1527
|
+
(error) => {␊
|
|
1528
|
+
__emnapiWasmEnvCleanupDrainPromise = undefined␊
|
|
1529
|
+
throw error␊
|
|
1530
|
+
},␊
|
|
1531
|
+
)␊
|
|
1532
|
+
__emnapiWasmEnvCleanupDrainPromise = drainPromise␊
|
|
1533
|
+
return drainPromise␊
|
|
1534
|
+
}␊
|
|
1535
|
+
␊
|
|
865
1536
|
function __destroyEmnapiContext() {␊
|
|
866
1537
|
if (__emnapiContextDestroyed || __emnapiContext === undefined) {␊
|
|
867
1538
|
__emnapiContextDestroyed = true␊
|
|
@@ -939,7 +1610,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
939
1610
|
return __completeWasiDisposal()␊
|
|
940
1611
|
}␊
|
|
941
1612
|
␊
|
|
942
|
-
function
|
|
1613
|
+
function __continueWasiDisposal() {␊
|
|
943
1614
|
const destroyResult = __destroyEmnapiContext()␊
|
|
944
1615
|
if (__isThenable(destroyResult)) {␊
|
|
945
1616
|
return Promise.resolve(destroyResult).then(__finishWasiDisposal)␊
|
|
@@ -947,6 +1618,18 @@ Generated by [AVA](https://avajs.dev).
|
|
|
947
1618
|
return __finishWasiDisposal()␊
|
|
948
1619
|
}␊
|
|
949
1620
|
␊
|
|
1621
|
+
function __startWasiDisposal() {␊
|
|
1622
|
+
// Run the pre-teardown barrier, then let the settlements it queued actually␊
|
|
1623
|
+
// reach JavaScript, and only then destroy the environment. Doing these two␊
|
|
1624
|
+
// back to back is what strands them.␊
|
|
1625
|
+
__prepareWasmEnvCleanup()␊
|
|
1626
|
+
const drainResult = __drainWasmEnvCleanup()␊
|
|
1627
|
+
if (__isThenable(drainResult)) {␊
|
|
1628
|
+
return Promise.resolve(drainResult).then(__continueWasiDisposal)␊
|
|
1629
|
+
}␊
|
|
1630
|
+
return __continueWasiDisposal()␊
|
|
1631
|
+
}␊
|
|
1632
|
+
␊
|
|
950
1633
|
/**␊
|
|
951
1634
|
* Disposes this generated WASI binding.␊
|
|
952
1635
|
*␊
|
|
@@ -1018,8 +1701,7 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1018
1701
|
return cleanupErrors␊
|
|
1019
1702
|
}␊
|
|
1020
1703
|
␊
|
|
1021
|
-
function
|
|
1022
|
-
const cleanupErrors = []␊
|
|
1704
|
+
function __destroyContextForWasiRollback(cleanupErrors) {␊
|
|
1023
1705
|
let destroyResult␊
|
|
1024
1706
|
try {␊
|
|
1025
1707
|
destroyResult = __destroyEmnapiContext()␊
|
|
@@ -1037,6 +1719,83 @@ Generated by [AVA](https://avajs.dev).
|
|
|
1037
1719
|
return __finishWasiInitializationRollback(cleanupErrors)␊
|
|
1038
1720
|
}␊
|
|
1039
1721
|
␊
|
|
1722
|
+
/**␊
|
|
1723
|
+
* Leaves a rollback that could not reach the queued settlements undestroyed, and␊
|
|
1724
|
+
* hands it to whatever this flavor has that can still reclaim it.␊
|
|
1725
|
+
*/␊
|
|
1726
|
+
function __retainFailedWasiRollback(cleanupErrors) {␊
|
|
1727
|
+
try {␊
|
|
1728
|
+
__retainWasiRollbackForRetry()␊
|
|
1729
|
+
} catch (cleanupError) {␊
|
|
1730
|
+
cleanupErrors.push(cleanupError)␊
|
|
1731
|
+
}␊
|
|
1732
|
+
return cleanupErrors␊
|
|
1733
|
+
}␊
|
|
1734
|
+
␊
|
|
1735
|
+
/**␊
|
|
1736
|
+
* Initialization can fail *after* registration has already run, and registration␊
|
|
1737
|
+
* runs with a live environment: a module-init hook can start async work and then␊
|
|
1738
|
+
* return an error, and the promise it created may already have escaped into␊
|
|
1739
|
+
* JavaScript. The barrier cancels that work and *queues* the settlement, so this␊
|
|
1740
|
+
* path needs the same drain the ordinary disposal does — destroying without␊
|
|
1741
|
+
* yielding discards the queue with a null env and strands the promise.␊
|
|
1742
|
+
*␊
|
|
1743
|
+
* Stays synchronous when nothing is queued, which covers every failure before␊
|
|
1744
|
+
* \`beforeInit\`: there is no instance to run the barrier on, so nothing to drain.␊
|
|
1745
|
+
*␊
|
|
1746
|
+
* A barrier or drain that did *not* finish stops the rollback short of␊
|
|
1747
|
+
* destroying, which is what \`dispose()\` already does — a rejected drain there␊
|
|
1748
|
+
* never reaches \`__continueWasiDisposal\`. Destroying anyway is the worse of the␊
|
|
1749
|
+
* two trades, and not because of what it saves:␊
|
|
1750
|
+
*␊
|
|
1751
|
+
* - It cannot deliver the settlements. \`Context.destroy()\` runs the␊
|
|
1752
|
+
* threadsafe function's cleanup hook, which drains the queue with a null env␊
|
|
1753
|
+
* and discards it, so a promise that already escaped into JavaScript hangs␊
|
|
1754
|
+
* forever with nothing left that could ever settle it.␊
|
|
1755
|
+
* - It saves less than it looks. \`Context.destroy()\` stops JavaScript calls␊
|
|
1756
|
+
* and runs cleanup hooks; it does not free the wasm instance or its Memory,␊
|
|
1757
|
+
* which this module's scope holds either way. What stopping short retains is␊
|
|
1758
|
+
* the emnapi context's bookkeeping and its un-run cleanup hooks.␊
|
|
1759
|
+
* - Retry is not theoretical. A rollback that records a cleanup error is␊
|
|
1760
|
+
* already kept in the process-wide registry above, so re-\`require()\`ing this␊
|
|
1761
|
+
* file replays it instead of re-instantiating — and the \`6e15de6f\` flag fix␊
|
|
1762
|
+
* means the replay drains again rather than skipping it. Destroying first is␊
|
|
1763
|
+
* what makes that retained record useless.␊
|
|
1764
|
+
*␊
|
|
1765
|
+
* The residual cost is honest: the CJS flavor hands the context to its␊
|
|
1766
|
+
* \`process.on('exit')\` teardown, so a process that never retries still reclaims␊
|
|
1767
|
+
* it on the way out. The ESM browser flavor has no equivalent — a module that␊
|
|
1768
|
+
* throws while evaluating is permanently errored, so re-importing rethrows␊
|
|
1769
|
+
* without re-running this file — and there the context stays until the realm␊
|
|
1770
|
+
* goes away. That is the deliberate choice: a hung promise is a silent liveness␊
|
|
1771
|
+
* bug with no upper bound, while the retained bookkeeping is bounded by the page.␊
|
|
1772
|
+
*/␊
|
|
1773
|
+
function __rollbackWasiInitialization() {␊
|
|
1774
|
+
const cleanupErrors = []␊
|
|
1775
|
+
let drainResult␊
|
|
1776
|
+
let settlementsUnreached = false␊
|
|
1777
|
+
try {␊
|
|
1778
|
+
__prepareWasmEnvCleanup()␊
|
|
1779
|
+
drainResult = __drainWasmEnvCleanup()␊
|
|
1780
|
+
} catch (cleanupError) {␊
|
|
1781
|
+
cleanupErrors.push(cleanupError)␊
|
|
1782
|
+
settlementsUnreached = true␊
|
|
1783
|
+
}␊
|
|
1784
|
+
if (__isThenable(drainResult)) {␊
|
|
1785
|
+
return Promise.resolve(drainResult).then(␊
|
|
1786
|
+
() => __destroyContextForWasiRollback(cleanupErrors),␊
|
|
1787
|
+
(cleanupError) => {␊
|
|
1788
|
+
cleanupErrors.push(cleanupError)␊
|
|
1789
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1790
|
+
},␊
|
|
1791
|
+
)␊
|
|
1792
|
+
}␊
|
|
1793
|
+
if (settlementsUnreached) {␊
|
|
1794
|
+
return __retainFailedWasiRollback(cleanupErrors)␊
|
|
1795
|
+
}␊
|
|
1796
|
+
return __destroyContextForWasiRollback(cleanupErrors)␊
|
|
1797
|
+
}␊
|
|
1798
|
+
␊
|
|
1040
1799
|
let __wasiModule␊
|
|
1041
1800
|
let __napiModule␊
|
|
1042
1801
|
␊
|