-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
sea: only assert snapshot main function for main threads #56120
Merged
nodejs-github-bot
merged 2 commits into
nodejs:main
from
joyeecheung:fix-worker-snapshot-sea
Dec 7, 2024
+85
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
80 changes: 80 additions & 0 deletions
80
test/sequential/test-single-executable-application-snapshot-worker.js
This file contains 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,80 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const { | ||
generateSEA, | ||
skipIfSingleExecutableIsNotSupported, | ||
} = require('../common/sea'); | ||
|
||
skipIfSingleExecutableIsNotSupported(); | ||
|
||
// This tests the snapshot support in single executable applications. | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
const { writeFileSync, existsSync } = require('fs'); | ||
const { | ||
spawnSyncAndAssert, spawnSyncAndExitWithoutError, | ||
} = require('../common/child_process'); | ||
const assert = require('assert'); | ||
|
||
const configFile = tmpdir.resolve('sea-config.json'); | ||
const seaPrepBlob = tmpdir.resolve('sea-prep.blob'); | ||
const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'sea'); | ||
|
||
{ | ||
tmpdir.refresh(); | ||
|
||
// FIXME(joyeecheung): currently `worker_threads` cannot be loaded during the | ||
// snapshot building process because internal/worker.js is accessing isMainThread at | ||
// the top level (and there are maybe more code that access these at the top-level), | ||
// and have to be loaded in the deserialized snapshot main function. | ||
// Change these states to be accessed on-demand. | ||
const code = ` | ||
const { | ||
setDeserializeMainFunction, | ||
} = require('v8').startupSnapshot; | ||
setDeserializeMainFunction(() => { | ||
const { Worker } = require('worker_threads'); | ||
new Worker("console.log('Hello from Worker')", { eval: true }); | ||
}); | ||
`; | ||
|
||
writeFileSync(tmpdir.resolve('snapshot.js'), code, 'utf-8'); | ||
writeFileSync(configFile, ` | ||
{ | ||
"main": "snapshot.js", | ||
"output": "sea-prep.blob", | ||
"useSnapshot": true | ||
} | ||
`); | ||
|
||
spawnSyncAndExitWithoutError( | ||
process.execPath, | ||
['--experimental-sea-config', 'sea-config.json'], | ||
{ | ||
cwd: tmpdir.path, | ||
env: { | ||
NODE_DEBUG_NATIVE: 'SEA', | ||
...process.env, | ||
}, | ||
}); | ||
|
||
assert(existsSync(seaPrepBlob)); | ||
|
||
generateSEA(outputFile, process.execPath, seaPrepBlob); | ||
|
||
spawnSyncAndAssert( | ||
outputFile, | ||
{ | ||
env: { | ||
NODE_DEBUG_NATIVE: 'SEA', | ||
...process.env, | ||
} | ||
}, | ||
{ | ||
trim: true, | ||
stdout: 'Hello from Worker' | ||
} | ||
); | ||
} |
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.
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.
Can we add a check that
env->snapshot_deserialize_main().IsEmpty()
when it is not the main thread?IIUC this relies on the trick thatEdit: the worker's main context usescontext->GetDataFromSnapshotOnce
will only return the data once and the second time it will return empty, as the main thread and worker threads are using the same snapshot data.SnapshotData::kNodeBaseContextIndex
which has nosnapshot_deserialize_main
. But I think it is still worth adding the check to ensuresnapshot_deserialize_main
is not set on worker threads, for the lines below:node/src/node.cc
Lines 344 to 346 in fe12b01
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.
hmm yeah, we still haven't figured out how it should really work for workers - we'll likely need some restructuring of the snapshot blob/API. It currently goes like:
All these share one isolate context.
It's still possible to support custom worker context snapshot (in which case the main context is the "root" worker context) + populate more stuff in built-in worker context snapshot, but it will probably need some restructuring. For a fuller built-in worker context snapshot we'll need to run an additional CreateEnvironment() call on the same isolate but with different flags and do this
node/src/env.h
Line 568 in a243225