http: emit error on aborted ServerResponse#63448
Conversation
Emit an error when a ServerResponse is closed after end() but before the finish event is emitted. Fixes: nodejs#50656 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5
|
Review requested:
|
Expect ECONNRESET when an HTTP response pipeline is closed before the response finish event is emitted.
Update HTTP tests that intentionally close the client connection to expect ECONNRESET from ServerResponse.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #63448 +/- ##
==========================================
- Coverage 90.06% 90.03% -0.03%
==========================================
Files 714 714
Lines 225918 225930 +12
Branches 42734 42735 +1
==========================================
- Hits 203464 203422 -42
- Misses 14232 14281 +49
- Partials 8222 8227 +5
🚀 New features to boost your workflow:
|
|
We need to link this up to #63249, which actively aims to match the current ServerResponse behaviour that this PR is now changing (neither erroring nor finishing if clients abort). These behaviours need to match. Do we definitely want to change this though? In the referenced issue (#50656) back in 2023, @ronag said:
But that's not generally true. There's plenty of counter-examples, most notably calling const { spawn } = require('node:child_process');
const child = spawn('node', ['-e', 'process.exit(0)']);
child.stdin.on('finish', () => console.log('stdin: finish'));
child.stdin.on('error', () => console.log('stdin: error'));
child.stdin.on('close', () => console.log('stdin: close'));In effect, In the specific HTTP case this is a very common occurrence (clients abort responses all the time) so this will produce a lot of errors. It's good that it's guarded just for listened events, but still it'll be a very visible behavioural change and it's hard to predict the results of that. Currently, AFAICT most implementations largely happily ignore this scenario unless they're doing something expensive, and they can already detect this manually if they want to. I'm very open to exploring this more, we could certainly improve stream & HTTP client abort behaviour, but this isn't the existing invariant, and I can see arguments on it both ways so we shouldn't just accept it as given. If we really do want to change it, we should definitely change the HTTP/2 PR above before that's merged too, and then potentially look at changing lots of other Node APIs as well afterwards to try to make this invariant consistent. |
This updates
ServerResponsesocket-close handling so an ended response thatnever emitted
finishis marked withECONNRESETand emitserrorbeforeclosewhen an error listener is present.A regression test covers the case where the request socket is destroyed before
a deferred
res.end(), which previously emitted onlyclose.Fixes: #50656
Assisted-by: openai:gpt-5.5