From ae46f454877677e26e09b2793d8581556ab72650 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 6 Oct 2019 17:57:45 -0400 Subject: [PATCH] stop bootstrap buffer as soon as usercode runs --- lib/internal/async_hooks.js | 18 +++++++++++------- lib/internal/modules/esm/module_job.js | 8 +++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 0d9de0bfbdc087..f64cbe5f031fb9 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -289,8 +289,8 @@ function bufferBootstrapHooks() { } } -function clearBootstrapHooksBuffer() { - if (!bootstrapBuffer) +function stopBootstrapHooksBuffer() { + if (!bootstrapHooks) return; async_hook_fields[kInit]--; async_hook_fields[kBefore]--; @@ -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; } @@ -331,6 +333,7 @@ function emitBootstrapHooksBuffer() { break; } } + bootstrapBuffer = null; } let wantPromiseHook = false; @@ -573,5 +576,6 @@ module.exports = { }, bufferBootstrapHooks, clearBootstrapHooksBuffer, - emitBootstrapHooksBuffer + emitBootstrapHooksBuffer, + stopBootstrapHooksBuffer }; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index a4c2ea658d9cf6..1bdb4c0d28f2c9 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -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() {} @@ -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; } }