Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a6f0cfa

Browse filesBrowse files
joyeecheungaduh95
authored andcommitted
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 <anna@addaleax.net> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent c2fb38c commit a6f0cfa
Copy full SHA for a6f0cfa

File tree

Expand file treeCollapse file tree

2 files changed

+85
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+85
-1
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
320320
CHECK(!env->isolate_data()->is_building_snapshot());
321321

322322
#ifndef DISABLE_SINGLE_EXECUTABLE_APPLICATION
323-
if (sea::IsSingleExecutable()) {
323+
// Snapshot in SEA is only loaded for the main thread.
324+
if (sea::IsSingleExecutable() && env->is_main_thread()) {
324325
sea::SeaResource sea = sea::FindSingleExecutableResource();
325326
// The SEA preparation blob building process should already enforce this,
326327
// this check is just here to guard against the unlikely case where
@@ -342,6 +343,9 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
342343
// move the pre-execution part into a different file that can be
343344
// reused when dealing with user-defined main functions.
344345
if (!env->snapshot_deserialize_main().IsEmpty()) {
346+
// Custom worker snapshot is not supported yet,
347+
// so workers can't have deserialize main functions.
348+
CHECK(env->is_main_thread());
345349
return env->RunSnapshotDeserializeMain();
346350
}
347351

Collapse file
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const {
6+
generateSEA,
7+
skipIfSingleExecutableIsNotSupported,
8+
} = require('../common/sea');
9+
10+
skipIfSingleExecutableIsNotSupported();
11+
12+
// This tests the snapshot support in single executable applications.
13+
14+
const tmpdir = require('../common/tmpdir');
15+
const { writeFileSync, existsSync } = require('fs');
16+
const {
17+
spawnSyncAndAssert, spawnSyncAndExitWithoutError,
18+
} = require('../common/child_process');
19+
const assert = require('assert');
20+
21+
const configFile = tmpdir.resolve('sea-config.json');
22+
const seaPrepBlob = tmpdir.resolve('sea-prep.blob');
23+
const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'sea');
24+
25+
{
26+
tmpdir.refresh();
27+
28+
// FIXME(joyeecheung): currently `worker_threads` cannot be loaded during the
29+
// snapshot building process because internal/worker.js is accessing isMainThread at
30+
// the top level (and there are maybe more code that access these at the top-level),
31+
// and have to be loaded in the deserialized snapshot main function.
32+
// Change these states to be accessed on-demand.
33+
const code = `
34+
const {
35+
setDeserializeMainFunction,
36+
} = require('v8').startupSnapshot;
37+
setDeserializeMainFunction(() => {
38+
const { Worker } = require('worker_threads');
39+
new Worker("console.log('Hello from Worker')", { eval: true });
40+
});
41+
`;
42+
43+
writeFileSync(tmpdir.resolve('snapshot.js'), code, 'utf-8');
44+
writeFileSync(configFile, `
45+
{
46+
"main": "snapshot.js",
47+
"output": "sea-prep.blob",
48+
"useSnapshot": true
49+
}
50+
`);
51+
52+
spawnSyncAndExitWithoutError(
53+
process.execPath,
54+
['--experimental-sea-config', 'sea-config.json'],
55+
{
56+
cwd: tmpdir.path,
57+
env: {
58+
NODE_DEBUG_NATIVE: 'SEA',
59+
...process.env,
60+
},
61+
});
62+
63+
assert(existsSync(seaPrepBlob));
64+
65+
generateSEA(outputFile, process.execPath, seaPrepBlob);
66+
67+
spawnSyncAndAssert(
68+
outputFile,
69+
{
70+
env: {
71+
NODE_DEBUG_NATIVE: 'SEA',
72+
...process.env,
73+
}
74+
},
75+
{
76+
trim: true,
77+
stdout: 'Hello from Worker'
78+
}
79+
);
80+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.