Skip to content

Commit

Permalink
bootstrap: throw ERR_NOT_SUPPORTED_IN_SNAPSHOT in unsupported operation
Browse files Browse the repository at this point in the history
This patch adds a new ERR_NOT_SUPPORTED_IN_SNAPSHOT error and throw
it in the worker constructor.

PR-URL: #47887
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed May 30, 2023
1 parent c480559 commit 3e6e3ab
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2420,6 +2420,13 @@ error indicates that the idle loop has failed to stop.
An attempt was made to use operations that can only be used when building
V8 startup snapshot even though Node.js isn't building one.

<a id="ERR_NOT_SUPPORTED_IN_SNAPSHOT"></a>

### `ERR_NOT_SUPPORTED_IN_SNAPSHOT`

An attempt was made to perform operations that are not supported when
building a startup snapshot.

<a id="ERR_NO_CRYPTO"></a>

### `ERR_NO_CRYPTO`
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,8 @@ E('ERR_NETWORK_IMPORT_DISALLOWED',
"import of '%s' by %s is not supported: %s", Error);
E('ERR_NOT_BUILDING_SNAPSHOT',
'Operation cannot be invoked when not building startup snapshot', Error);
E('ERR_NOT_SUPPORTED_IN_SNAPSHOT',
'%s is not supported in startup snapshot', Error);
E('ERR_NO_CRYPTO',
'Node.js is not compiled with OpenSSL crypto support', Error);
E('ERR_NO_ICU',
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/v8/startup_snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
const {
codes: {
ERR_NOT_BUILDING_SNAPSHOT,
ERR_NOT_SUPPORTED_IN_SNAPSHOT,
ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION,
},
} = require('internal/errors');
Expand All @@ -14,7 +15,7 @@ const {
setSerializeCallback,
setDeserializeCallback,
setDeserializeMainFunction: _setDeserializeMainFunction,
isBuildingSnapshotBuffer
isBuildingSnapshotBuffer,
} = internalBinding('mksnapshot');

function isBuildingSnapshot() {
Expand All @@ -27,6 +28,12 @@ function throwIfNotBuildingSnapshot() {
}
}

function throwIfBuildingSnapshot(reason) {
if (isBuildingSnapshot()) {
throw new ERR_NOT_SUPPORTED_IN_SNAPSHOT(reason);
}
}

const deserializeCallbacks = [];
let deserializeCallbackIsSet = false;
function runDeserializeCallbacks() {
Expand Down Expand Up @@ -102,6 +109,7 @@ function setDeserializeMainFunction(callback, data) {
module.exports = {
initializeCallbacks,
runDeserializeCallbacks,
throwIfBuildingSnapshot,
// Exposed to require('v8').startupSnapshot
namespace: {
addDeserializeCallback,
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ const { deserializeError } = require('internal/error_serdes');
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const { validateArray, validateString } = require('internal/validators');

const {
throwIfBuildingSnapshot,
} = require('internal/v8/startup_snapshot');
const {
ownsProcessState,
isMainThread,
Expand Down Expand Up @@ -129,6 +131,7 @@ function assignEnvironmentData(data) {

class Worker extends EventEmitter {
constructor(filename, options = kEmptyObject) {
throwIfBuildingSnapshot('Creating workers');
super();
const isInternal = arguments[2] === kIsInternal;
debug(
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/snapshot/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const { Worker } = require('worker_threads');

new Worker('1', { eval: true });
34 changes: 34 additions & 0 deletions test/parallel/test-snapshot-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

// This tests snapshot JS API using the example in the docs.

require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');

tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
const entry = fixtures.path('snapshot', 'worker.js');
{
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
entry,
], {
cwd: tmpdir.path
});
const stderr = child.stderr.toString();
assert.match(
stderr,
/Error: Creating workers is not supported in startup snapshot/);
assert.match(
stderr,
/ERR_NOT_SUPPORTED_IN_SNAPSHOT/);
assert.strictEqual(child.status, 1);
assert(!fs.existsSync(blobPath));
}

0 comments on commit 3e6e3ab

Please sign in to comment.