To send JSON RPC requests to my server, I'm calling WebRequest's GetRequestStreamAsync() once a second. Unfortunately, I get an ObjectDisposedException every hour or two:
> System.ObjectDisposedException: System.ObjectDisposedException: Cannot access a disposed object.
> Object name: 'System.Net.Sockets.NetworkStream'.
> at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x00059] in /build/mono-22.214.171.124/mcs/class/System/System.Net/HttpWebRequest.cs:1031
> at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> --- End of stack trace from previous location where exception was thrown ---
> at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <37f6d41e94d84ceba93f73ad8a999efc>:0
> at PLSNetwork.Network+<SendDataViaRpcAsync>d__86`1[T].MoveNext () [0x00334] in <daa2e52e162d40a4b36d02fa848aa03c>:0
Could this be a race condition in Mono? Happens (at least) in 5.0 and 5.2.
If I call GetRequestStreamAsync() more often, it gets worse. Sometimes I get ObjectDisposedExpections every few minutes!
I'm going to check if this bug depends on the WebRequest settings I use (MaxIdleTime, ConnectionLimit, etc.).
Any help is appreciated!
Might be related to Bug 54059 ("Sporadic objectDisposed exception in client http POST request") or https://github.com/mono/mono/pull/4693 ("[WebConnection] Fix race condition between Close and BeginWrite")?
Hello, this is likely a race condition in the HTTP stack. Could you please provide a repro case so we can look at it? Anything, even if it takes a few minutes to reproduce, would be enough. Thank you.
Created attachment 23215 [details]
MWE to reproduce the bug
I attached a MWE. This sets up a simple web server on port 4321 and then continuously sends POST requests to this server.
> mcs Program.cs && mono --debug Program.exe
This should lead to a ObjectDisposedException every few seconds.
The exception does not happen if KeepAlive is true!
I hope this helps and the reason is not a mistake in my code.
I reproduce the bug with the sample you provided, and it's takes a bit longer than a few seconds (a few minutes). I could also reproduce on macOS.
Thank you very much,
Needs to be triaged against the new web stack.