runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.Net.Sockets.Socket.DoConnect() doesn't work as expected.

Open sirentek opened this issue 11 years ago • 7 comments

Here is an other application which makes the stack trace more clear:

           try
            {
                try
                {
                    socket.Connect(ep);
                }
                catch (SocketException e)
                {
                    => The socket exception is catched here!

                    Console.WriteLine("Excpetion Message=" + e.Message);
                    Console.WriteLine("Excpetion Stack=" + e.StackTrace);

                    Console.WriteLine("Socket Error Code=" + e.SocketErrorCode.ToString());

                    if (e.SocketErrorCode != SocketError.WouldBlock)
                    {
                        throw;
                    }
                }
                var write = new List<Socket> { socket };
                var error = new List<Socket> { socket };
                Socket.Select(null, write, error, perIpTimeout);
                var errorCode = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
                if (errorCode != 0)
                {
                    Console.WriteLine("error code is not zero!");
                    throw new SocketException((int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error));
                }
                if (!write.Any())
                {
                    Console.WriteLine(
                        $"Timeout after {new TimeSpan(perIpTimeout * 10).TotalSeconds} seconds when connecting to {ips[0]}");
                    try { socket.Dispose(); }
                    catch
                    {
                        // ignored
                    }

                    // if i == 0
                    Console.WriteLine("i==0 exception");
                    throw new TimeoutException();

                    // continue;
                }
                socket.Blocking = true;

                return;
            }
            catch (TimeoutException) { throw; }
            catch
            {
                try { socket.Dispose(); }
                catch
                {
                    // ignored
                }

                Console.WriteLine("Failed to connect to " + ips[0]);

                // if (i == 0)

                Console.WriteLine("i==0 exception 2");
                throw;

            }

Here is my output from the above program:

Excpetion Message=Unknown error 10035 Excpetion Stack= at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at SirenTek.Program.Main(String[] args) Socket Error Code=WouldBlock Timeout after -1E-06 seconds when connecting to 192.168.1.72 i==0 exception System.TimeoutException: The operation has timed out.

Socket.DoConnect method creates an exception. This makes us unable to use npgsql with core50 runtime.

sirentek avatar Nov 30 '15 19:11 sirentek

@sirentek is this issue on linux per https://github.com/dotnet/corefx/issues/4631?

joshfree avatar Nov 30 '15 19:11 joshfree

I use https://myget.org/gallery/dotnet-core as feed. System.Net.Sockets v4.1.0-rc2-23530. runtime as coreclr-rc2-16177 linux mint 17.2

@joshfree yes, it still resists..

FYI: I have mono 4.2 installed and I don't get this exception when using mono runtime 1.0.0-beta8 with dnx451.

sirentek avatar Nov 30 '15 19:11 sirentek

There is a very helpful discussion here of the socket issue and how it is affecting Npgsql on coreclr. No solution, but a workaround if you're able to use mono instead. As others have noted, a fix was merged to coreclr some time ago and is slated for milestone rc-2. As of today there are still 73 open issues against milestone rc-2.

kferrio avatar Feb 02 '16 07:02 kferrio

It sounds like this was fixed with dotnet/corefx#4652. If this still repros on current builds, please re-open this issue.

ericeil avatar Feb 10 '16 01:02 ericeil

@ericeil
No, https://github.com/dotnet/corefx/pull/4652 didn't fix it. This bug is very important. It blocks using postgresql in linux environment with coreclr runtime. Could you please re-open it ?

sirentek avatar Feb 11 '16 09:02 sirentek

I had introduced a new bug that affected this code last week, fixed with dotnet/corefx#6075. Now that this is fixed, I'm able to run the following test (derived from your sample code) on both Windows and Ubuntu, successfully:

        [Fact]
        public void Repro4733()
        {
            // Just to make sure a real server is involved, connect to bing.com
            var endPoint = new IPEndPoint(
                new IPAddress(new byte[] { 204, 79, 197, 200 }), 
                80);

            using (var client = new Socket(
                AddressFamily.InterNetwork, 
                SocketType.Stream, 
                ProtocolType.Tcp))
            {
                client.Blocking = false;

                SocketError errorCode;
                try
                {
                    client.Connect(endPoint);
                    errorCode = SocketError.Success;
                }
                catch (SocketException e)
                {
                    errorCode = e.SocketErrorCode;
                    if (errorCode != SocketError.WouldBlock)
                    {
                        throw;
                    }
                }
                Assert.Equal(SocketError.WouldBlock, errorCode);

                var write = new List<Socket> { client };
                var error = new List<Socket> { client };

                Socket.Select(null, write, error, -1);

                Assert.Contains(client, write);

                errorCode = (SocketError)(int)client.GetSocketOption(
                    SocketOptionLevel.Socket, 
                    SocketOptionName.Error);
                Assert.Equal(SocketError.Success, errorCode);
            }
        }

I will consider this closed for now; if you have a more representative test you can give me that still fails, even with the fix for dotnet/corefx#6075, please post it here.

ericeil avatar Feb 12 '16 21:02 ericeil

@ericeil,

I used coreclr-rc2-16397 runtime and it worked for me in linux. Thanks!

sirentek avatar Feb 15 '16 08:02 sirentek