Skip to content

Commit

Permalink
ISSUE#416 Fixed Socket.ConnectAsync timeout issue (#466)
Browse files Browse the repository at this point in the history
Co-authored-by: Yurii Pelekh <[email protected]>
Co-authored-by: Gutemberg Ribeiro <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 5bc8c3e commit c0f44a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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);
}
}
}

0 comments on commit c0f44a7

Please sign in to comment.