Sitelet https://github.com/npgsql/npgsql/pull/5664/files
Skip to content
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
52 changes: 39 additions & 13 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,10 +1232,24 @@ internal async Task NegotiateEncryption(SslMode sslMode, NpgsqlTimeout timeout,

void Connect(NpgsqlTimeout timeout)
{
// Note that there aren't any timeout-able or cancellable DNS methods
var endpoints = NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath)
? new EndPoint[] { new UnixDomainSocketEndPoint(socketPath) }
: IPAddressesToEndpoints(Dns.GetHostAddresses(Host), Port);
EndPoint[]? endpoints;
if (NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath))
{
endpoints = [new UnixDomainSocketEndPoint(socketPath!)];
}
else
{
// Note that there aren't any timeout-able or cancellable DNS methods
try
{
endpoints = IPAddressesToEndpoints(Dns.GetHostAddresses(Host), Port);
}
catch (SocketException ex)
{
throw new NpgsqlException(ex.Message, ex);
}
}

timeout.Check();

// Give each endpoint an equal share of the remaining time
Expand Down Expand Up @@ -1302,16 +1316,28 @@ void Connect(NpgsqlTimeout timeout)

async Task ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
{
Task<IPAddress[]> GetHostAddressesAsync(CancellationToken ct) =>
Dns.GetHostAddressesAsync(Host, ct);

// Whether the framework and/or the OS platform support Dns.GetHostAddressesAsync cancellation API or they do not,
// we always fake-cancel the operation with the help of TaskTimeoutAndCancellation.ExecuteAsync. It stops waiting
// and raises the exception, while the actual task may be left running.
var endpoints = NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath)
? new EndPoint[] { new UnixDomainSocketEndPoint(socketPath) }
: IPAddressesToEndpoints(await TaskTimeoutAndCancellation.ExecuteAsync(GetHostAddressesAsync, timeout, cancellationToken).ConfigureAwait(false),
Port);
EndPoint[] endpoints;
if (NpgsqlConnectionStringBuilder.IsUnixSocket(Host, Port, out var socketPath))
{
endpoints = [new UnixDomainSocketEndPoint(socketPath)];
}
else
{
IPAddress[] ipAddresses;
try
{
ipAddresses = await Dns.GetHostAddressesAsync(Host, cancellationToken)
.WaitAsync(timeout.CheckAndGetTimeLeft(), cancellationToken).ConfigureAwait(false);
Comment on lines +1332 to +1333

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@NinoFloris I'm pretty sure before it was calling TaskTimeoutAndCancellation.ExecuteAsync instead of Dns.GetHostAddressesAsync directly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let me put up a PR to pull it out entirely and fix the timeout flowing into the token.

}
catch (SocketException ex)
{
throw new NpgsqlException(ex.Message, ex);
}
endpoints = IPAddressesToEndpoints(ipAddresses, Port);
}

// Give each endpoint an equal share of the remaining time
var perEndpointTimeout = default(TimeSpan);
Expand Down Expand Up @@ -1376,9 +1402,9 @@ Task ConnectAsync(CancellationToken ct) =>
}
}

IPEndPoint[] IPAddressesToEndpoints(IPAddress[] ipAddresses, int port)
EndPoint[] IPAddressesToEndpoints(IPAddress[] ipAddresses, int port)
{
var result = new IPEndPoint[ipAddresses.Length];
var result = new EndPoint[ipAddresses.Length];
for (var i = 0; i < ipAddresses.Length; i++)
result[i] = new IPEndPoint(ipAddresses[i], port);
return result;
Expand Down
33 changes: 33 additions & 0 deletions test/Npgsql.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand Down Expand Up @@ -313,6 +314,38 @@ public void Connect_timeout_cancel()
Assert.That(conn.State, Is.EqualTo(ConnectionState.Closed));
}

[Test]
public void Bad_hostname()
{
using var dataSource = CreateDataSource(csb => csb.Host = "hostname.that.does.not.exist");
using var conn = dataSource.CreateConnection();

Assert.That(
() => conn.Open(),
Throws.Exception
.TypeOf<NpgsqlException>()
.With
.Property(nameof(NpgsqlException.InnerException))
.TypeOf<SocketException>()
);
}

[Test]
public void Bad_hostname_async()
{
using var dataSource = CreateDataSource(csb => csb.Host = "hostname.that.does.not.exist");
using var conn = dataSource.CreateConnection();

Assert.That(
async () => await conn.OpenAsync(),
Throws.Exception
.TypeOf<NpgsqlException>()
.With
.Property(nameof(NpgsqlException.InnerException))
.TypeOf<SocketException>()
);
}

#endregion

#region Client Encoding
Expand Down
2 changes: 1 addition & 1 deletion test/Npgsql.Tests/TracingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public async Task Error_open([Values] bool async)
ActivitySource.AddActivityListener(activityListener);

await using var dataSource = CreateDataSource(x => x.Host = "not-existing-host");
var ex = Assert.ThrowsAsync<SocketException>(async () =>
var ex = Assert.ThrowsAsync<NpgsqlException>(async () =>
{
await using var conn = async
? await dataSource.OpenConnectionAsync()
Expand Down