0

Our diagnostic service said there was a hang at this position of our code. Is there anything that is shown that could cause a hang when reading a XML file with StreamReader?

STACK_TEXT:  
000000e1`8225cef8 00007ffa`b93f8eb8     : 00000000`00000002 00000000`00000000 00007ffa`b1ef6cdf 000000e1`8225cdd8 : ntdll!NtReadFile+0xa
000000e1`8225cf00 00007ffa`b0b3c9c8     : 00000000`00001000 00000000`00000000 00000000`00000018 000000e1`8225d098 : KERNELBASE!ReadFile+0x74
000000e1`8225cf80 00007ffa`b0a8e803     : 000000e3`d59a39d8 000000e3`d5b65b00 000000e3`d59a3ea8 00000000`00000400 : mscorlib_ni+0x63c9c8
000000e1`8225d060 00007ffa`b0a8e75d     : 000000e3`d5ec4bd8 00000000`00001000 00000000`00000000 000000ee`f3acbb90 : mscorlib_ni!System.IO.FileStream.ReadFileNative+0x83
000000e1`8225d0c0 00007ffa`b0a8e68a     : 00000000`00000000 000000e3`d5b65b30 000000e3`d5b65300 00007ffa`b0a775db : mscorlib_ni!System.IO.FileStream.ReadCore+0x5d
000000e1`8225d130 00007ffa`b0b082d4     : 000000e3`d5b65b30 00007ffa`b0a8e898 00000000`00000001 000000e3`d59a39f8 : mscorlib_ni!System.IO.FileStream.Read+0x13a
000000e1`8225d190 00007ffa`b0b09fab     : 00000000`00000000 00000000`00000000 00007ffa`b1f30d35 000000e1`8225cef8 : mscorlib_ni!System.IO.StreamReader.ReadBuffer+0x44
000000e1`8225d1e0 00007ffa`57ffff6d     : 00007ffa`b05a5188 00000000`00000004 000000e3`d598bf58 00007ffa`52e4d508 : mscorlib_ni!System.IO.StreamReader.ReadToEnd+0x8b
000000e1`8225d230 00007ffa`57fe774a     : 000000e3`d598bf58 000000e3`d59a3688 fffffff8`00000004 00000000`00000000 : Tournaments_Services!Tournaments.Services.Statistics.StatCrewService.ParseXml+0xbd

Code

private IGameCastGame ParseXml(string fileLocation, StatisticsType statisticsType)
{
    XDocument xDocument;

    try
    {
        using (var fileStream = new FileStream(
            fileLocation,
            FileMode.Open,
            FileAccess.Read,
            FileShare.ReadWrite))
        {
            using (var streamReader = new StreamReader(fileStream))
            {
                xDocument = XDocument.Parse(streamReader.ReadToEnd());
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error(ex);
        return null;
    }

    return StatCrewGame.Create(xDocument, statisticsType).Parse();
}
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 3
    A way to large XML File? – Ralf Jul 04 '23 at 15:30
  • 1
    Stack trace says there's a problem reading the file, nothing to do with parsing the XML. – Neil Jul 04 '23 at 15:41
  • Why are you doing `XDocument.Parse(streamReader.ReadToEnd())` instead of [`XDocument.Load(streamReader)`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.load?view=net-7.0#system-xml-linq-xdocument-load(system-io-textreader))? Reading the XML into a temporary string will be much less memory-efficient than not doing so. And if the XML file is large you might hit the [effective string length limit](https://stackoverflow.com/q/140468) on your system. – dbc Jul 04 '23 at 15:54
  • Also, why `FileShare.ReadWrite` and not `FileShare.Read`? What do you want to happen if some other process modifies the file as you are reading it? – dbc Jul 04 '23 at 15:56
  • Well this file is written to at the same time as its being read by thousands of users. I updated to `XDocument.Load(streamReader)` for now. Not sure why I did that before. These are not large files. – Mike Flynn Jul 05 '23 at 01:36
  • @dbc I have people writing to the same file as people reading. What would be a better approach then? – Mike Flynn Jul 06 '23 at 01:24
  • *Well this file is written to at the same time as its being read by thousands of users.* -- I reckon the better approach would be to use a database. As it is, if one process writes while others are reading, the results are unpredictable. From the point of view of the readers, the file may look corrupt because it gets overwritten partway through. – dbc Jul 06 '23 at 01:39
  • Yes I handle corrupt XML by just storing the valid xml in cache until a fresh, valid xml is available. – Mike Flynn Jul 06 '23 at 11:04

0 Answers0