Skip to content

Commit

Permalink
stop bootstrap buffer as soon as usercode runs
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 6, 2019
1 parent ac7fdca commit ae46f45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 11 additions & 7 deletions lib/internal/async_hooks.js
Expand Up @@ -289,8 +289,8 @@ function bufferBootstrapHooks() {
}
}

function clearBootstrapHooksBuffer() {
if (!bootstrapBuffer)
function stopBootstrapHooksBuffer() {
if (!bootstrapHooks)
return;
async_hook_fields[kInit]--;
async_hook_fields[kBefore]--;
Expand All @@ -299,19 +299,21 @@ function clearBootstrapHooksBuffer() {
async_hook_fields[kPromiseResolve]--;
async_hook_fields[kTotals] -= 5;
active_hooks.array.splice(active_hooks.array.indexOf(bootstrapHooks), 1);
bootstrapHooks = null;
if (async_hook_fields[kTotals] === 0) {
disableHooks();
// Flush microtasks to ensure disable has run.
process._tickCallback();
}
const _bootstrapBuffer = bootstrapBuffer;
}

function clearBootstrapHooksBuffer() {
if (bootstrapHooks)
stopBootstrapHooksBuffer();
bootstrapBuffer = null;
bootstrapHooks = null;
return _bootstrapBuffer;
}

function emitBootstrapHooksBuffer() {
const bootstrapBuffer = clearBootstrapHooksBuffer();
if (!bootstrapBuffer || async_hook_fields[kTotals] === 0) {
return;
}
Expand All @@ -331,6 +333,7 @@ function emitBootstrapHooksBuffer() {
break;
}
}
bootstrapBuffer = null;
}

let wantPromiseHook = false;
Expand Down Expand Up @@ -573,5 +576,6 @@ module.exports = {
},
bufferBootstrapHooks,
clearBootstrapHooksBuffer,
emitBootstrapHooksBuffer
emitBootstrapHooksBuffer,
stopBootstrapHooksBuffer
};
8 changes: 5 additions & 3 deletions lib/internal/modules/esm/module_job.js
Expand Up @@ -11,7 +11,8 @@ const { ModuleWrap } = internalBinding('module_wrap');
const { decorateErrorStack } = require('internal/util');
const { getOptionValue } = require('internal/options');
const assert = require('internal/assert');
const { clearBootstrapHooksBuffer } = require('internal/async_hooks');
const { clearBootstrapHooksBuffer, stopBootstrapHooksBuffer } =
require('internal/async_hooks');
const resolvedPromise = SafePromise.resolve();

function noop() {}
Expand Down Expand Up @@ -107,13 +108,14 @@ class ModuleJob {
const module = await this.instantiate();
const timeout = -1;
const breakOnSigint = false;
if (this.isMain)
stopBootstrapHooksBuffer();
const output = {
module,
result: module.evaluate(timeout, breakOnSigint)
};
if (this.isMain) {
if (this.isMain)
clearBootstrapHooksBuffer();
}
return output;
}
}
Expand Down

0 comments on commit ae46f45

Please sign in to comment.