Skip to content
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

lib: account for cwd access from snapshot serialization cb #51901

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ get serialized into a snapshot and exit. This can be used to release
resources that should not or cannot be serialized or to convert user data
into a form more suitable for serialization.

Callbacks are run in the order in which they are added.

### `v8.startupSnapshot.addDeserializeCallback(callback[, data])`

<!-- YAML
Expand All @@ -1040,6 +1042,8 @@ serialized into the snapshot, they can be used to re-initialize the state
of the application or to re-acquire resources that the application needs
when the application is restarted from the snapshot.

Callbacks are run in the order in which they are added.

### `v8.startupSnapshot.setDeserializeMainFunction(callback[, data])`

<!-- YAML
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/bootstrap/switches/does_own_process_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const credentials = internalBinding('credentials');
const rawMethods = internalBinding('process_methods');
const {
namespace: {
addDeserializeCallback,
addSerializeCallback,
isBuildingSnapshot,
},
Expand Down Expand Up @@ -114,8 +115,13 @@ function wrapPosixCredentialSetters(credentials) {
let cachedCwd = '';

if (isBuildingSnapshot()) {
// Reset the cwd on both serialization and deserialization so it's fine
// for process.cwd() to be accessed inside of serialization callbacks.
addSerializeCallback(() => {
cachedCwd = '';
addDeserializeCallback(() => {
cachedCwd = '';
});
});
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/snapshot/cwd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const {
addSerializeCallback,
setDeserializeMainFunction,
} = require('v8').startupSnapshot;

// To make sure the cwd is present in the cache
process.cwd();

// Also access it from a serialization callback once
addSerializeCallback(() => {
process.cwd();
});

setDeserializeMainFunction(() => {
console.log(process.cwd());
});
Loading