From 2e4ceb5747b0e5d1bccd79837de7731c4030dc48 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 17 Apr 2019 23:45:53 +0800 Subject: [PATCH] util: access process states lazily in debuglog `debuglog()` depends on `process.pid` and `process.env.NODE_DEBUG`, so it needs to be called lazily in top scopes of internal modules that may be loaded before these run time states are allowed to be accessed. This patch makes its implementation lazy by default, the process states are only accessed when the returned debug function is called for the first time. PR-URL: https://github.com/nodejs/node/pull/27281 Reviewed-By: Ruben Bridgewater Reviewed-By: Luigi Pinca Reviewed-By: Richard Lau Reviewed-By: Yongsheng Zhang --- lib/_stream_readable.js | 9 +-------- lib/internal/main/worker_thread.js | 3 +-- lib/internal/modules/cjs/loader.js | 9 +-------- lib/internal/stream_base_commons.js | 2 +- lib/internal/timers.js | 8 +------- lib/internal/util/debuglog.js | 18 +++++++++++++++++- lib/internal/worker.js | 9 +-------- lib/internal/worker/io.js | 9 +-------- lib/timers.js | 9 +-------- 9 files changed, 25 insertions(+), 51 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 1b6f2175ceb..34118f4fbee 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -30,14 +30,7 @@ const EE = require('events'); const Stream = require('stream'); const { Buffer } = require('buffer'); -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('stream'); - } - debuglog(...args); -} - +const debug = require('internal/util/debuglog').debuglog('stream'); const BufferList = require('internal/streams/buffer_list'); const destroyImpl = require('internal/streams/destroy'); const { getHighWaterMark } = require('internal/streams/state'); diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index a06fda7a59c..dd6a9426fff 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -44,6 +44,7 @@ const { } = require('internal/process/execution'); const publicWorker = require('worker_threads'); +const debug = require('internal/util/debuglog').debuglog('worker'); const assert = require('internal/assert'); @@ -51,8 +52,6 @@ patchProcessObject(); setupInspectorHooks(); setupDebugEnv(); -const debug = require('internal/util/debuglog').debuglog('worker'); - setupWarningHandler(); // Since worker threads cannot switch cwd, we do not need to diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 16940aa0c29..20ca6d113c0 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -166,14 +166,7 @@ Object.defineProperty(Module, 'wrapper', { } }); -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('module'); - } - debuglog(...args); -} - +const debug = require('internal/util/debuglog').debuglog('module'); Module._debug = deprecate(debug, 'Module._debug is deprecated.', 'DEP0077'); // Given a module name, and a list of paths to test, returns the first diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index 50eac427723..88896083f19 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -31,7 +31,7 @@ const kAfterAsyncWrite = Symbol('kAfterAsyncWrite'); const kHandle = Symbol('kHandle'); const kSession = Symbol('kSession'); -const debug = require('util').debuglog('stream'); +const debug = require('internal/util/debuglog').debuglog('stream'); function handleWriteReq(req, data, encoding) { const { handle } = req; diff --git a/lib/internal/timers.js b/lib/internal/timers.js index aef099b4b99..566d7df0365 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -107,13 +107,7 @@ const L = require('internal/linkedlist'); const PriorityQueue = require('internal/priority_queue'); const { inspect } = require('internal/util/inspect'); -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('timer'); - } - debuglog(...args); -} +const debug = require('internal/util/debuglog').debuglog('timer'); // *Must* match Environment::ImmediateInfo::Fields in src/env.h. const kCount = 0; diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js index e92f3ad2bd5..11de25b6b43 100644 --- a/lib/internal/util/debuglog.js +++ b/lib/internal/util/debuglog.js @@ -31,7 +31,7 @@ function emitWarningIfNeeded(set) { } } -function debuglog(set) { +function debuglogImpl(set) { set = set.toUpperCase(); if (!debugs[set]) { if (debugEnvRegex.test(set)) { @@ -48,6 +48,22 @@ function debuglog(set) { return debugs[set]; } +// debuglogImpl depends on process.pid and process.env.NODE_DEBUG, +// so it needs to be called lazily in top scopes of internal modules +// that may be loaded before these run time states are allowed to +// be accessed. +function debuglog(set) { + let debug; + return function(...args) { + if (!debug) { + // Only invokes debuglogImpl() when the debug function is + // called for the first time. + debug = debuglogImpl(set); + } + debug(...args); + }; +} + module.exports = { debuglog, initializeDebugEnv diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 796d1713dd5..e9d961f35a0 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -48,14 +48,7 @@ const kOnErrorMessage = Symbol('kOnErrorMessage'); const kParentSideStdio = Symbol('kParentSideStdio'); const SHARE_ENV = Symbol.for('nodejs.worker_threads.SHARE_ENV'); - -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('worker'); - } - debuglog(...args); -} +const debug = require('internal/util/debuglog').debuglog('worker'); class Worker extends EventEmitter { constructor(filename, options = {}) { diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 04f8f9ad597..8b7aedd39d0 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -21,14 +21,7 @@ const { const { Readable, Writable } = require('stream'); const EventEmitter = require('events'); const { inspect } = require('internal/util/inspect'); - -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('worker'); - } - debuglog(...args); -} +const debug = require('internal/util/debuglog').debuglog('worker'); const kIncrementsPortRef = Symbol('kIncrementsPortRef'); const kName = Symbol('kName'); diff --git a/lib/timers.js b/lib/timers.js index 85801face55..ddce43e7491 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -50,14 +50,7 @@ const { deprecate } = require('internal/util'); const { ERR_INVALID_CALLBACK } = require('internal/errors').codes; - -let debuglog; -function debug(...args) { - if (!debuglog) { - debuglog = require('internal/util/debuglog').debuglog('timer'); - } - debuglog(...args); -} +const debug = require('internal/util/debuglog').debuglog('timer'); const { destroyHooksExist,