Skip to content

fix(core): reject instead of hanging when a multipart response fails - #3324

Open
yeegz wants to merge 2 commits into
firebase:mainfrom
yeegz:fix/2105-multipart-stream-error
Open

yeegz wants to merge 2 commits into
firebase:mainfrom
yeegz:fix/2105-multipart-stream-error

Conversation

@yeegz

@yeegz yeegz commented Nov 18, 2026

Copy link
Copy Markdown

Summary

handleMultipartResponse didn't listen for 'error' on the response stream, the multipart parser or its parts:

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 its req.destroyed early 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:

  • a local HTTP server sends part of a multipart body and then drops the socket (retries off): the request rejects with ECONNRESET
  • a truncated multipart body: the request rejects

On main, the first times out (the request never settles) and the second throws an uncaught error. api-request.spec.ts and the messaging specs pass (716), and eslint and tsc are clean.


I used an AI coding assistant for this and reviewed it myself.

🤖 Generated with Claude Code

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
@yeegz
yeegz requested a review from a team November 18, 2026 11:21

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils/api-request.ts
Comment on lines +635 to +637
const onError = (err: Error): void => {
this.enhanceAndReject(err, null, response.request);
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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);
};

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request hangs up on connection destroy when parsing response stream

1 participant