restify / restify/node-restify

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

Open
#1,788 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.7k
Forks
975
Avg merge
1d 6h
Merged PRs (30d)
5

Description

  • Used appropriate template for the issue type
  • Searched both open and closed issues for duplicates of this issue
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the cpuUsageThrottle plugin and its close() path; inspect the pidusage cleanup and the plugin._timeout race described in the report. Reproduce the supplied snippet, then verify that close() releases all handles and allows the process to exit.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.