Skip to content

XmlReader ReadElementContentAsString Hangs if End of Document Tag Reached in Stream  #1029

Open

Description

@iUnknwn

Given the following XML sent over a network stream:
<?xml version="1.0" encoding="utf-16"?><DeviceCommand>PlayTone</DeviceCommand>

Which is generated by the XmlWriter command:

var writer = XmlWriter.Create(namedPipeClientStream)
writer.WriteElementString("DeviceCommand", "PlayTone")
writer.Flush()

This reader command will cause an XML reader on the server to hang until the network stream is closed:

var reader = XmlReader.Create(namedPipeServerStream)
reader.MoveToContent()
var txt = reader.ReadElementContentAsString()

The command fails even though the entire element was sent. This alternative command works as expected:

reader.MoveToContent()
reader.Read()
var txt = reader.Value;

However, if the stream is closed, then the XmlReader is able to read the node without issue and the call to ReadElementContentAsString works as expected.

It looks like XmlReader is waiting until it gets the node after the EndElement before it returns the string, but it doesn't properly recognize the end of the root node. This appears to be undocumented behavior.

This was tested on .NET Core 3.0 on Windows.

Activity

  1. added
    untriaged
    on Dec 18, 2019
  2. removed
    untriaged
    on Jun 29, 2020
  3. added this to the 6.0.0 milestone on Jun 29, 2020
  4. buyaa-n commented on Jun 29, 2020

    @buyaa-n
    Contributor

    @iUnknwn thank you for reporting the issue, this issue needs some detailed investigations which cannot be done within .Net 5.
    It would be great if you would be interested in doing the investigation/fixes.

  5. iUnknwn commented on Jun 30, 2020

    @iUnknwn
    Author

    @buyaa-n I'd be potentially interested, what are you asking for?

    I could probably write up an MSUnit/xUnit test to test for this behavior, if you're looking for something basic to demo the problem.

    If you're looking for a patch/PR to fix the issue, that might be a bit trickier.

  6. buyaa-n commented on Jun 30, 2020

    @buyaa-n
    Contributor

    I could probably write up an MSUnit/xUnit test to test for this behavior, if you're looking for something basic to demo the problem.

    That would be very helpful for whoever would investigate it

    If you're looking for a patch/PR to fix the issue, that might be a bit trickier.

    Sure it is not easy, and it is optional/voluntary. If you are willing to contribute for test repro, finding the root cause, propose a fix or raise a PR etc., whichever level it was that would be very helpful. Thanks again!

  7. iUnknwn commented on Aug 6, 2020

    @iUnknwn
    Author

    @buyaa-n Sorry this took so long, but I have a repo setup with test cases for this issue. It shows the issue only happens on open streams, and it specifically occurs on single-element html items.

    https://github.com/iUnknwn/XmlReaderBugDemo

    Please let me know if you need more info.

  8. modified the milestones: 6.0.0, 7.0.0 on Jul 15, 2021
  9. krwq commented on Jul 15, 2021

    @krwq
    Member

    For now I believe you should be able to use ReadContentAsString which ReadElementContentAsString calls internally except ReadElementContentAsString also tries to move to the next element.

    I think the fix for this should be somewhere here

    private void FinishReadElementContentAsXxx()

    but this will likely require more investigation as there are more than one XmlReader implementation in the framework.

    I'm moved this to 7.0 for now since we do not plan to do this investigation in 6.0

  10. ghost added
    needs-further-triage
    on Jul 15, 2021
  11. removed
    needs-further-triage
    on Jul 15, 2021
  12. modified the milestones: 7.0.0, Future on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions