Skip to content

Implement XmlSerializer end element handling workaround and tests#126765

Open
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
StephenMolloy:47371-EndOfStream-Workaround
Open

Implement XmlSerializer end element handling workaround and tests#126765
StephenMolloy wants to merge 1 commit intodotnet:mainfrom
StephenMolloy:47371-EndOfStream-Workaround

Conversation

@StephenMolloy
Copy link
Copy Markdown
Member

@StephenMolloy StephenMolloy commented Apr 10, 2026

This pull request improves handling of XML fragments over streaming transports, particularly when deserializing multiple root elements. The changes add a workaround (enabled by default) to avoid unnecessary reads after a top-level end element, preventing timeouts or exceptions when more data is not immediately available. An AppContext opt-out is also included. The PR also adds comprehensive tests and supporting infrastructure to validate and control this behavior.

XML Serializer Compatibility and Behavior Improvements:

  • Added a new UseXmlSerializerReadEndElementWorkaround switch (default: true) to LocalAppContextSwitches to control workaround behavior for reading end elements in XML fragments.
  • Updated XmlSerializationReader.ReadEndElement() to respect the new switch, avoiding extra reads after a top-level end element when enabled, which prevents blocking or timeouts in streaming/fragment scenarios.
  • Introduced AdvancePastTopLevelEndElementIfNeeded(XmlReader) utility and invoked it in XmlSerializer methods (GetMapping, CanDeserialize) to ensure correct reader advancement when the workaround is enabled. [1] [2] [3]

Testing and Infrastructure Enhancements:

  • Added new tests to XmlSerializerTests.cs to verify deserialization of XML fragments with and without the workaround, including a test that disables the switch and expects a timeout exception.
  • Implemented BlockingAfterBufferStream, a custom Stream that simulates blocking behavior after buffer exhaustion, to facilitate robust testing of streaming scenarios.
  • Improved XmlSerializerAppContextSwitchScope to properly unset AppContext switches and clear cached values, ensuring test isolation and reliability.

Fixes: #47371

Copilot AI review requested due to automatic review settings April 10, 2026 21:26
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Apr 10, 2026
Copy link
Copy Markdown
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 PR improves XmlSerializer behavior when deserializing XML fragments over streaming transports by avoiding an extra read after completing a top-level end element (configurable via an AppContext switch), and adds tests to validate fragment deserialization behavior with the workaround enabled/disabled.

Changes:

  • Added UseXmlSerializerReadEndElementWorkaround AppContext switch (default true) to control end-element read behavior.
  • Updated XmlSerializationReader.ReadEndElement() and XmlSerializer entry points to avoid unnecessary reads after a top-level end element while still supporting multi-root fragment workflows.
  • Added fragment/streaming-focused tests plus a custom stream and improved AppContext-switch test scoping.

Reviewed changes

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

File Description
src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs Introduces the new AppContext switch (default enabled).
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs Implements the end-element handling workaround in ReadEndElement().
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs Advances past top-level end elements when starting Deserialize / CanDeserialize to enable multi-fragment reading.
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs Adds streaming/fragment tests and supporting test infrastructure (custom stream + switch scope improvements).

Comment on lines +919 to +924
XmlSerializer serializer = new XmlSerializer(typeof(SimpleType));
byte[] data = Encoding.UTF8.GetBytes(payload);

// Default should be true
//const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround";
//using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true);
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Remove the commented-out AppContext switch setup and make the test explicitly set the switch value (e.g., within an XmlSerializerAppContextSwitchScope). As written, the test both carries dead commented code and implicitly relies on the process-wide default/environment switch value, which can make the test less deterministic.

Suggested change
XmlSerializer serializer = new XmlSerializer(typeof(SimpleType));
byte[] data = Encoding.UTF8.GetBytes(payload);
// Default should be true
//const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround";
//using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true);
const string switchName = "Switch.System.Xml.UseXmlSerializerReadEndElementWorkaround";
XmlSerializer serializer = new XmlSerializer(typeof(SimpleType));
byte[] data = Encoding.UTF8.GetBytes(payload);
using var switchScope = new XmlSerializerAppContextSwitchScope(switchName, true);

Copilot uses AI. Check for mistakes.
Comment on lines +3092 to +3100
internal sealed class BlockingAfterBufferStream : Stream
{
private readonly byte[] _buffer;
private int _position;

public BlockingAfterBufferStream(byte[] buffer)
{
_buffer = buffer;
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

BlockingAfterBufferStream does not actually block; once the buffer is exhausted it throws TimeoutException. Consider renaming it to reflect the behavior (timeout/throwing) or implementing actual blocking semantics to match the current name, to avoid misleading future readers of the test infrastructure.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlSerializer requires next tag after EndElement to be present in Stream

2 participants