Skip to content

Include IP information in socket.ConnectAsync(endPoint) exceptions #37685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

antonfirsov
Copy link
Member

@antonfirsov antonfirsov commented Jun 10, 2020

Unlike the synchronous variant, the exception message of SocketTaskExtensions.ConnectAsync(...) failures did not include the IP+Port information.

Related to #1326, but does not solve the issue for SocketsHttpHandler. It could be considered as a step towards the long-term solution however (see #1326 (comment)).

@ghost
Copy link

ghost commented Jun 10, 2020

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@antonfirsov antonfirsov requested a review from a team June 10, 2020 01:55
@antonfirsov
Copy link
Member Author

/azp run runtime-libraries outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -82,6 +82,20 @@ public async Task Connect_MultipleIPAddresses_Success(IPAddress listenAt)
}
}

[OuterLoop("Slow on Windows")]
Copy link
Member

@wfurt wfurt Jun 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something like

            using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                listener.Bind(new IPEndPoint(IPAddress.Loopback, 10000));

                Console.WriteLine("Connecting  {0}", DateTime.Now);
                var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    await clientSocket.ConnectAsync(listener.LocalEndPoint);
                } 
                catch(Exception ex)
                {
                    Console.WriteLine("Connect failed {0} {1}", ex.Message, DateTime.Now);

                }
        }

and I get this on Windows 10

Connecting  6/19/2020 10:48:07 AM
Connect failed No connection could be made because the target machine actively refused it. 6/19/2020 10:48:09 AM

it seems like it takes 2s to fail. That does not seems to bad.

@@ -204,5 +218,8 @@ public sealed class ConnectTask : Connect<SocketHelperTask>
public sealed class ConnectEap : Connect<SocketHelperEap>
{
public ConnectEap(ITestOutputHelper output) : base(output) {}

// We should skip this since the exception creation logic is defined in SocketHelperEap.
public override Task Connect_WhenFails_ThrowsSocketExceptionWithIPEndpointInfo(bool useDns) => Task.CompletedTask;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to simply put the test function here?

EndPoint badEndpoint = useDns ? (EndPoint)new DnsEndPoint("localhost", 288) : new IPEndPoint(IPAddress.Loopback, 288);
using Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketException ex = await Assert.ThrowsAnyAsync<SocketException>(() => ConnectAsync(client, badEndpoint));
Assert.Contains("127.0.0.1:288", ex.Message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be safer to bind on anonymous port like the example above. I think that would fit better existing test pattern.

if (!attemptSocket.ConnectAsync(args))
bool pending = attemptSocket.ConnectAsync(args);
// Copy the socket address so we can use it in exception message.
_userArgs!._socketAddress = _internalArgs!._socketAddress;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there reason why we don't use user supplied data? If we were connecting to name resolving to multiple addresses this would log just one, right?

@stephentoub
Copy link
Member

@antonfirsov, are you still working on this?

@antonfirsov
Copy link
Member Author

@stephentoub not right ATM, but I think the change in this PR still makes sense since the exception messages in sync and async connect variants do not match. Unless we state it explicitly that we don't want to spend team capacity for reviews here, I'd like to finish it since it's quite a small change, and comments were only concerned about test code so far.

@stephentoub
Copy link
Member

Ok, it's just been open with no progress for three months now. If all that remains to merge it is someone to review it, let's make that happen and get it merged (though I see @wfurt did review it and has feedback that hasn't been addressed), otherwise let's close it. Thanks.

@antonfirsov
Copy link
Member Author

Closing, since the code has been refactored into SocketAsyncEventArgs.DnsConnectAsync with #43661. However, we may want to consider adding IPEndpoint info to the exceptions created in that method for consistency with the synchronous variant.

@antonfirsov antonfirsov closed this Nov 3, 2020
@geoffkizer
Copy link
Contributor

If we think this is something we should address, let's make sure we have an issue to track it.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
@karelz karelz added this to the 6.0.0 milestone Jan 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants