[Process] Fix possible double call to Begin(Output|Error)ReadLine
authorLudovic Henry <ludovic@xamarin.com>
Thu, 4 Feb 2016 14:28:17 +0000 (14:28 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Thu, 4 Feb 2016 14:30:25 +0000 (14:30 +0000)
This is not allowed in .NET, as it could lead to unexpected results.

mcs/class/System/System.Diagnostics/Process.cs
mcs/class/System/Test/System.Diagnostics/ProcessTest.cs

index 59616e64d94d652f9091843da386f04e2ab191b6..462245210456027a6140718be20df39c954f0239 100644 (file)
@@ -1350,6 +1350,9 @@ namespace System.Diagnostics {
                        if ((async_mode & AsyncModes.SyncOutput) != 0)
                                throw new InvalidOperationException ("Cannot mix asynchronous and synchonous reads.");
 
+                       if ((async_mode & AsyncModes.AsyncOutput) != 0)
+                               throw new InvalidOperationException ("An async read operation has already been started on the stream.");
+
                        async_mode |= AsyncModes.AsyncOutput;
 
                        if (async_output == null)
@@ -1396,6 +1399,9 @@ namespace System.Diagnostics {
                        if ((async_mode & AsyncModes.SyncError) != 0)
                                throw new InvalidOperationException ("Cannot mix asynchronous and synchonous reads.");
 
+                       if ((async_mode & AsyncModes.AsyncError) != 0)
+                               throw new InvalidOperationException ("An async read operation has already been started on the stream.");
+
                        async_mode |= AsyncModes.AsyncError;
 
                        if (async_error == null)
index 602a07d017a487e811174e4909f508256dcba424..9b883465eb630fb7ea8507eed6f59de3f89aeef2 100644 (file)
@@ -1018,5 +1018,45 @@ namespace MonoTests.System.Diagnostics
                                Assert.IsTrue (found, sb.ToString ());
                        }
                }
+
+               [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void TestDoubleBeginOutputReadLine ()
+               {
+                       using (Process p = new Process ()) {
+                               p.StartInfo = GetCrossPlatformStartInfo ();
+                               p.StartInfo.UseShellExecute = false;
+                               p.StartInfo.RedirectStandardOutput = true;
+                               p.StartInfo.RedirectStandardError = true;
+
+                               p.Start ();
+
+                               p.BeginOutputReadLine ();
+                               p.BeginOutputReadLine ();
+
+                               Assert.Fail ();
+                       }
+               }
+
+               [Test]
+               [NUnit.Framework.Category ("MobileNotWorking")]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void TestDoubleBeginErrorReadLine ()
+               {
+                       using (Process p = new Process ()) {
+                               p.StartInfo = GetCrossPlatformStartInfo ();
+                               p.StartInfo.UseShellExecute = false;
+                               p.StartInfo.RedirectStandardOutput = true;
+                               p.StartInfo.RedirectStandardError = true;
+
+                               p.Start ();
+
+                               p.BeginErrorReadLine ();
+                               p.BeginErrorReadLine ();
+
+                               Assert.Fail ();
+                       }
+               }
        }
 }