Skip to content

fix(core): reject prematurely closed HTTP/2 streams - #3326

Open
eger1393 wants to merge 2 commits into
firebase:mainfrom
eger1393:fix/http2-stream-close-3325
Open

eger1393 wants to merge 2 commits into
firebase:mainfrom
eger1393:fix/http2-stream-close-3325

Conversation

@eger1393

Copy link
Copy Markdown

Discussion

Fixes #3325.

AsyncHttp2Call listens for response and error events, but a stream can close before response headers without either event. Its request Promise then remains pending, and Messaging.sendEach() cannot finish waiting for all message requests.

Reject the request with ECONNRESET when close fires before readableEnded. This uses the existing HTTP client's error translation and configurable retry policy.

Checking readableEnded avoids rejecting a complete response while asynchronous decompression is still finishing. An original stream error that already rejected the Promise is preserved.

This does not modify multipart parsing or overlap with the multipart-specific changes proposed in #3324 for #2105. No new timeout, transport switch, dependency, or background process is introduced.

Testing

Added six tests using a real localhost HTTP/2 server:

  • Session destroyed before response headers.
  • Stream canceled before response headers.
  • Original HTTP/2 stream error preserved.
  • Complete JSON response succeeds.
  • Complete gzip response succeeds.
  • Canceled stream retries successfully when connection resets are configured as retryable.

Before the fix, the first two cases and retry case timed out; the three controls passed. After the fix, all six pass.

The public issue's reproduction was also run against the built SDK: the control succeeds, and both failure cases settle with a failed message result instead of hanging. The verifier allows at least one request rather than exactly one, since a canceled stream now correctly reaches the existing retry policy.

Validation on Windows, Node.js v26.13.0, npm 11.20.0:

  • npm run build — passed.
  • npm run build:tests — passed.
  • npm test — lint passed; 6,470 tests passed.
  • npm run api-extractor — passed; public API reports are unchanged.
  • git diff --check — passed.
  • Built-SDK reproduction: normal response succeeds; session disconnect fails promptly; repeated stream cancellation returns a failed result after five configured attempts (about seven seconds), without reaching the 20-second watchdog.

Emulator-based integration tests were not run locally: the installed Java runtime is 11, whereas the repository's CI uses Java 21. No live-project integration tests were run.

API Changes

None. Public API declarations, dependencies, and retry configuration are unchanged.

Additional context

Prepared with AI assistance. Tests use only localhost, fake credentials, and fake registration tokens; no live FCM requests are made by the reproduction or new regression tests.

@eger1393
eger1393 requested a review from a team November 19, 2026 10:00
@google-cla

google-cla Bot commented Nov 19, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 adds a 'close' event listener to HTTP/2 requests to reject the request with an ECONNRESET error if the stream closes before the response is fully read, and includes comprehensive unit tests with a local HTTP/2 server. The feedback suggests using a strict equality check req.readableEnded === false instead of !req.readableEnded to avoid potential issues where readableEnded is undefined in mock or custom stream environments.

Comment thread src/utils/api-request.ts Outdated

req.on('close', () => {
// A closed stream cannot time out or produce the response the caller is waiting for.
if (!req.readableEnded) {

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

Using strict equality check req.readableEnded === false is safer and more robust than !req.readableEnded. In some testing environments or when using custom mock streams, the readableEnded property might be undefined. If it is undefined, !req.readableEnded evaluates to true, which would cause successful requests to be incorrectly rejected with ECONNRESET when the stream closes. Checking for explicit false prevents this issue while still correctly identifying prematurely closed streams in all supported Node.js environments.

Suggested change
if (!req.readableEnded) {
if (req.readableEnded === false) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated in 514aded to check req.readableEnded === false explicitly. Native ClientHttp2Stream exposes a boolean, so behavior on supported Node.js versions is unchanged; an undefined property in an incomplete mock no longer triggers this fallback. Re-ran npm test (lint and all 6,470 tests passed), npm run build:tests, and git diff --check.

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.

[Messaging] sendEach() remains pending after an HTTP/2 stream closes without a response

1 participant