Bug 24117 - Erroneous timeouts when previous connection attempts fail in web requests
Summary: Erroneous timeouts when previous connection attempts fail in web requests
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: Other Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-10-28 16:39 UTC by David Straw
Modified: 2014-10-29 18:19 UTC (History)
1 user (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 David Straw 2014-10-28 16:39:29 UTC
In our Mono-based application we find that sometimes we see timeouts in our web requests when we have external logging showing that the requests ran to completion well within the timeout configured in the application. This seems to happen when a previous request could not connect to the remote endpoint for some reason, i.e. there is a period of time after a failed connection in which valid web requests do not work properly.

We have seen this behavior on 3.4, 3.6, 3.8 and 3.10. To demonstrate this issue, the program below makes a bunch of requests that should all time out after 10ms, but on Mono the majority of the requests fail quicker than that after only 2-3 initial requests.

Additionally, I see many instances of the following warning on the console when running on Mono:
_wapi_connect: error looking up socket handle 0x4

using System;
using System.Net;
using System.Net.Sockets;

namespace ConnectTimeoutTest
    class Program
        static void Main(string[] args)
            const string ipAddress = "";
            const int port = 8000;
            string uri = string.Format("http://{0}:{1}/", ipAddress, port);

            // Open up a listening socket, but don't respond to any connections. This causes callers to eventually time out.
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));

            Console.WriteLine("Listing on {0}:{1}", ipAddress, port);

            const int timeoutMs = 10;

            // Repeatedly make requests to the endpoint configured above
            while (true)
                var start = DateTime.Now;
                    var client = (HttpWebRequest)WebRequest.Create(uri);
                    client.Timeout = timeoutMs;
                catch (WebException ex)
                    var end = DateTime.Now;

                    // We expect timeouts, but if less time has passed than we configured for the timeout, it
                    // means the framework threw the error without waiting. This never happens on .NET but
                    // it happens frequently on Mono.
                    if ((end - start).TotalMilliseconds < timeoutMs)
Comment 1 David Straw 2014-10-29 14:02:50 UTC
After further testing with variants of the test case I posted, I don't think I have properly reproduced the issue after all, though the code certainly does expose some potential issues.

This doesn't change the original statements I made about the behavior we see in our application, but I will have to think of another way to reproduce the problem.
Comment 2 David Straw 2014-10-29 18:19:04 UTC
Closing this because I had made some wrong assumptions. While the warnings in the console output and the fact that timeouts occur a little faster than expect concern me a little bit, those particular issues are not blocking our application.

If I can get a better handle on what's going on in our application I will file a new defect.