Skip to content

Commit 40565e9

Browse files
authored
lib: account for cwd access from snapshot serialization cb
Functions registered with `addSerializeCallback()` can access and call `process.cwd()`. b7d836e accounted for the fact that it is necessary to reset the cwd cache after the snapshot builder script has run, but did not account for possible accesses from serialization callbacks. To properly account for these, add a deserialization callback as well. As a related drive-by fix, also mention the execution order of callbacks in the documentation. Refs: #49684 PR-URL: #51901 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent f63e8b7 commit 40565e9

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/api/v8.md

+4
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ get serialized into a snapshot and exit. This can be used to release
10211021
resources that should not or cannot be serialized or to convert user data
10221022
into a form more suitable for serialization.
10231023

1024+
Callbacks are run in the order in which they are added.
1025+
10241026
### `v8.startupSnapshot.addDeserializeCallback(callback[, data])`
10251027

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

1045+
Callbacks are run in the order in which they are added.
1046+
10431047
### `v8.startupSnapshot.setDeserializeMainFunction(callback[, data])`
10441048

10451049
<!-- YAML

lib/internal/bootstrap/switches/does_own_process_state.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const credentials = internalBinding('credentials');
44
const rawMethods = internalBinding('process_methods');
55
const {
66
namespace: {
7+
addDeserializeCallback,
78
addSerializeCallback,
89
isBuildingSnapshot,
910
},
@@ -114,8 +115,13 @@ function wrapPosixCredentialSetters(credentials) {
114115
let cachedCwd = '';
115116

116117
if (isBuildingSnapshot()) {
118+
// Reset the cwd on both serialization and deserialization so it's fine
119+
// for process.cwd() to be accessed inside of serialization callbacks.
117120
addSerializeCallback(() => {
118121
cachedCwd = '';
122+
addDeserializeCallback(() => {
123+
cachedCwd = '';
124+
});
119125
});
120126
}
121127

test/fixtures/snapshot/cwd.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const {
2+
addSerializeCallback,
23
setDeserializeMainFunction,
34
} = require('v8').startupSnapshot;
45

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

9+
// Also access it from a serialization callback once
10+
addSerializeCallback(() => {
11+
process.cwd();
12+
});
13+
814
setDeserializeMainFunction(() => {
915
console.log(process.cwd());
1016
});

0 commit comments

Comments
 (0)