Repository navigation
Worker.terminate() and Worker.dispatch() (Worker step 4) - #83
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 toonmessage/onerrorand returns how many it delivered. It waits up totimeoutfor 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 callsclose(), which is the usual browser pattern:terminate()wait forever;Afterwards,
onmessageandonerrorare never called again for that worker,postMessageis ignored, andterminate()orjoin()do nothing.MessageQueue.discard()closes a queue and empties it under one lock, so the worker can't take a message in between.Tests
New:
tests/worker_terminate.ts, covering:dispatch()from the creating thread's loop, then terminating a worker that never closes;postMessage,dispatch,receive,joinandterminateafterterminate();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_echoandworker_objectspassed 5 runs out of 5 per model (gc,rc,none,own), compiled and under the JIT.message_queue,thread_spawn,thread_move,atomics_waitandcatch_library_errorstill pass under all four.tslangduring 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=ownlimit, written up in the design docIt applies once the worker's function captures anything from the creating thread:
self, for example to post a reply, doesn't compile ("its value is the caller's").const counter = handled;for aShared<T>).So a handler gets one or the other, not both. Workers whose function captures nothing are unaffected.
worker_terminatetests each shape separately.The design doc lists two possible fixes, neither applied:
onmessageas a second argument again;It looks like a compiler limit rather than a deliberate rule.
🤖 Generated with Claude Code