TooTallNate/node-weak

Node process crashes unexpectedly when referencing a weak variable inside of its GC callback

Opened this issue · 0 comments

Issue

The following script crashes when the garbage collector is run, without throwing an exception.
(run with --expose-gc)

const weak = require('weak');

// Never called
process.on('uncaughtException', (ex) => {
	console.error("Uncaught exception: ", ex);
});

// Keep alive
setInterval(() => null, 1000);

function foo() { this.bar = 5 };

var strongFoo = new foo();
var weakFoo = weak(strongFoo, () => {
	console.log("Foo is rip: ", weakFoo);
});

console.log("weakFoo before GC: ", weakFoo);
strongFoo = null;
global.gc();
console.log("weakFoo after GC: ", weakFoo);

results in the following when running node wrapped using nodemon:

weakFoo before GC:  foo { bar: 5 }
[nodemon] app crashed - waiting for file changes before starting...

running with node directly immediately exits after the before GC statement is printed.
Node does not crash during the after GC statement if you bypass the callback by removing the reference to 'weakFoo'.

Expected behavior.

Node does not crash unexpectedly, or throws an exception when an unsafe reference to a weak variable is made inside of the variables' callback.