Skip to content

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

Closed

Description

@sirentek

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.

Activity

  1. sirentek commented on Nov 30, 2015

    @sirentek
    Author

    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.

  2. assigned and unassigned on Jan 5, 2016
  3. kferrio commented on Feb 2, 2016

    @kferrio

    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.

  4. ericeil commented on Feb 10, 2016

    @ericeil
    Contributor

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

  5. sirentek commented on Feb 11, 2016

    @sirentek
    Author

    @ericeil
    No, dotnet/corefx#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 ?

  6. ericeil commented on Feb 12, 2016

    @ericeil
    Contributor

    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.

  7. added a commit that references this issue on Feb 12, 2016
  8. sirentek commented on Feb 15, 2016

    @sirentek
    Author

    @ericeil,

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

  9. transferred this issue fromdotnet/corefxon Jan 31, 2020
  10. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  11. ghost locked as resolved and limited conversation to collaborators on Jan 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Netos-linux

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions