Skip to content

ErrorEvent and Worker.onerror (Worker step 3) - #82

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

ASDAlexander77 merged 1 commit into
mainfrom
worker-errors

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Step 3 of docs/worker-design.md: errors from a worker reach the thread that created it.

  • ErrorEvent (with message) is new in src/core/core.ts, declared in core.d.ts next to SyntaxError.
  • worker.onerror receives what the worker throws, delivered by join() and receive(). With no handler set, the error is written to stderr as Worker error: <message>.
  • A throw from a message handler becomes an error event, and the worker keeps handling later messages, as in a browser. A throw from the worker's setup function becomes an error event and the worker ends.
  • join() no longer throws. That was the stopgap from step 2.

Design decisions found while building it

  • One queue toward the creating thread carries both kinds of event. It holds the event objects themselves (type WorkerPost<Out> = MessageEvent<Out> | ErrorEvent), so errors arrive in order with the messages. join() and receive() tell them apart with instanceof ErrorEvent.
  • Why not an envelope object: a wrapper with a data: T | undefined field 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.
  • The error carries text: an Error's message, or ${e} for anything else. Under own that text is borrowed from the thrown object, so postError copies it.
  • receive(timeout): it hands errors to onerror on the way to the next message, and the timeout applies to each wait, not overall.

Tests

  • New: tests/worker_errors.ts, covering handler throws arriving in order with the replies, a throw from the setup function, and a thrown string.
  • Changed: tests/worker_echo.ts no longer has its "join() throws" case, which worker_errors replaces.

I ran these after full release builds of all four libraries:

  • 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's timestamp was the same before and after the test run. That mattered because something else on this machine was rebuilding tslang during the session, so I rebuilt and reran everything on the new compiler.

Checked by hand: the default onerror under gc and own. Stdout gets ALL DONE, stderr gets Worker error: nobody listening, and the exit code is 0. It isn't in a test because tests.ps1 fails any test that writes to stderr.

Not run locally:

  • The debug configuration: the machine's compiler debug runtime is out of date, so CI runs it first.
  • scripts/check_declarations.ps1: it hangs on this machine for main'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 new ErrorEvent declaration.

🤖 Generated with Claude Code

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>
@ASDAlexander77
ASDAlexander77 merged commit 6542c8c 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