Skip to content
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

ISSUE#416 Fixed Socket.ConnectAsync timeout issue #466

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Docker.DotNet/Microsoft.Net.Http.Client/ManagedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,19 @@ private static async Task<Socket> TCPSocketOpenerAsync(string host, int port, Ca
var s = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
await s.ConnectAsync(address, port).ConfigureAwait(false);
#if (NETSTANDARD1_3 || NETSTANDARD1_6 || NETSTANDARD2_0)
// TODO: This method can be replaced by native ConnectAsync when this functionality is available:
// https://github.com/dotnet/runtime/pull/40750
await s.ConnectAsync(address, port, cancellationToken).ConfigureAwait(false);
#else
await Task.Factory.FromAsync(
s.BeginConnect,
s.EndConnect,
new IPEndPoint(address, port),
null
).ConfigureAwait(false);
#endif

connectedSocket = s;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Net.Http.Client
{
public static class SocketExtensionMethods
{
public static async Task ConnectAsync(this Socket socket, IPAddress address, int port, CancellationToken cancellationToken)
{
Task socketConnectTask = socket.ConnectAsync(address, port);
Task delayTask = Task.Delay(int.MaxValue, cancellationToken);

await await Task.WhenAny(socketConnectTask, delayTask);
}
}
}
Loading