You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1fd6b65
Browse filesBrowse the repository at this point in the historyBrowse files
fix(worker): route worker_threads errors like the web surface, and fire once listeners once (#2089)
* fix(worker): route parentPort listener throws and handled Node-style errors like the web surface
A parentPort listener that threw was dispatched without rethrowing, so the error went to the uncaught-error reporter and never reached the worker's onerror or its parent. The relay now dispatches the way worker-global message delivery does.
The worker_threads Worker's onerror handler returned nothing, so an error its 'error' listeners took was also reported to the parent's global scope as unhandled. It returns whether a listener ran, which cancels the event.
* fix(worker): fire a worker_threads once listener at most once under a reentrant emit
An earlier listener that emitted the same event again let the nested emit fire and remove a later once listener, and the outer emit then called it a second time from its snapshot. A once registration now records that it fired, as Node's once wrapper does.
* fix(worker): hand worker_threads 'error' listeners an Error rebuilt from the thrown value
A worker_threads 'error' listener received the runtime's ErrorEvent, not an Error, and the message it carried was V8's "Uncaught Error: ..." text. The worker now reads the thrown value's name and message and forwards them with the error payload. The parent rebuilds an Error from them with the worker's stack, using the built-in constructor the name belongs to, and the parent's global error event carries that same Error when nothing handled it.
The Worker shim cancels an error its listeners took with preventDefault() rather than through the truthy-return contract of onerror.
* fix(worker): keep a forwarded worker error exact when its stack getter throws
Reading the stack of the error a worker's onerror threw could run a `stack` getter, and a getter that threw replaced that error in the TryCatch holding it, so the parent rebuilt the getter's error instead. The stack read now runs under its own TryCatch.
The thrown value's name and message travel to the parent as UTF-16, so an unpaired surrogate arrives as thrown rather than as U+FFFD. ArgConverter::ConvertToString copies by length, so an embedded NUL no longer cuts a converted string short.
* docs(worker): match the #reportExit comment to iOS
Co-authored-by: Adrian Niculescu <15037449+adrian-niculescu@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/worker-threads.md
+17-1Lines changed: 17 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ means deliberately unsupported.
54
54
|`threadName`| shim | Always `undefined`. |
55
55
|`workerData`| shim | Always `null` — see below. |
56
56
|`parentPort`| shim |`null` on the main isolate. Inside a worker, a `MessagePort`-shaped `EventTarget` over the worker's existing parent channel: `postMessage` forwards to the global `postMessage`, `message`/`messageerror` are re-dispatched from the worker global scope, `start()` and `close()` are no-ops. It is **not** a real port: not transferable, no queue of its own. |
57
-
|`Worker`| shim | A class over the runtime's global `Worker` with a small Node-style emitter (`on`/`once`/`off`/`removeListener`) for `message`, `messageerror`, `error`, `online` and `exit`. `postMessage(value, transfer)` and `terminate()` forward. `online` is emitted off a microtask after construction, not from the thread. `exit` (always code `0`) fires exactly once, when the thread has ended, whether the worker was terminated or ended by its own `close()`; `terminate()` resolves at the same point. Unsupported options throw a `TypeError` naming the option: `workerData`, `env`, `eval`, `transferList`, and `stdin`/`stdout`/`stderr` when explicitly truthy. The runtime's own `Worker` options (`androidPriority`) ride along untouched — the native constructor ignores keys it does not know. |
57
+
|`Worker`| shim | A class over the runtime's global `Worker` with a small Node-style emitter (`on`/`once`/`off`/`removeListener`) for `message`, `messageerror`, `error`, `online` and `exit`. An `error` listener receives the worker's error rebuilt as an `Error` (see below). `postMessage(value, transfer)` and `terminate()` forward. `online` is emitted off a microtask after construction, not from the thread. `exit` (always code `0`) fires exactly once, when the thread has ended, whether the worker was terminated or ended by its own `close()`; `terminate()` resolves at the same point. Unsupported options throw a `TypeError` naming the option: `workerData`, `env`, `eval`, `transferList`, and `stdin`/`stdout`/`stderr` when explicitly truthy. The runtime's own `Worker` options (`androidPriority`) ride along untouched — the native constructor ignores keys it does not know. |
58
58
|`postMessageToThread`| throws |`Error: postMessageToThread is not supported in this runtime`. |
59
59
|`moveMessagePortToContext`| throws |`Error: moveMessagePortToContext is not supported in this runtime`. |
60
60
|`locks`| absent | Web Locks are not implemented; the property does not exist. |
@@ -83,6 +83,22 @@ the code is `0` whichever way the worker ended — `terminate()`, its own
83
83
delivers the notification, so a `terminate()` awaited from a dying isolate
84
84
stays pending, as it does in Node when the parent process exits.
85
85
86
+
### An `error` listener receives a rebuilt `Error`
87
+
88
+
Node hands an `error` listener the worker's error deserialized on the parent.
89
+
Only strings cross the isolate boundary here, so the parent rebuilds it from
90
+
the thrown value's `name` and `message`, with the worker's stack as its
91
+
`stack`. A built-in name (`TypeError`, `RangeError`, …) rebuilds with that
92
+
constructor, so `instanceof` holds; any other name, a subclass's or a
93
+
`DOMException`'s, is an own `name` on an `Error`. Other properties, such as a
94
+
`code` or a `cause`, are not carried. A thrown value with no string `message`,
95
+
such as a string or a number, arrives as an `Error` whose message is its string
96
+
form, where Node hands over the value itself.
97
+
98
+
Once an `error` listener has received it, the error counts as handled and is
99
+
not reported on the parent's global scope. With no `error` listener, the
100
+
parent's global `error` event carries the same rebuilt `Error`.
101
+
86
102
### A worker error carries no `error` object, and the worker scope's `onerror` is not an event
87
103
88
104
An error the worker scope leaves unhandled reaches the parent as a real
0 commit comments