From 3898a550c1c38d8e89b5c9efb876cf6ae72f2d53 Mon Sep 17 00:00:00 2001 From: Muhammad Noor Misyuari Date: Fri, 16 Dec 2022 19:56:26 +0700 Subject: [PATCH 1/2] fix: destroyed async/await handler --- lib/chain.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); }); From 3eb005326cb6499feac9829dcd55292091158efe Mon Sep 17 00:00:00 2001 From: Muhammad Noor Misyuari Date: Thu, 16 Feb 2023 16:56:34 +0700 Subject: [PATCH 2/2] add test --- test/server.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/server.test.js b/test/server.test.js index 291f6616..0cf1f411 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -2952,3 +2952,22 @@ test('Server correctly handles multiple clientError listeners', function(t) { }); }).end(); }); + +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(); + }); +});