Skip to content

fix(storage): close HTTP2 response bodies to prevent flow-control leaks#14324

Open
krishnamd-jkp wants to merge 2 commits intogoogleapis:mainfrom
krishnamd-jkp:fix-storage-http2-leaks-14934639463026262210
Open

fix(storage): close HTTP2 response bodies to prevent flow-control leaks#14324
krishnamd-jkp wants to merge 2 commits intogoogleapis:mainfrom
krishnamd-jkp:fix-storage-http2-leaks-14934639463026262210

Conversation

@krishnamd-jkp
Copy link
Copy Markdown
Contributor

Fixes an issue where HTTP2 stream connections might exhaust flow-control quota and cause INTERNAL_ERROR in long-lived connections.

  • Modifies httpReader.Read to properly close the previous stream body before reopening it.
  • Adds a defer block in parseReadResponse to ensure response bodies aren't leaked when an error is returned during header parsing.
  • Ensures bodies are closed when stream discarding (io.CopyN) or X-Goog-Generation header parsing fails in readerReopen. These fixes apply equally to both the JSON and XML reader code paths.

Fixes an issue where HTTP2 stream connections might exhaust flow-control quota and cause INTERNAL_ERROR in long-lived connections.

- Modifies `httpReader.Read` to properly close the previous stream body before reopening it.
- Adds a defer block in `parseReadResponse` to ensure response bodies aren't leaked when an error is returned during header parsing.
- Ensures bodies are closed when stream discarding (`io.CopyN`) or `X-Goog-Generation` header parsing fails in `readerReopen`.
These fixes apply equally to both the JSON and XML reader code paths.

Co-authored-by: krishnamd-jkp <230955344+krishnamd-jkp@users.noreply.github.com>
@krishnamd-jkp krishnamd-jkp requested review from a team as code owners April 1, 2026 16:51
@product-auto-label product-auto-label bot added the api: storage Issues related to the Cloud Storage API. label Apr 1, 2026
Copy link
Copy Markdown
Contributor

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

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 improves resource management in storage/http_client.go by ensuring http.Response.Body is consistently closed during error conditions and before retrying read operations. Key changes include moving the body closure before reopening connections in the Read method and implementing a deferred closure in parseReadResponse using named return parameters. Feedback suggests enhancing error context in parseReadResponse by wrapping the strconv.ParseInt error when handling invalid Content-Range headers.

Comment on lines +1569 to +1570
err = fmt.Errorf("storage: invalid Content-Range %q", cr)
return nil, err
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

The error from strconv.ParseInt is discarded here. Per repository rules, helper functions that parse strings should include error handling for parsing failures. For consistency and better error context, consider wrapping the error here as well.

Suggested change
err = fmt.Errorf("storage: invalid Content-Range %q", cr)
return nil, err
err = fmt.Errorf("storage: invalid Content-Range %q: %w", cr, err)
return nil, err
References
  1. When implementing helper functions that parse strings, always include error handling for parsing failures.

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.

Fix this?

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

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants