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

Restify inflight request counter leaks when using handleUncaughtExceptions and throwing an exception after connection is aborted

Open cprussin 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

Restify Version

Proven reproducible on 8.2.0 and 7.3.0

Node.js Version

Proven reproducible on v8.15.0 and v10.15.1

Expected behaviour

Restify inflight counter should not leak

Inflight requests: 1
Inflight requests: 1
Inflight requests: 1
Inflight requests: 1
...

Actual behaviour

Restify inflight counter leaks

Inflight requests: 1
Inflight requests: 2
Inflight requests: 3
Inflight requests: 4
...

Repro case

const restify = require('restify');
const http = require('http');

const server = restify.createServer({ handleUncaughtExceptions: true });
const delay = 100;

server.pre((req, res, next) => {
    console.log(`Inflight requests: ${server.inflightRequests()}`);
    next();
});

server.get('/', (req, res, next) => {
    setTimeout(() => { throw new Error('foo'); }, delay);
});

server.on('uncaughtException', (req, res) => res.send('foo'));

server.listen(8080, () => {
    console.log('Server now up on port 8080');

    setInterval(() => {
        const req = http.get("http://localhost:8080/").on('error', () => {});
        setTimeout(() => req.abort(), delay / 2);
    }, delay);
});

Cause

Something in Restify's domain handling causes this function to never get called with res._handlersFinished === true: https://github.com/restify/node-restify/blob/f363b36b0e5176c3f134b345c057ec6199d70b83/lib/server.js#L1347

I'm still working out the precise bug here but I should be able to trace more precisely what the bug is from there and update this ticket with more details.

Are you willing and able to fix this?

Yes (but extra eyes are appreciated)

cprussin avatar Mar 26 '19 21:03 cprussin

FYI @mridgway @misterdjules @jdarren

cprussin avatar Mar 26 '19 21:03 cprussin