Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
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
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 8886 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
In special cases on GitHub you might also want the comments:
GitHub Markdown with public comments
An exception can occur while writing to a stream, specifically a WebConnectionStream, for instance while copying from another stream. When this happens, we find that our thread will hang while calling Dispose on the WebConnectionStream because it appears to be waiting on the "pending" ManualResetEvent therein to get signaled, which it never does. We set SendChunked to true, which causes the WebConnectionStream to use the "pending" event to keep track of pending reads/writes so that for instance during close/dispose it can wait before sending the null chunk to indicate that the transfer is complete. If an exception is thrown in the right place, the "pending" event may never get signaled and this would lead to the hung thread. I believe a similar problem can happen if a read fails.
In the following code if an exception is thrown during the CopyStream we will leave the "using" block, and dispose will be automatically called, and then the thread will hang indefinitely in WebConnectionStream.Dispose while calling WaitHandle.WaitOne():
using(Stream requestStream = request.GetRequestStream())
At this time I believe a workaround is to not use the using statement and avoid calling dispose on the stream anywhere else unless there was no write exception. Looking at the mono source code, there does not appear to be any negative implications to not calling Dipose/Close on the stream, at least it doesn't appear to leak any resources.
Upon further analysis it appears that WebConnectionStream::EndWrite does decrement pendingWrites correctly and set the pending event appropriately when an exception is thrown, but when the exception is rethrown WriteCallbackWrapper eats the exception. Since this is all handled asynchronously, the original thread that started the stream writing is not immediately notified of the exception. That thread is allowed to attempt to write at least one additional time before receiving its exception and bailing. The problem here is that now the second write attempt has incremented pendingWrites to 1 and the pending event has now been reset, so that now if Dispose is called it will hang.