Bug 3374 - HttpWebRequest fails when called from native thread
Summary: HttpWebRequest fails when called from native thread
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-02-10 09:58 UTC by Simon Chisholm
Modified: 2012-03-30 04:54 UTC (History)
4 users (show)

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

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 on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Simon Chisholm 2012-02-10 09:58:05 UTC
HttpWebRequest.GetRequestStream() throws a timeout exception if it is called from a thread that was created by native code (and then PInvokes out to managed code).

The timeout exception is normally preceeded by "_wapi_connect: error looking up socket handle..."

This happens for any use of HttpWebRequest, such as

    Uri uri = new Uri("http://www.google.com");
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST";
    using (Stream stream = request.GetRequestStream ()) {
        Console.WriteLine ("Won't get here if running in a native thread");

The same code works perfectly when run from a thread created in managed code.

I've reproduced this using 2.10.5 and 2.10.6 on Linux x86 & ARM.
Comment 1 Gonzalo Paniagua Javier 2012-03-24 21:25:03 UTC
You don't provide any code, so I am going to assume that you missed this: http://www.mono-project.com/Embedding_Mono#Threads
Comment 2 Lorenzo GIRARDIN 2012-03-30 04:54:22 UTC
Hi all, 
I have posted a portion of code (practically a clone of Simon one) on bug issue #2992. The issue to me happens when running in debug (MonoDev Soft Debugger) on Mono-2.8/2.10/2.10.8, not running the application whithout debug, nor with MS .NET runtime.

@Simon : you state "My problem was fixed by creating a worker thread in managed code and shuffling all uses of HttpWebRequest into that" but it's not clear to me : could you post sample code, please?

Thanks for any suggestion.