Repository navigation
ErrorEvent and Worker.onerror (Worker step 3) - #82
Merged
Merged
Conversation
What a worker throws reaches the creating thread as an ErrorEvent, which
join() and receive() hand to worker.onerror; with none set, it is
written to stderr ("Worker error: <message>"). A throw from a handler
leaves the worker running, as in a browser; a throw from the worker's
function ends it. join() no longer throws.
- One queue toward the creator carries both kinds of event, as the
event objects themselves (MessageEvent<Out> | ErrorEvent), so errors
arrive in order with the messages. An envelope with a T | undefined
field does not work under -mm=own: a class instance can't be moved
out of the field.
- The event carries the thrown thing as text (an Error's message, else
`${e}`), copied, since under own it is borrowed from the thrown object.
- ErrorEvent is in core.ts / core.d.ts, next to SyntaxError.
Tests: worker_errors (handler throws in order with the replies, a throw
from the worker's function, a thrown string); worker_echo drops its
join-throws case. Pass under gc, rc, none and own, release, compiled
and under the JIT, after full release builds; debug not built locally.
The default stderr onerror was checked by hand (the test runner fails
on stderr). scripts/check_declarations.ps1 hangs here on main's
sources as well, so it was not run to completion.
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 3 of
docs/worker-design.md: errors from a worker reach the thread that created it.ErrorEvent(withmessage) is new insrc/core/core.ts, declared incore.d.tsnext toSyntaxError.worker.onerrorreceives what the worker throws, delivered byjoin()andreceive(). With no handler set, the error is written to stderr asWorker error: <message>.join()no longer throws. That was the stopgap from step 2.Design decisions found while building it
type WorkerPost<Out> = MessageEvent<Out> | ErrorEvent), so errors arrive in order with the messages.join()andreceive()tell them apart withinstanceof ErrorEvent.data: T | undefinedfield doesn't work under-mm=own, because the message can't be moved back out of the field. A destructuring swap works for strings but not for class instances.Error'smessage, or${e}for anything else. Underownthat text is borrowed from the thrown object, sopostErrorcopies it.receive(timeout): it hands errors toonerroron the way to the next message, and the timeout applies to each wait, not overall.Tests
tests/worker_errors.ts, covering handler throws arriving in order with the replies, a throw from the setup function, and a thrown string.tests/worker_echo.tsno longer has its "join()throws" case, whichworker_errorsreplaces.I ran these after full release builds of all four libraries:
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, so I rebuilt and reran everything on the new compiler.Checked by hand: the default
onerrorundergcandown. Stdout getsALL DONE, stderr getsWorker error: nobody listening, and the exit code is 0. It isn't in a test becausetests.ps1fails any test that writes to stderr.Not run locally:
scripts/check_declarations.ps1: it hangs on this machine formain's sources too, even with a private copy of the compiler. Both of its compile steps finish in 1–2 seconds when run directly. CI doesn't run it, so it gave no result for the newErrorEventdeclaration.🤖 Generated with Claude Code