-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sea: only assert snapshot main function for main threads
Snapshot main functions are only loaded for main threads in single executable applications. Update the check to avoid asserting it in worker threads - this allows worker threads to be spawned in snapshot main functions bundled into a single executable application. PR-URL: #56120 Fixes: #56077 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
- Loading branch information
1 parent
c2fb38c
commit a6f0cfa
Showing
2 changed files
with
85 additions
and
1 deletion.
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' | ||
} | ||
); | ||
} |