-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
worker: fix Web Worker spec deviations #66354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
panva
wants to merge
6
commits into
nodejs:main
Choose a base branch
from
panva:webworker-spec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
53a4c67
worker: fix postMessage overload resolution
panva d3e37cf
worker: convert importScripts arguments first
panva c186605
worker: preserve blob module URLs
panva 2ee9a66
worker: serialize messages after exit
panva 5a55669
worker: discard queued messages on termination
panva 06ef7b4
fixup! worker: fix postMessage overload resolution
panva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Flags: --experimental-web-worker | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| if (!common.hasCrypto) common.skip('missing crypto'); | ||
|
|
||
| const assert = require('node:assert'); | ||
| const source = ` | ||
| import value from 'data:text/javascript,export default 42'; | ||
| postMessage([import.meta.url, location.href, value]); | ||
| `; | ||
| const url = URL.createObjectURL(new Blob([source], { type: 'text/javascript' })); | ||
| const worker = new Worker(url, { type: 'module' }); | ||
| URL.revokeObjectURL(url); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| worker.onmessage = common.mustCall(({ data }) => { | ||
| worker.terminate(); | ||
| assert.deepStrictEqual(data, [url, url, 42]); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Flags: --experimental-web-worker | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
|
|
||
| for (const type of ['classic', 'module']) { | ||
| const source = ` | ||
| const results = []; | ||
| const conversions = []; | ||
| const sentinel = new Error('conversion'); | ||
| try { | ||
| importScripts('https://[', { | ||
| toString() { conversions.push('converted'); throw sentinel; } | ||
| }); | ||
| } catch (error) { | ||
| results.push([conversions, error === sentinel]); | ||
| } | ||
| const order = []; | ||
| try { | ||
| importScripts( | ||
| { toString() { order.push(1); return 'https://['; } }, | ||
| { toString() { order.push(2); return 'data:text/javascript,'; } } | ||
| ); | ||
| } catch (error) { | ||
| results.push([order, error.name]); | ||
| } | ||
| postMessage(results); | ||
| `; | ||
| const worker = new Worker(`data:text/javascript,${encodeURIComponent(source)}`, { type }); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| worker.onmessage = common.mustCall(({ data }) => { | ||
| worker.terminate(); | ||
| assert.deepStrictEqual(data, [ | ||
| [['converted'], true], | ||
| [[1, 2], type === 'module' ? 'TypeError' : 'SyntaxError'], | ||
| ]); | ||
| }); | ||
| } | ||
|
|
||
| if (common.hasCrypto) { | ||
| // URL parsing must capture blob entries after all arguments are converted. | ||
| const source = ` | ||
| self.ran = false; | ||
| const url = URL.createObjectURL(new Blob(['self.ran = true'], { | ||
| type: 'text/javascript' | ||
| })); | ||
| let errorName; | ||
| try { | ||
| importScripts(url, { | ||
| toString() { | ||
| URL.revokeObjectURL(url); | ||
| return 'data:text/javascript,'; | ||
| } | ||
| }); | ||
| } catch (error) { | ||
| errorName = error.name; | ||
| } | ||
| postMessage([self.ran, errorName]); | ||
| `; | ||
| const worker = new Worker(`data:text/javascript,${encodeURIComponent(source)}`); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| worker.onmessage = common.mustCall(({ data }) => { | ||
| worker.terminate(); | ||
| assert.deepStrictEqual(data, [false, 'NetworkError']); | ||
| }); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Flags: --experimental-web-worker | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
|
|
||
| function checkSerialization(worker) { | ||
| assert.throws(() => worker.postMessage(() => {}), { name: 'DataCloneError' }); | ||
| const buffer = new ArrayBuffer(8); | ||
| assert.throws(() => worker.postMessage(null, [buffer, buffer]), { name: 'DataCloneError' }); | ||
| assert.strictEqual(buffer.byteLength, 8); | ||
| worker.postMessage(null, [buffer]); | ||
| assert.strictEqual(buffer.byteLength, 0); | ||
| } | ||
|
|
||
| // A failed script fetch still leaves an outside port that serializes messages. | ||
| { | ||
| const worker = new Worker('data:text/plain,'); | ||
| worker.onerror = common.mustCall(() => checkSerialization(worker)); | ||
| checkSerialization(worker); | ||
| } | ||
|
|
||
| // Serialization is required even after the backing thread has exited. | ||
| { | ||
| const worker = new Worker('data:text/javascript,close()'); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| process.once('worker', common.mustCall((thread) => { | ||
| thread.once('exit', common.mustCall(() => checkSerialization(worker))); | ||
| })); | ||
| } | ||
|
|
||
| // Queue messages before terminating so the test does not depend on thread speed. | ||
| { | ||
| const source = ` | ||
| onmessage = ({ data }) => { | ||
| for (let i = 0; i < 3; i++) postMessage(i); | ||
| const state = new Int32Array(data); | ||
| Atomics.store(state, 0, 1); | ||
| Atomics.notify(state, 0); | ||
| }; | ||
| `; | ||
| const worker = new Worker(`data:text/javascript,${encodeURIComponent(source)}`); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| worker.onmessage = common.mustNotCall('message delivered after terminate()'); | ||
| const state = new Int32Array(new SharedArrayBuffer(4)); | ||
| worker.postMessage(state.buffer); | ||
| assert.notStrictEqual(Atomics.wait(state, 0, 0, common.platformTimeout(10000)), 'timed-out'); | ||
| worker.terminate(); | ||
| worker.terminate(); | ||
| checkSerialization(worker); | ||
| } | ||
|
|
||
| // terminate() can run during message dispatch, including while the backing | ||
| // thread is draining messages on exit. Finish this event but discard later ones. | ||
| for (const closeAfterPosting of [false, true]) { | ||
| const source = ` | ||
| onmessage = ({ data }) => { | ||
| for (let i = 0; i < 3; i++) postMessage(i); | ||
| const state = new Int32Array(data); | ||
| Atomics.store(state, 0, 1); | ||
| Atomics.notify(state, 0); | ||
| if (${closeAfterPosting}) close(); | ||
| }; | ||
| `; | ||
| const worker = new Worker(`data:text/javascript,${encodeURIComponent(source)}`); | ||
| worker.onerror = common.mustNotCall('worker failed'); | ||
| worker.addEventListener('message', common.mustCall(({ data }) => { | ||
| assert.strictEqual(data, 0); | ||
| worker.terminate(); | ||
| checkSerialization(worker); | ||
| })); | ||
| worker.addEventListener('message', common.mustCall(({ data }) => { | ||
| assert.strictEqual(data, 0); | ||
| })); | ||
| const state = new Int32Array(new SharedArrayBuffer(4)); | ||
| worker.postMessage(state.buffer); | ||
| assert.notStrictEqual(Atomics.wait(state, 0, 0, common.platformTimeout(10000)), 'timed-out'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.