none
Console.WriteLine "Probable I/O race condition" issue RRS feed

  • Question

  • I noticed that an I/O race condition is triggered when using Console.WriteLine.

    The following exception will be thrown,
    System.IndexOutOfRangeException: Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.

    This is very hard to reproduce but it happened in a production environment.

    To reproduce this, I have to use the following code on a WPF application project.

    The project must be compiled and the application run from the output folder. It must not run in debug mode.

    Parallel.For(0, 1000, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, (_) =>
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("test message to the out stream");
                Thread.Sleep(100);
                Console.Error.WriteLine("test message to the error stream");
            }
        }
        catch (Exception ex)
        {
            //Debugger.Launch();
            WriteToMessageArea(ex.Message);
            //throw;
        }
    });
    Has anyone experience this issue?

    Thursday, October 27, 2016 3:20 AM

All replies

  • Hi decryp7,

    Console and Console.Error are both thread-safe as they each return a thread-safe wrapper (via TextWriter.Synchronized) for the console output and error stream TextWriters. However, this thread-safety only applies if Consolet and Console.Error are TextWriters for different streams.The output and error TextWriters are both set to StreamWriter.Null, which is a singleton. Your code calls both Console.WriteLine and Console.Error.WriteLine and this causes the exception when one thread happens to call Console.WriteLine at the same time that another thread is calling Console.Error.WriteLine. This causes the same stream to be written to from 2 threads at the same time, resulting in the "Probable I/O race condition detected while copying memory." exception. If you only use Console.WriteLine or only use Console.Error.WriteLine, you'll discover that the exception no longer occurs.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Wednesday, November 2, 2016 5:37 AM
    • Unproposed as answer by decryp7 Wednesday, November 2, 2016 6:24 AM
    Thursday, October 27, 2016 6:58 AM
  • Hi, Could you point me to the location where this is documented? Thank you.
    Saturday, October 29, 2016 1:41 AM
  • Hi, Could you point me to the location where this is documented? Thank you.

    Hi decryp7,

    I've tested your demo on my side. Yes, these code will throw exception as you descried.

    All above that I've referred from Here. But as Hans Passant says, this is a bug should be reported at connect.microsoft.com.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 31, 2016 1:22 AM