chrisa / node-dtrace-provider

Native DTrace probes for node.js apps
Other
320 stars 68 forks source link

Provider objects can be garbage-collected while Probes still exist #91

Closed arekinath closed 7 years ago

arekinath commented 7 years ago

Currently because the Provider object (the return value of createDTraceProvider) is not referenced (in Javascript land) by Probe objects (returned from .addProbe()), it is possible for the Provider to be GC'd while Probes are still referred to and valid.

For example:

var mod_dtrace = require('dtrace-provider');

var prov = mod_dtrace.createDTraceProvider('testusdt');
var p1 = prov.addProbe('foo', 'int');
prov.enable();

var n = 0;
setInterval(function () {
    p1.fire(function () { return ([n++]); });
}, 1000);
setTimeout(function () { process.exit(0); }, 30000);

Because the closure inside setInterval only holds a reference to p1 and not prov (since it didn't use prov), V8 will sometimes garbage-collect prov. At this point the finalizer for the provider runs and disables all the probes.

This means that sometimes when you run the code above, the dtrace probes will silently fail to fire without any error being produced.

I think that the Probe objects should store a reference to their Provider in their Javascript objects (as an internal-use property). This way the entire bundle of Provider + Probe objects has to be unreferenced before GC will collect any of them and cause finalizers to be called.

arekinath commented 7 years ago

Dupe of #90, sorry