Skip to content

Commit 7b2ca4c

Browse files
committed
lib: cleanup stdio after snapshot building
1 parent 5c4aee8 commit 7b2ca4c

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

lib/internal/bootstrap/pre_execution.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const { Buffer } = require('buffer');
2020
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
2121
const assert = require('internal/assert');
2222

23-
function prepareMainThreadExecution(expandArgv1 = false) {
23+
function prepareMainThreadExecution(expandArgv1 = false, initialzeModules = true) {
2424
refreshRuntimeOptions();
2525

2626
// TODO(joyeecheung): this is also necessary for workers when they deserialize
@@ -73,6 +73,9 @@ function prepareMainThreadExecution(expandArgv1 = false) {
7373
initializeCJSLoader();
7474
initializeESMLoader();
7575

76+
if (!initialzeModules) {
77+
return;
78+
}
7679
const CJSLoader = require('internal/modules/cjs/loader');
7780
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
7881
loadPreloadModules();

lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,51 @@ let stdin;
117117
let stdout;
118118
let stderr;
119119

120+
let stdoutDestroy;
121+
let stderrDestroy;
122+
123+
function refreshStdoutOnSigWinch() {
124+
stdout._refreshSize();
125+
}
126+
127+
function refreshStderrOnSigWinch() {
128+
stderr._refreshSize();
129+
}
130+
120131
function getStdout() {
121132
if (stdout) return stdout;
122133
stdout = createWritableStdioStream(1);
123134
stdout.destroySoon = stdout.destroy;
124135
// Override _destroy so that the fd is never actually closed.
136+
stdoutDestroy = stdout._destroy;
125137
stdout._destroy = dummyDestroy;
126138
if (stdout.isTTY) {
127-
process.on('SIGWINCH', () => stdout._refreshSize());
139+
process.on('SIGWINCH', refreshStdoutOnSigWinch);
128140
}
141+
142+
internalBinding('mksnapshot').cleanups.push(function cleanupStdout() {
143+
stdout._destroy = stdoutDestroy;
144+
stdout.destroy();
145+
process.removeListener('SIGWINCH', refreshStdoutOnSigWinch);
146+
});
129147
return stdout;
130148
}
131149

132150
function getStderr() {
133151
if (stderr) return stderr;
134152
stderr = createWritableStdioStream(2);
135153
stderr.destroySoon = stderr.destroy;
154+
stderrDestroy = stderr._destroy;
136155
// Override _destroy so that the fd is never actually closed.
137156
stderr._destroy = dummyDestroy;
138157
if (stderr.isTTY) {
139-
process.on('SIGWINCH', () => stderr._refreshSize());
158+
process.on('SIGWINCH', refreshStderrOnSigWinch);
140159
}
160+
internalBinding('mksnapshot').cleanups.push(function cleanupStderr() {
161+
stderr._destroy = stderrDestroy;
162+
stderr.destroy();
163+
process.removeListener('SIGWINCH', refreshStderrOnSigWinch);
164+
});
141165
return stderr;
142166
}
143167

@@ -224,6 +248,9 @@ function getStdin() {
224248
}
225249
}
226250

251+
internalBinding('mksnapshot').cleanups.push(function cleanupStdin() {
252+
stdin.destroy();
253+
});
227254
return stdin;
228255
}
229256

lib/internal/main/mksnapshot.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ const {
55
SafeArrayIterator
66
} = primordials;
77

8+
const binding = internalBinding('mksnapshot');
89
const {
9-
compileSnapshotMain
10-
} = internalBinding('mksnapshot');
10+
compileSnapshotMain,
11+
} = binding;
1112

1213
const {
1314
getOptionValue
@@ -101,7 +102,14 @@ function requireForUserSnapshot(id) {
101102
return require(id);
102103
}
103104

104-
// prepareMainThreadExecution(true);
105+
require('internal/bootstrap/pre_execution').prepareMainThreadExecution(true, false);
106+
107+
process.on('beforeExit', function() {
108+
for (const cleanup of binding.cleanups) {
109+
cleanup();
110+
}
111+
binding.cleanups = undefined;
112+
});
105113

106114
const mainFile = readFileSync(getOptionValue('--snapshot-main'), 'utf-8');
107115

src/node_snapshotable.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,13 @@ static void Initialize(Local<Object> target,
818818
Local<Context> context,
819819
void* priv) {
820820
Environment* env = Environment::GetCurrent(context);
821+
Isolate* isolate = context->GetIsolate();
821822
env->SetMethod(target, "compileSnapshotMain", CompileSnapshotMain);
823+
target
824+
->Set(context,
825+
FIXED_ONE_BYTE_STRING(isolate, "cleanups"),
826+
v8::Array::New(isolate))
827+
.Check();
822828
}
823829

824830
static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {

0 commit comments

Comments
 (0)