@webpodjs/net-worker 0.1.0 → 0.1.2

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/abi/ring.js CHANGED
@@ -267,25 +267,97 @@ export class RingConsumer {
267
267
  return [this.#r.words, HEAD, this.#tail | 0];
268
268
  }
269
269
  }
270
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
271
- /** Sleeps until the word changes from `value`, a notify, or the watchdog fires. Never blocks the thread. */
270
+ /** At most one outstanding waiter per word (see waitWord). */
271
+ const waiters = new WeakMap();
272
+ /** This thread's waiters that have not settled yet, given up ones included. */
273
+ const pending = new Set();
274
+ /**
275
+ * Settles this thread's outstanding ring waits (a notify on each word) and resolves once they have
276
+ * settled, or after `timeoutMs`. A Worker calls it before it is terminated: the waits have no
277
+ * timeout, and iOS 26 hung `Worker.terminate()` on the page while one was pending (T49).
278
+ */
279
+ export async function settleWaits(timeoutMs = 200) {
280
+ if (pending.size === 0)
281
+ return;
282
+ const all = [...pending];
283
+ for (const w of all)
284
+ Atomics.notify(w.words, w.index);
285
+ let timer;
286
+ await Promise.race([Promise.all(all.map((w) => w.settled)), new Promise((r) => (timer = setTimeout(r, timeoutMs)))]);
287
+ clearTimeout(timer);
288
+ }
289
+ /**
290
+ * Sleeps until the word changes from `value`, a notify, the signal aborts, or the watchdog fires.
291
+ * Never blocks the thread.
292
+ *
293
+ * The waitAsync has no timeout and outlives the sleep: a notify wakes every waiter on the word
294
+ * whatever value it waited for, so the next sleep on the same word reuses it, and a new one is
295
+ * made only once it has settled. A waitAsync with a timeout per sleep, raced with a timer as
296
+ * before, made iOS 26 slower with every sleep: wake-ups took 16x longer within five minutes of a
297
+ * busy ring (bench/net wa-pace.html, T49), where one outstanding waiter stays flat. The watchdog
298
+ * still bounds each sleep (WebKit can lose waitAsync wake-ups, P1 #133); when it finds the word
299
+ * changed under a waiter that never fired, that waiter is given up and the next sleep makes a
300
+ * fresh one.
301
+ */
272
302
  async function waitWord(words, index, value, options) {
273
303
  const ms = options.watchdogMs ?? 50;
304
+ const signal = options.signal;
305
+ if (signal?.aborted) {
306
+ // As before: an aborted signal still sleeps to the watchdog, a macrotask. Callers that loop on
307
+ // other conditions (channel.ts) would otherwise spin in microtasks and freeze the page.
308
+ await new Promise((r) => setTimeout(r, ms));
309
+ return;
310
+ }
274
311
  const waitAsync = Atomics.waitAsync;
275
- const abort = options.signal
276
- ? new Promise((r) => options.signal.addEventListener("abort", () => r(), { once: true }))
277
- : undefined;
278
- const timers = [sleep(ms)];
279
- if (abort)
280
- timers.push(abort);
312
+ let waiter;
281
313
  if (typeof waitAsync === "function") {
282
- const res = waitAsync(words, index, value, ms);
283
- if (!res.async)
314
+ let byIndex = waiters.get(words);
315
+ if (!byIndex)
316
+ waiters.set(words, (byIndex = new Map()));
317
+ waiter = byIndex.get(index);
318
+ if (!waiter) {
319
+ const res = waitAsync(words, index, value);
320
+ if (!res.async)
321
+ return;
322
+ const map = byIndex;
323
+ const w = { words, index, sleepers: new Set(), settled: Promise.resolve() };
324
+ // One reaction per waiter: it may outlive many sleeps, and a reaction per sleep would pile
325
+ // up on it while the ring is quiet.
326
+ w.settled = res.value.then(() => {
327
+ pending.delete(w);
328
+ if (map.get(index) === w)
329
+ map.delete(index);
330
+ for (const wake of w.sleepers)
331
+ wake();
332
+ });
333
+ pending.add(w);
334
+ byIndex.set(index, w);
335
+ waiter = w;
336
+ }
337
+ else if (Atomics.load(words, index) !== value)
284
338
  return;
285
- await Promise.race([res.value, ...timers]);
286
- return;
287
339
  }
288
- await Promise.race(timers);
340
+ let timedOut = false;
341
+ await new Promise((resolve) => {
342
+ let timer;
343
+ const end = () => {
344
+ clearTimeout(timer);
345
+ signal?.removeEventListener("abort", end);
346
+ waiter?.sleepers.delete(end);
347
+ resolve();
348
+ };
349
+ timer = setTimeout(() => {
350
+ timedOut = true;
351
+ end();
352
+ }, ms);
353
+ signal?.addEventListener("abort", end, { once: true });
354
+ waiter?.sleepers.add(end);
355
+ });
356
+ if (timedOut && waiter && Atomics.load(words, index) !== value) {
357
+ const byIndex = waiters.get(words);
358
+ if (byIndex?.get(index) === waiter)
359
+ byIndex.delete(index);
360
+ }
289
361
  }
290
362
  /** Waits until the consumer has a message (or the watchdog fires; callers just re-check). */
291
363
  export async function waitReadable(c, options = {}) {
@@ -678,7 +678,7 @@ function __wbg_get_imports() {
678
678
  return ret;
679
679
  },
680
680
  __wbindgen_generic_0000000000000002: function(arg0, arg1) {
681
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 806, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
681
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 807, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
682
682
  const ret = makeMutClosure(arg0, arg1, wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue__core_6a51abefd5362b2___result__Result_____wasm_bindgen_5a2380350bab5903___JsError___true_);
683
683
  return ret;
684
684
  },
Binary file
package/dist/worker.js CHANGED
@@ -10,7 +10,7 @@
10
10
  // worker -> host: ready, ca { pem }, relay-token-request { id }, peer-added { id }, stats { stats },
11
11
  // stopped, log, error
12
12
  import init, { NetPod, set_logger } from "./pkg/webpod_net_wasm.js";
13
- import { RingConsumer, RingProducer, waitReadable } from "./abi/index.js";
13
+ import { RingConsumer, RingProducer, settleWaits, waitReadable } from "./abi/index.js";
14
14
 
15
15
  const ready = init();
16
16
  const post = (m, transfer = []) => self.postMessage(m, transfer);
@@ -202,6 +202,8 @@ self.onmessage = async ({ data: m }) => {
202
202
  case "stop":
203
203
  stopping = true;
204
204
  wake?.();
205
+ // The ring waits have no timeout: settle them before the host terminates this Worker.
206
+ await settleWaits();
205
207
  post({ type: "stopped" });
206
208
  break;
207
209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webpodjs/net-worker",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Net Worker for webpod: the guest's gateway (webpod-net NetStack), browser egress and page connections",
5
5
  "keywords": [
6
6
  "webpod",