diff --git a/lib/chain.js b/lib/chain.js index f1fc5caf..4e32d0c3 100644 --- a/lib/chain.js +++ b/lib/chain.js @@ -140,7 +140,7 @@ Chain.prototype.run = function run(req, res, done) { var handler = self._stack[index++]; // all done or request closed - if (!handler || req.connectionState() === 'close') { + if (!handler || req.connectionState() === 'close' || res.destroyed) { process.nextTick(function nextTick() { return done(err, req, res); }); diff --git a/test/server.test.js b/test/server.test.js index 5fd10806..d1e53b1a 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -3057,3 +3057,22 @@ test('should use custom function for error handling', function(t) { }); }); }); + +test('should stop the chain when response already destroyed', function(t) { + let counter = 0; + SERVER.get( + '/test', + async function test(req, res) { + counter++; + res.send(); + }, + async function test(req, res) { + counter++; + res.send(); + } + ); + CLIENT.get('/test', function() { + t.equal(counter, 1); + t.end(); + }); +});