Bugzilla – Bug 323081
[2.0] AsyncResult should check owner.
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by alan.mcgovern@gmail.com 2006-12-28 22:26:55 MST ---- An invalid operation exception should be thrown when i pass an AsyncResult from one object into the EndXXX of another object. Mono doesn't care. This can be a very bad thing. Run the attached testcase. In MS.NET an InvalidOperation exception is thrown on the call to EndRead as i passed in the wrong async result. using System; using System.IO; class Borked { static void Main(string[] args) { byte[] data = System.Text.Encoding.ASCII.GetBytes("This is my really long test string in which to test async reading with"); using (FileStream s = File.Create("tempFile.temp")) for (int i = 0; i < 10000; i++) s.Write(data, 0, data.Length); byte[] buffer1 = new byte[102400]; byte[] buffer2 = new byte[102400]; FileStream s1 = new FileStream("tempFile.temp", FileMode.Open, FileAccess.Read); FileStream s2 = new FileStream("tempFile.temp", FileMode.Open, FileAccess.Read); IAsyncResult result1 = s1.BeginRead(buffer1, 0, 102400, null, null); IAsyncResult result2 = s2.BeginRead(buffer2, 0, 102400, null, null); try { Console.WriteLine("Bytes read: " + s2.EndRead(result1)); } catch (Exception ex) { Console.WriteLine(ex); } s1.Dispose(); s2.Dispose(); File.Delete("tempFile.temp"); Console.WriteLine("You should have seen an exception"); Console.ReadLine(); } } ---- Additional Comments From miguel@ximian.com 2006-12-28 22:42:18 MST ---- This only happens with the 2.0 framework. the error (after removing the Disposes and the Delete) is: quack$ ./js.exe System.InvalidOperationException: The IAsyncResult object provided does not match this delegate. at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase) at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData) at System.IO.Stream.ReadDelegate.EndInvoke(IAsyncResult result) at System.IO.Stream.EndRead(IAsyncResult asyncResult) at System.IO.FileStream.EndRead(IAsyncResult asyncResult) at Borked.Main(String[] args) You should have seen an exception ---- Additional Comments From miguel@ximian.com 2006-12-28 22:48:35 MST ---- This would be only a 2.0 feature, and since we are calling the runtime generated BeginXXX method from the delegate, this probably will have to be checked in RealProxy.PrivateInvoke in corlib. ---- Additional Comments From robertj@gmx.net 2006-12-29 00:29:57 MST ---- Created an attachment (id=171191) marshal.diff ---- Additional Comments From robertj@gmx.net 2006-12-29 00:31:27 MST ---- I don't know how to test the runtime version inside the runtime itself, so the patch is not complete unless we want this on NET_1_* too. ---- Additional Comments From robertj@gmx.net 2006-12-29 00:35:13 MST ---- Created an attachment (id=171192) bug-80392.diff (unit test) ---- Additional Comments From robertj@gmx.net 2007-01-02 08:25:51 MST ---- -> runtime ---- Additional Comments From lupus@ximian.com 2007-01-09 08:32:15 MST ---- Robert, you can use the following check for the current runtime version: mono_get_runtime_info ()->framework_version [0] == '2' Add that after the delegate check and commit, thanks. ---- Additional Comments From robertj@gmx.net 2007-01-09 18:46:55 MST ---- Fixed in SVN r70742:70744. Imported an attachment (id=171191) Imported an attachment (id=171192) Unknown operating system unknown. Setting to default OS "Other".