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 af32d43

Browse filesBrowse files
addaleaxmarco-ippolito
authored andcommitted
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 1edbc7d commit af32d43
Copy full SHA for af32d43

File tree

Expand file treeCollapse file tree

3 files changed

+16
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+16
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/v8.md‎

Copy file name to clipboardExpand all lines: doc/api/v8.md
+4Lines changed: 4 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎lib/internal/bootstrap/switches/does_own_process_state.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/switches/does_own_process_state.js
+6Lines changed: 6 additions & 0 deletions
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

Collapse file

‎test/fixtures/snapshot/cwd.js‎

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.