Skip to content

Commit ae46f45

Browse files
committed
stop bootstrap buffer as soon as usercode runs
1 parent ac7fdca commit ae46f45

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

lib/internal/async_hooks.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ function bufferBootstrapHooks() {
289289
}
290290
}
291291

292-
function clearBootstrapHooksBuffer() {
293-
if (!bootstrapBuffer)
292+
function stopBootstrapHooksBuffer() {
293+
if (!bootstrapHooks)
294294
return;
295295
async_hook_fields[kInit]--;
296296
async_hook_fields[kBefore]--;
@@ -299,19 +299,21 @@ function clearBootstrapHooksBuffer() {
299299
async_hook_fields[kPromiseResolve]--;
300300
async_hook_fields[kTotals] -= 5;
301301
active_hooks.array.splice(active_hooks.array.indexOf(bootstrapHooks), 1);
302+
bootstrapHooks = null;
302303
if (async_hook_fields[kTotals] === 0) {
303304
disableHooks();
304305
// Flush microtasks to ensure disable has run.
305306
process._tickCallback();
306307
}
307-
const _bootstrapBuffer = bootstrapBuffer;
308+
}
309+
310+
function clearBootstrapHooksBuffer() {
311+
if (bootstrapHooks)
312+
stopBootstrapHooksBuffer();
308313
bootstrapBuffer = null;
309-
bootstrapHooks = null;
310-
return _bootstrapBuffer;
311314
}
312315

313316
function emitBootstrapHooksBuffer() {
314-
const bootstrapBuffer = clearBootstrapHooksBuffer();
315317
if (!bootstrapBuffer || async_hook_fields[kTotals] === 0) {
316318
return;
317319
}
@@ -331,6 +333,7 @@ function emitBootstrapHooksBuffer() {
331333
break;
332334
}
333335
}
336+
bootstrapBuffer = null;
334337
}
335338

336339
let wantPromiseHook = false;
@@ -573,5 +576,6 @@ module.exports = {
573576
},
574577
bufferBootstrapHooks,
575578
clearBootstrapHooksBuffer,
576-
emitBootstrapHooksBuffer
579+
emitBootstrapHooksBuffer,
580+
stopBootstrapHooksBuffer
577581
};

lib/internal/modules/esm/module_job.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const { ModuleWrap } = internalBinding('module_wrap');
1111
const { decorateErrorStack } = require('internal/util');
1212
const { getOptionValue } = require('internal/options');
1313
const assert = require('internal/assert');
14-
const { clearBootstrapHooksBuffer } = require('internal/async_hooks');
14+
const { clearBootstrapHooksBuffer, stopBootstrapHooksBuffer } =
15+
require('internal/async_hooks');
1516
const resolvedPromise = SafePromise.resolve();
1617

1718
function noop() {}
@@ -107,13 +108,14 @@ class ModuleJob {
107108
const module = await this.instantiate();
108109
const timeout = -1;
109110
const breakOnSigint = false;
111+
if (this.isMain)
112+
stopBootstrapHooksBuffer();
110113
const output = {
111114
module,
112115
result: module.evaluate(timeout, breakOnSigint)
113116
};
114-
if (this.isMain) {
117+
if (this.isMain)
115118
clearBootstrapHooksBuffer();
116-
}
117119
return output;
118120
}
119121
}

0 commit comments

Comments
 (0)