Bug 57705 - [New Web Stack] GetRequestStreamAsync: System.ObjectDisposedException in HttpWebRequest.cs
Summary: [New Web Stack] GetRequestStreamAsync: System.ObjectDisposedException in Http...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http ()
Version: 5.2 (2017-04)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on: 58141
  Show dependency tree
Reported: 2017-06-22 20:42 UTC by Fabian
Modified: 2017-09-13 19:12 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

MWE to reproduce the bug (2.60 KB, text/plain)
2017-06-29 14:12 UTC, Fabian

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 57705 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Fabian 2017-06-22 20:42:57 UTC
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- 
>  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!
Comment 1 Fabian 2017-06-23 10:45:17 UTC
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")?
Comment 2 Ludovic Henry 2017-06-28 19:51:05 UTC
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.
Comment 3 Fabian 2017-06-29 14:12:15 UTC
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.
Comment 4 Ludovic Henry 2017-06-29 22:11:54 UTC

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,

Comment 5 Martin Baulig 2017-09-13 19:12:29 UTC
Needs to be triaged against the new web stack.