-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix XDocument.LoadAsync to not use sync reads #123800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.CreateAsyncmethod 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 |
| { | ||
| string? version = r.GetAttribute("version"); | ||
| string? encoding = r.GetAttribute("encoding"); | ||
| string? standalone = r.GetAttribute("standalone"); |
Copilot
AI
Jan 30, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra whitespace before 'standalone' variable name. Should be single space like the lines above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| string? standalone = r.GetAttribute("standalone"); | |
| string? standalone = r.GetAttribute("standalone"); |
|
Tagging subscribers to this area: @dotnet/area-system-xml |
stephentoub
left a comment
There was a problem hiding this 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?
| { | ||
| string? version = r.GetAttribute("version"); | ||
| string? encoding = r.GetAttribute("encoding"); | ||
| string? standalone = r.GetAttribute("standalone"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| string? standalone = r.GetAttribute("standalone"); | |
| string? standalone = r.GetAttribute("standalone"); |
Fixes #100239