Skip to content

Worker.terminate() and Worker.dispatch() (Worker step 4) - #83

Merged
ASDAlexander77 merged 1 commit into
mainfrom
worker-terminate
Nov 9, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
worker-terminate

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Step 4 of docs/worker-design.md: ending workers that never close themselves, and delivering their events from the creating thread's own loop.

  • dispatch(timeout?) hands everything that has arrived from the worker to onmessage / onerror and returns how many it delivered. It waits up to timeout for the first event only; with no timeout it doesn't wait. A program calls it from its own loop, in place of a browser's event loop.

  • terminate() ends a worker that never calls close(), which is the usual browser pattern:

    • messages still queued for the worker are dropped;
    • a handler that's already running finishes first, since the thread isn't interrupted, so a handler that never returns makes terminate() wait forever;
    • the worker's thread ends;
    • replies it posted but that haven't been delivered are dropped.

    Afterwards, onmessage and onerror are never called again for that worker, postMessage is ignored, and terminate() or join() do nothing.

  • MessageQueue.discard() closes a queue and empties it under one lock, so the worker can't take a message in between.

const worker = new Worker<int, int>((self: WorkerScope<int, int>) => {
    self.onmessage = (e: MessageEvent<int>) => self.postMessage(e.data * 2);
});
worker.onmessage = (e: MessageEvent<int>) => print(e.data);
worker.postMessage(1);
while (worker.dispatch(100) == 0) {}   // the creating thread's own loop
worker.terminate();

Tests

New: tests/worker_terminate.ts, covering:

  • dispatch() from the creating thread's loop, then terminating a worker that never closes;
  • terminating while 1,000 messages are still queued for a slow worker (most are dropped);
  • terminating while that worker's replies are still arriving (none is delivered afterwards);
  • postMessage, dispatch, receive, join and terminate after terminate();
  • terminating an idle worker, and one that already closed itself.

I ran these after rebuilding the library for all four memory models on the compiler build that was current at the time:

  • worker_terminate, worker_errors, worker_echo and worker_objects passed 5 runs out of 5 per model (gc, rc, none, own), compiled and under the JIT.
  • message_queue, thread_spawn, thread_move, atomics_wait and catch_library_error still pass under all four.
  • The compiler binary didn't change during the run. That mattered because something else on this machine was rebuilding tslang during the session.

Not run locally: the debug configuration, because this machine's compiler debug runtime is out of date. CI runs it first.

An -mm=own limit, written up in the design doc

It applies once the worker's function captures anything from the creating thread:

  • A handler that captures self, for example to post a reply, doesn't compile ("its value is the caller's").
  • A handler can use the creator's shared state if it captures a second handle made inside the function (const counter = handled; for a Shared<T>).

So a handler gets one or the other, not both. Workers whose function captures nothing are unaffected. worker_terminate tests each shape separately.

The design doc lists two possible fixes, neither applied:

  • pass the scope to onmessage as a second argument again;
  • send the shared state in a message.

It looks like a compiler limit rather than a deliberate rule.

🤖 Generated with Claude Code

dispatch(timeout?) hands every message and error that has arrived from
the worker to onmessage / onerror and says how many; it waits up to
timeout for the first only (none: no wait). It stands in for the event
loop in the creating thread's own loop.

terminate() ends a worker that never closes, as most browser workers:
what was queued for it is dropped, it stops once the running handler
returns (not interrupted), its thread ends, and what it posted but was
not delivered is dropped. Afterwards onmessage / onerror are not called
again, postMessage is ignored, and terminate / join do nothing.

MessageQueue.discard() closes a queue and drops what is in it under one
lock, so the worker can't take a message in between.

Tests: worker_terminate (dispatch then terminate, terminate with 1 000
messages queued, terminate with replies arriving, every call after
terminate, terminate an idle and a closed worker). Pass under gc, rc,
none and own, release, compiled and under the JIT, after full release
builds; debug not built locally.

docs/worker-design.md records an -mm=own limit found on the way: once
a worker's function captures anything from the creating thread, its
handler can use that state (through a second Shared handle) or post
with self, not both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 188675e into main Nov 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant