node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

cpuUsageThrottle plugin `close()` does not clean up properly

Open DonutEspresso opened this issue 5 years ago • 1 comments

  • [X] Used appropriate template for the issue type
  • [X] Searched both open and closed issues for duplicates of this issue
  • [X] Title adequately and concisely reflects the feature or the bug

Bug Report

When cleaning up resources on shutdown (or test teardown), calling close() on the plugin does not properly clean up all resources.

Restify Version

8.3.2

Node.js Version

All

Expected behaviour

Calling plugin.close() should allow process to exit.

Actual behaviour

close() does not clean up handles, leaving process running.

Repro case

const cpuUsageThrottle = require('restify').plugins.cpuUsageThrottle;

const plugin = cpuUsageThrottle({
    limit: 0.75,
    max: 1,
    interval: 50,
    halfLife: 200
});

plugin.close();

Cause

It seems there are two causes:

  1. pidusage.clear() needs to be called, as pidusage itself attaches a setInterval on the process.
  2. There is race condition where plugin.close() can be called before pidusage()'s first invocation can return. In that scenario, plugin._timeout is null as it has never been initialized, and therefore the re-looping setTimeout never gets unregistered.

DonutEspresso avatar Jun 04 '19 00:06 DonutEspresso

Hi, @DonutEspresso I'm curious about the issue you posted here. However, I'm new to restify. I wonder where pidusage() is invoked as you mentioned. Thx.

ChangXiaoning avatar Aug 29 '20 12:08 ChangXiaoning