Skip to content

Fix memory leak when creating debug instances #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();
exports.destroy = (() => {
let warned = false;

return () => {
if (!warned) {
warned = true;
console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
}
};
})();

/**
* Colors.
Expand Down
43 changes: 20 additions & 23 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ function setup(env) {
createDebug.enable = enable;
createDebug.enabled = enabled;
createDebug.humanize = require('ms');
createDebug.destroy = destroy;

Object.keys(env).forEach(key => {
createDebug[key] = env[key];
});

/**
* Active `debug` instances.
*/
createDebug.instances = [];

/**
* The currently active debug mode names, and names to skip.
*/
Expand Down Expand Up @@ -63,6 +59,7 @@ function setup(env) {
*/
function createDebug(namespace) {
let prevTime;
let enableOverride = null;

function debug(...args) {
// Disabled?
Expand Down Expand Up @@ -115,31 +112,28 @@ function setup(env) {
}

debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = createDebug.selectColor(namespace);
debug.destroy = destroy;
debug.extend = extend;
debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.

Object.defineProperty(debug, 'enabled', {
enumerable: true,
configurable: false,
get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride,
set: v => {
enableOverride = v;
}
});

// Env-specific initialization logic for debug instances
if (typeof createDebug.init === 'function') {
createDebug.init(debug);
}

createDebug.instances.push(debug);

return debug;
}

function destroy() {
const index = createDebug.instances.indexOf(this);
if (index !== -1) {
createDebug.instances.splice(index, 1);
return true;
}
return false;
}

function extend(namespace, delimiter) {
const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
newDebug.log = this.log;
Expand Down Expand Up @@ -177,11 +171,6 @@ function setup(env) {
createDebug.names.push(new RegExp('^' + namespaces + '$'));
}
}

for (i = 0; i < createDebug.instances.length; i++) {
const instance = createDebug.instances[i];
instance.enabled = createDebug.enabled(instance.namespace);
}
}

/**
Expand Down Expand Up @@ -256,6 +245,14 @@ function setup(env) {
return val;
}

/**
* XXX DO NOT USE. This is a temporary stub function.
* XXX It WILL be removed in the next major release.
*/
function destroy() {
console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
}

createDebug.enable(createDebug.load());

return createDebug;
Expand Down
4 changes: 4 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.destroy = util.deprecate(
() => {},
'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
);

/**
* Colors.
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,24 @@ describe('debug', () => {
assert.deepStrictEqual(oldNames.map(String), debug.names.map(String));
assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String));
});

it('handles re-enabling existing instances', () => {
debug.disable('*');
const inst = debug('foo');
const messages = [];
inst.log = msg => messages.push(msg.replace(/^[^@]*@([^@]+)@.*$/, '$1'));

inst('@test@');
assert.deepStrictEqual(messages, []);
debug.enable('foo');
assert.deepStrictEqual(messages, []);
inst('@test2@');
assert.deepStrictEqual(messages, ['test2']);
inst('@test3@');
assert.deepStrictEqual(messages, ['test2', 'test3']);
debug.disable('*');
inst('@test4@');
assert.deepStrictEqual(messages, ['test2', 'test3']);
});
});
});