-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(worker_threads): Accept SHARE_ENV symbol in worker env option #21877
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
robobun
wants to merge
4
commits into
main
Choose a base branch
from
claude/fix-share-env-worker-threads
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b3015a
fix(worker_threads): Accept SHARE_ENV symbol in worker env option
8675343
fix(worker_threads): Improve SHARE_ENV implementation for Node.js com…
4dc6ab9
[autofix.ci] apply automated fixes
autofix-ci[bot] 04fbb2f
fix(worker_threads): Use symbol registry for SHARE_ENV comparison
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,147 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { SHARE_ENV, Worker } from "worker_threads"; | ||
|
|
||
| test("SHARE_ENV symbol should be accepted as env option", async () => { | ||
| // This test verifies that the SHARE_ENV symbol is properly accepted | ||
| // as the env option in worker threads, fixing the issue where it was | ||
| // incorrectly rejected as an invalid type | ||
|
|
||
| const worker = new Worker( | ||
| ` | ||
| const { parentPort } = require('worker_threads'); | ||
| // Send back the current environment variable to verify SHARE_ENV works | ||
| parentPort.postMessage({ | ||
| PATH: process.env.PATH ? 'present' : 'absent', | ||
| NODE_ENV: process.env.NODE_ENV | ||
| }); | ||
| `, | ||
| { | ||
| eval: true, | ||
| env: SHARE_ENV, | ||
| }, | ||
| ); | ||
|
|
||
| const message = await new Promise((resolve, reject) => { | ||
| worker.on("message", resolve); | ||
| worker.on("error", reject); | ||
| setTimeout(() => reject(new Error("Test timeout")), 5000); | ||
| }); | ||
|
|
||
| // Verify that environment variables are shared from parent process | ||
| expect(message).toHaveProperty("PATH"); | ||
| expect((message as any).PATH).toBe("present"); | ||
|
|
||
| await worker.terminate(); | ||
| }); | ||
|
|
||
| test("SHARE_ENV enables true environment sharing", async () => { | ||
| // Set a unique test variable in the parent | ||
| const testVar = `TEST_VAR_${Date.now()}`; | ||
| process.env[testVar] = "parent_value"; | ||
|
|
||
| const worker = new Worker( | ||
| ` | ||
| const { parentPort } = require('worker_threads'); | ||
| // Worker should see the parent's environment variable | ||
| parentPort.postMessage({ | ||
| testVar: process.env["${testVar}"], | ||
| hasTestVar: "${testVar}" in process.env | ||
| }); | ||
| `, | ||
| { | ||
| eval: true, | ||
| env: SHARE_ENV, | ||
| }, | ||
| ); | ||
|
|
||
| const message = await new Promise((resolve, reject) => { | ||
| worker.on("message", resolve); | ||
| worker.on("error", reject); | ||
| setTimeout(() => reject(new Error("Test timeout")), 5000); | ||
| }); | ||
|
|
||
| // Verify the worker can see the parent's environment variable | ||
| expect((message as any).hasTestVar).toBe(true); | ||
| expect((message as any).testVar).toBe("parent_value"); | ||
|
|
||
| // Clean up | ||
| delete process.env[testVar]; | ||
| await worker.terminate(); | ||
| }); | ||
|
|
||
| test("SHARE_ENV should be the correct symbol", () => { | ||
| // Verify that SHARE_ENV is the expected symbol | ||
| expect(typeof SHARE_ENV).toBe("symbol"); | ||
| expect(SHARE_ENV.description).toBe("nodejs.worker_threads.SHARE_ENV"); | ||
| }); | ||
|
|
||
| test("non-SHARE_ENV symbols should still be rejected", async () => { | ||
| const someOtherSymbol = Symbol("other.symbol"); | ||
|
|
||
| expect(() => { | ||
| new Worker("", { | ||
| eval: true, | ||
| env: someOtherSymbol as any, | ||
| }); | ||
| }).toThrow( | ||
| /The "options\.env" property must be of type object or one of undefined, null, or worker_threads\.SHARE_ENV/, | ||
| ); | ||
| }); | ||
|
|
||
| test("other env option types should still work", async () => { | ||
| // Test that regular object env still works | ||
| const worker1 = new Worker( | ||
| ` | ||
| const { parentPort } = require('worker_threads'); | ||
| parentPort.postMessage(process.env.CUSTOM_VAR); | ||
| `, | ||
| { | ||
| eval: true, | ||
| env: { CUSTOM_VAR: "custom_value" }, | ||
| }, | ||
| ); | ||
|
|
||
| const message1 = await new Promise((resolve, reject) => { | ||
| worker1.on("message", resolve); | ||
| worker1.on("error", reject); | ||
| setTimeout(() => reject(new Error("Test timeout")), 5000); | ||
| }); | ||
|
|
||
| expect(message1).toBe("custom_value"); | ||
| await worker1.terminate(); | ||
|
|
||
| // Test that undefined env still works | ||
| const worker2 = new Worker( | ||
| ` | ||
| const { parentPort } = require('worker_threads'); | ||
| parentPort.postMessage('success'); | ||
| `, | ||
| { | ||
| eval: true, | ||
| env: undefined, | ||
| }, | ||
| ); | ||
|
|
||
| const message2 = await new Promise((resolve, reject) => { | ||
| worker2.on("message", resolve); | ||
| worker2.on("error", reject); | ||
| setTimeout(() => reject(new Error("Test timeout")), 5000); | ||
| }); | ||
|
|
||
| expect(message2).toBe("success"); | ||
| await worker2.terminate(); | ||
|
|
||
| // Test that null env still works | ||
| expect(() => { | ||
| new Worker( | ||
| ` | ||
| const { parentPort } = require('worker_threads'); | ||
| parentPort.postMessage('success'); | ||
| `, | ||
| { | ||
| eval: true, | ||
| env: null, | ||
| }, | ||
| ); | ||
| }).not.toThrow(); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct. It needs to use symbolRegistry()->keyFor or something along those lines. It needs to use the sybmol registry and check the two Symbol's are equal and not the description value