Skip to content

Commit d253080

Browse files
joyeecheungCeres6
authored andcommitted
bootstrap: throw ERR_NOT_SUPPORTED_IN_SNAPSHOT in unsupported operation
This patch adds a new ERR_NOT_SUPPORTED_IN_SNAPSHOT error and throw it in the worker constructor. PR-URL: nodejs#47887 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 19be632 commit d253080

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,13 @@ error indicates that the idle loop has failed to stop.
24202420
An attempt was made to use operations that can only be used when building
24212421
V8 startup snapshot even though Node.js isn't building one.
24222422

2423+
<a id="ERR_NOT_SUPPORTED_IN_SNAPSHOT"></a>
2424+
2425+
### `ERR_NOT_SUPPORTED_IN_SNAPSHOT`
2426+
2427+
An attempt was made to perform operations that are not supported when
2428+
building a startup snapshot.
2429+
24232430
<a id="ERR_NO_CRYPTO"></a>
24242431

24252432
### `ERR_NO_CRYPTO`

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,8 @@ E('ERR_NETWORK_IMPORT_DISALLOWED',
14691469
"import of '%s' by %s is not supported: %s", Error);
14701470
E('ERR_NOT_BUILDING_SNAPSHOT',
14711471
'Operation cannot be invoked when not building startup snapshot', Error);
1472+
E('ERR_NOT_SUPPORTED_IN_SNAPSHOT',
1473+
'%s is not supported in startup snapshot', Error);
14721474
E('ERR_NO_CRYPTO',
14731475
'Node.js is not compiled with OpenSSL crypto support', Error);
14741476
E('ERR_NO_ICU',

lib/internal/v8/startup_snapshot.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
const {
77
codes: {
88
ERR_NOT_BUILDING_SNAPSHOT,
9+
ERR_NOT_SUPPORTED_IN_SNAPSHOT,
910
ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION,
1011
},
1112
} = require('internal/errors');
@@ -14,7 +15,7 @@ const {
1415
setSerializeCallback,
1516
setDeserializeCallback,
1617
setDeserializeMainFunction: _setDeserializeMainFunction,
17-
isBuildingSnapshotBuffer
18+
isBuildingSnapshotBuffer,
1819
} = internalBinding('mksnapshot');
1920

2021
function isBuildingSnapshot() {
@@ -27,6 +28,12 @@ function throwIfNotBuildingSnapshot() {
2728
}
2829
}
2930

31+
function throwIfBuildingSnapshot(reason) {
32+
if (isBuildingSnapshot()) {
33+
throw new ERR_NOT_SUPPORTED_IN_SNAPSHOT(reason);
34+
}
35+
}
36+
3037
const deserializeCallbacks = [];
3138
let deserializeCallbackIsSet = false;
3239
function runDeserializeCallbacks() {
@@ -102,6 +109,7 @@ function setDeserializeMainFunction(callback, data) {
102109
module.exports = {
103110
initializeCallbacks,
104111
runDeserializeCallbacks,
112+
throwIfBuildingSnapshot,
105113
// Exposed to require('v8').startupSnapshot
106114
namespace: {
107115
addDeserializeCallback,

lib/internal/worker.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ const { deserializeError } = require('internal/error_serdes');
6060
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
6161
const { kEmptyObject } = require('internal/util');
6262
const { validateArray, validateString } = require('internal/validators');
63-
63+
const {
64+
throwIfBuildingSnapshot,
65+
} = require('internal/v8/startup_snapshot');
6466
const {
6567
ownsProcessState,
6668
isMainThread,
@@ -129,6 +131,7 @@ function assignEnvironmentData(data) {
129131

130132
class Worker extends EventEmitter {
131133
constructor(filename, options = kEmptyObject) {
134+
throwIfBuildingSnapshot('Creating workers');
132135
super();
133136
const isInternal = arguments[2] === kIsInternal;
134137
debug(

test/fixtures/snapshot/worker.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const { Worker } = require('worker_threads');
4+
5+
new Worker('1', { eval: true });

test/parallel/test-snapshot-worker.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// This tests snapshot JS API using the example in the docs.
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const { spawnSync } = require('child_process');
8+
const tmpdir = require('../common/tmpdir');
9+
const fixtures = require('../common/fixtures');
10+
const path = require('path');
11+
const fs = require('fs');
12+
13+
tmpdir.refresh();
14+
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
15+
const entry = fixtures.path('snapshot', 'worker.js');
16+
{
17+
const child = spawnSync(process.execPath, [
18+
'--snapshot-blob',
19+
blobPath,
20+
'--build-snapshot',
21+
entry,
22+
], {
23+
cwd: tmpdir.path
24+
});
25+
const stderr = child.stderr.toString();
26+
assert.match(
27+
stderr,
28+
/Error: Creating workers is not supported in startup snapshot/);
29+
assert.match(
30+
stderr,
31+
/ERR_NOT_SUPPORTED_IN_SNAPSHOT/);
32+
assert.strictEqual(child.status, 1);
33+
assert(!fs.existsSync(blobPath));
34+
}

0 commit comments

Comments
 (0)