[Mono-bugs] [Bug 56883][Maj] New - FileStream.EndRead returns 0 in AsyncIO Callback

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 12 Apr 2004 17:15:17 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by marijn@mad.scientist.com.

http://bugzilla.ximian.com/show_bug.cgi?id=56883

--- shadow/56883	2004-04-12 17:15:17.000000000 -0400
+++ shadow/56883.tmp.19948	2004-04-12 17:15:17.000000000 -0400
@@ -0,0 +1,107 @@
+Bug#: 56883
+Product: Mono: Class Libraries
+Version: unspecified
+OS: other
+OS Details: Microsoft Windows XP Professional
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Major
+Component: System
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: marijn@mad.scientist.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: FileStream.EndRead returns 0 in AsyncIO Callback
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+Calling EndRead in an Async Callback returns 0 on windos with mono version
+0.31. with mono 0.30 on windows and 0.29.99 and 0.30.2 on linux is
+correctly returns the length of the request.
+
+Steps to reproduce the problem:
+1. compile and run the following code on mono 0.31 on windows:
+
+using System;
+using System.IO;
+using System.Threading;
+                                                                          
+                                                                          
+        
+public class AsyncIO
+{
+    public static bool finish = false;
+    public static Object mutex = new Object[0];
+    public static byte[] buffer = new byte[1];
+                                                                          
+                                                                          
+        
+    public static void ReadCallback(IAsyncResult asyncResult)
+    {
+        Stream stream = (Stream) asyncResult.AsyncState;
+        int bytesRead = stream.EndRead(asyncResult);
+        stream.Close();
+                                                                          
+                                                                          
+        
+        if(bytesRead != buffer.Length)
+        {
+            Console.Out.WriteLine("short read: requested {0}, got {1}!",
+buffer.Length, bytesRead);
+            throw new Exception("short read!");
+        }
+                                                                          
+                                                                          
+        
+        // signal completion
+        lock(mutex)
+        {
+            finish = true;
+            Monitor.Pulse(mutex);
+        }
+    }
+                                                                          
+                                                                          
+        
+    public static void Main(string[] args)
+    {
+        AsyncCallback readCallback = new
+            AsyncCallback(ReadCallback);
+        FileStream fs = new FileStream("AsyncIO.exe",
+            FileMode.Open, FileAccess.Read, FileShare.Read, 1, true);
+        fs.BeginRead(buffer, 0, buffer.Length, readCallback, fs);
+                                                                          
+                                                                          
+        
+        // wait for completion
+        lock (mutex)
+        {
+            if (!finish)
+            {
+                Monitor.Wait(mutex);
+            }
+        }
+    }
+ }
+
+Actual Results:
+Hangs after "short read: requested 1, got 0!"
+
+Expected Results:
+Runs to completion, as it does on 0.30.
+
+How often does this happen? 
+Always.
+
+Additional Information:
+The hang follows from the uncaught exception killing the handler-thread.
+
+The msdn documentation for EndRead reads
+(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassendreadtopic.asp):
+"Return Value: The number of bytes read from the stream, between 0 and the
+number of bytes you requested. Streams only return 0 at the end of the
+stream, otherwise, they should block until at least 1 byte is available."