You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added info: a test with SqlClient shows clearly that the old {Begin,End}Execute* methods ignore CommandTimeout while the modern ExecuteAsync methods do enforce it.
More added info: it seems that NetworkStream does not support timeouts or cancellation tokens, so it doesn't seem like this is implementable... I wonder what exactly SqlClient does.
using Dapper;
private static async Task Test()
{
try
{
using (Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection("Server=localhost;Port=5432;User Id=my_user;Password=testpw;Database=animerecs_small;Timeout=5;Command Timeout=3"))
{
Console.WriteLine("Connecting to PostgreSQL.");
await conn.OpenAsync().ConfigureAwait(false);
Console.WriteLine("Connected to PostgreSQL.");
Console.WriteLine("Sleeping...");
await conn.ExecuteAsync("SELECT pg_sleep(10)").ConfigureAwait(false); // does not throw
//conn.Execute("SELECT pg_sleep(10)"); // throws exception due to timeout
Console.WriteLine("Done sleeping");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
I tried to do a timeout "manually" using a cancellation token and that resulted in a PostgresException "57014: canceling statement due to user request" instead of the OperationCanceledException/TaskCanceledException I was expecting. I guess if there was a broken network connection it would not work because it would be trying to communicate with the server?
using Dapper;
private static async Task Test()
{
try
{
using (Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection("Server=localhost;Port=5432;User Id=my_user;Password=my_password;Database=my_database;Timeout=5;Command Timeout=3"))
{
Console.WriteLine("Connecting to PostgreSQL.");
await conn.OpenAsync().ConfigureAwait(false);
Console.WriteLine("Connected to PostgreSQL.");
Console.WriteLine("Sleeping...");
using (CancellationTokenSource timeout = new CancellationTokenSource(TimeSpan.FromSeconds(3)))
{
CommandDefinition cmd = new CommandDefinition("SELECT pg_sleep(120)", commandTimeout: 10000, cancellationToken: timeout.Token);
try
{
await conn.ExecuteAsync(cmd).ConfigureAwait(false);
}
catch (OperationCanceledException ex)
{
// This is not entered, the exception is a PostgresException
if (timeout.IsCancellationRequested)
{
Console.WriteLine("Timed out");
}
else
{
throw;
}
}
Console.WriteLine("Done sleeping");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
@LHCGreg yes, this is still an outstanding issue - frontend (i.e. socket) timeouts for async methods isn't supported. And yes, a backend timeout (which is what you triggered with the cancellation token) will not have any effect in case of loss of network connectivity. Unfortunately this is the result of a lack of support in .NET - NetworkStream does not honor socket timeout, nor does it support cancellation.
The only possibility here is for Npgsql to close the socket - this would "cancel" the operation but also close the connection. In theory we could hook up the cancellation token to do this.
@roji I was just asking because this issue is marked as closed. I found a few similar issues: #633, #1265, #1344, #1567.
In my particular case I am connecting to a database on localhost and have no plans to ever separate the app from the DB, so I guess backend timeouts are good enough for me - the OS should cause socket exceptions to occur if the other side of the connection dies, so I don't have to worry about operations waiting forever.
Waiting for the fix!
We have faced the problem (and spent a lot of time to find it) that async methods (ToListAsync for example) don't throw the exception on timeout - just hang (if there is a problem in database and the query takes long time).
After googling and searching through the code we've found:
// TODO: Socket.ReceiveTimeout doesn't work for async.
Activity
roji commentedon Apr 20, 2016
Added info: a test with SqlClient shows clearly that the old {Begin,End}Execute* methods ignore CommandTimeout while the modern ExecuteAsync methods do enforce it.
roji commentedon Apr 24, 2016
More added info: it seems that NetworkStream does not support timeouts or cancellation tokens, so it doesn't seem like this is implementable... I wonder what exactly SqlClient does.
Emill commentedon Apr 24, 2016
SqlClient uses some other native stuff than a normal Socket...
roji commentedon Apr 24, 2016
You mean even for TCP connections to SqlServer? Could be...
Emill commentedon Apr 24, 2016
Yes, check out
http://referencesource.microsoft.com/#System.Data/System/Data/SqlClient/TdsParser.cs,67b2c9cce4167aab
roji commentedon Apr 24, 2016
Bah... I guess this is also why they can implements timeouts without breaking the connection... Not fair :)
LHCGreg commentedon Aug 10, 2017
@roji This is still an outstanding issue right?
I tried to do a timeout "manually" using a cancellation token and that resulted in a PostgresException "57014: canceling statement due to user request" instead of the OperationCanceledException/TaskCanceledException I was expecting. I guess if there was a broken network connection it would not work because it would be trying to communicate with the server?
roji commentedon Aug 11, 2017
@LHCGreg yes, this is still an outstanding issue - frontend (i.e. socket) timeouts for async methods isn't supported. And yes, a backend timeout (which is what you triggered with the cancellation token) will not have any effect in case of loss of network connectivity. Unfortunately this is the result of a lack of support in .NET - NetworkStream does not honor socket timeout, nor does it support cancellation.
The only possibility here is for Npgsql to close the socket - this would "cancel" the operation but also close the connection. In theory we could hook up the cancellation token to do this.
LHCGreg commentedon Aug 13, 2017
@roji I was just asking because this issue is marked as closed. I found a few similar issues: #633, #1265, #1344, #1567.
In my particular case I am connecting to a database on localhost and have no plans to ever separate the app from the DB, so I guess backend timeouts are good enough for me - the OS should cause socket exceptions to occur if the other side of the connection dies, so I don't have to worry about operations waiting forever.
[-]Frontend timeouts for async methods?[/-][+]Frontend timeouts for async methods[/+]roji commentedon Oct 14, 2019
Reopening as https://github.com/dotnet/corefx/issues/24430 added cancellation token support to socket operations, which we can use to implement timeouts (see also https://github.com/dotnet/corefx/issues/9698).
freerider7777 commentedon Mar 27, 2020
Waiting for the fix!
We have faced the problem (and spent a lot of time to find it) that async methods (ToListAsync for example) don't throw the exception on timeout - just hang (if there is a problem in database and the query takes long time).
After googling and searching through the code we've found:
in NpgsqlConnector :)
https://github.com/npgsql/npgsql/blob/572918fc3a94cf201ba947dac341d1a1de38f7b9/src/Npgsql/NpgsqlConnector.cs
freerider7777 commentedon Mar 27, 2020
HttpClient can be used as the sample I think:
https://github.com/dotnet/runtime/blob/4b03513e20e0caf9ee34817eb74356903f1bb33e/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs
roji commentedon Mar 27, 2020
Yeah, we should definitely do this at some point. If someone wants to take a stab at this, please do so.
YohDeadfall commentedon Jun 24, 2020
@vonzshik I need your comment here to assign the issue.
vonzshik commentedon Jun 24, 2020
@YohDeadfall I'm here
Frontend timeouts for async methods (#2911)