Repository navigation
Conversation
handleMultipartResponse had no 'error' handlers on the response stream, the multipart parser or its parts. A connection reset mid-body left the request pending forever, and a truncated body was thrown as an unhandled 'error' event. Handle all three and reject the request. Fixes firebase#2105
There was a problem hiding this comment.
Code Review
This pull request introduces error handling for multipart responses in AsyncRequestCall to handle dropped connections or truncated bodies, preventing requests from hanging. It also adds corresponding unit tests. The reviewer suggested destroying the request in the onError handler to immediately terminate the connection and free up resources if a parsing error occurs.
| const onError = (err: Error): void => { | ||
| this.enhanceAndReject(err, null, response.request); | ||
| }; |
There was a problem hiding this comment.
If a parsing error occurs on the multipartParser or one of its parts (e.g., due to malformed multipart data), the network stream (respStream) and the request may still be active. Without explicitly destroying the request, the client will continue to download the remaining data from the server, wasting bandwidth and system resources. Calling response.request?.destroy() on error ensures that the connection is immediately terminated and resources are freed.
| const onError = (err: Error): void => { | |
| this.enhanceAndReject(err, null, response.request); | |
| }; | |
| const onError = (err: Error): void => { | |
| response.request?.destroy(); | |
| this.enhanceAndReject(err, null, response.request); | |
| }; |
Summary
handleMultipartResponsedidn't listen for'error'on the response stream, the multipart parser or its parts:'error'event.This adds one handler for all three that rejects the request, which then goes through the usual retry logic.
One difference from
handleRegularResponse: I didn't copy itsreq.destroyedearly return. Once the response is being read, the request is already marked destroyed, so that check swallowed exactly these errors. Rejecting an already-settled promise is a no-op, so the check isn't needed for safety.Fixes #2105
Tests
Two new tests in
api-request.spec.ts:ECONNRESETOn
main, the first times out (the request never settles) and the second throws an uncaught error.api-request.spec.tsand the messaging specs pass (716), and eslint andtscare clean.I used an AI coding assistant for this and reviewed it myself.
🤖 Generated with Claude Code