Skip to content

Fix XDocument.LoadAsync to not use sync reads#123800

Merged
stephentoub merged 3 commits intodotnet:mainfrom
lilinus:xdocument-loadasync-async
Feb 4, 2026
Merged

Fix XDocument.LoadAsync to not use sync reads#123800
stephentoub merged 3 commits intodotnet:mainfrom
lilinus:xdocument-loadasync-async

Conversation

@lilinus
Copy link
Contributor

@lilinus lilinus commented Jan 30, 2026

Fixes #100239

Copilot AI review requested due to automatic review settings January 30, 2026 14:41
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 30, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes an issue where XDocument.LoadAsync was incorrectly using synchronous reads when processing XML declarations, violating the async contract. The fix ensures that the async code path uses await r.ReadAsync() instead of the synchronous r.Read().

Changes:

  • Moved XML declaration handling from shared initialization code into separate sync/async code paths
  • Added new XDeclaration.CreateAsync method to handle XML declaration creation asynchronously
  • Added comprehensive tests to verify that async methods only call async I/O operations and sync methods only call sync I/O operations

Reviewed changes

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

File Description
src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDocument.cs Moved XML declaration handling from InitLoad into Load and LoadAsyncInternal to support separate sync/async code paths
src/libraries/System.Private.Xml.Linq/src/System/Xml/Linq/XDeclaration.cs Added CreateAsync method to support asynchronous XML declaration creation
src/libraries/System.Private.Xml.Linq/tests/misc/LoadSaveAsyncTests.cs Added test coverage to verify sync/async I/O patterns are correctly followed

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-xml
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

This looks reasonable to me, but @krwq can you take a look, too?

Copilot AI review requested due to automatic review settings February 2, 2026 09:11
Copy link
Contributor

Copilot AI left a comment

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.

@stephentoub stephentoub requested a review from krwq February 2, 2026 13:57
@stephentoub stephentoub merged commit 3d22564 into dotnet:main Feb 4, 2026
85 of 89 checks passed
@stephentoub
Copy link
Member

thanks, @lilinus

lewing pushed a commit to lewing/runtime that referenced this pull request Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Xml community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Synchronous call inside XDocument.LoadAsync

3 participants