Skip to content

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

@ghost

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions