Skip to content

fix: InputStreamContent closes caller InputStream between retry attempts#49650

Open
arnabnandy7 wants to merge 13 commits into
Azure:mainfrom
arnabnandy7:fix/stream-closed-retry
Open

fix: InputStreamContent closes caller InputStream between retry attempts#49650
arnabnandy7 wants to merge 13 commits into
Azure:mainfrom
arnabnandy7:fix/stream-closed-retry

Conversation

@arnabnandy7

Copy link
Copy Markdown
Contributor

Description

This pull request resolves Issue #49648, where InputStreamContent built from a mark/reset-capable InputStream loses replayability after a failed HTTP attempt, causing all subsequent retries to fail with an IOException: Stream closed exception.

Root Cause

When validating the body length of a request, RestProxyUtils wraps the InputStream in a LengthValidatingInputStream. Upon a failed request, the HTTP client/reactive pipeline cleans up resources by calling close() on the wrapped stream. LengthValidatingInputStream.close() was unconditionally delegating the close() call to the underlying user-provided stream, thereby closing it prematurely. On subsequent retries, when the pipeline calls reset() on the wrapped stream, the underlying stream throws a "Stream closed" exception as it has already been closed.

Fix

Updated LengthValidatingInputStream.close() to be a no-op. Since the contract of BinaryData.fromStream(inputStream) guarantees that the Azure SDK does not take ownership or close the user-provided stream (the caller is responsible for its lifecycle), the REST proxy should not close the underlying stream either.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
Copilot AI review requested due to automatic review settings June 26, 2026 19:39
@github-actions github-actions Bot added Azure.Core azure-core Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution @arnabnandy7! We will review the pull request and get back to you soon.

Copilot AI 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.

Pull request overview

Fixes an InputStreamContent retry regression in azure-core where request-body length validation wrapped the caller’s InputStream in LengthValidatingInputStream, and the wrapper’s close() would close the underlying caller stream between retry attempts—breaking mark/reset replayability and causing subsequent retries to fail with IOException: Stream closed.

Changes:

  • Changed LengthValidatingInputStream.close() to be a no-op to avoid closing the underlying user-provided InputStream between retries.
  • Added in-code rationale tying the behavior to the BinaryData.fromStream ownership/closure contract.

Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

arnabnandy7 and others added 2 commits June 27, 2026 01:41
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread sdk/core/azure-core/CHANGELOG.md Outdated
arnabnandy7 and others added 2 commits June 27, 2026 02:04
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Arnab Nandy <arnab_nandy7@yahoo.com>
@arnabnandy7 arnabnandy7 requested a review from Copilot June 26, 2026 20:41

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread sdk/core/azure-core/src/test/java/com/azure/core/util/UrlBuilderTests.java Outdated
Comment thread sdk/core/azure-core/src/test/java/com/azure/core/util/UrlBuilderTests.java Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread sdk/core/azure-core/src/test/java/com/azure/core/util/UrlBuilderTests.java Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread sdk/core/azure-core/src/test/java/com/azure/core/util/UrlBuilderTests.java Outdated
Apply Spotless formatting to the strengthened close/reset regression test
and remove an accidental duplicate semicolon.
Document caller-owned stream handling in LengthValidatingInputStream,
use assertFalse in the close/reset regression test, and revert unrelated
UrlBuilder timeout increases.

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

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

Labels

Azure.Core azure-core Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] InputStreamContent closes caller InputStream between retry attempts, causing resettableContent() to fail with "Stream closed"

2 participants