X Tutup
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
20 changes: 15 additions & 5 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ internal bool PostgresCancellationPerformed
internal bool UserCancellationRequested => _userCancellationRequested;
internal CancellationToken UserCancellationToken { get; set; }
internal bool AttemptPostgresCancellation { get; private set; }
static readonly TimeSpan _cancelImmediatelyTimeout = TimeSpan.FromMilliseconds(-1);
static readonly TimeSpan _cancelImmediatelyTimeout = TimeSpan.Zero;

static readonly SslApplicationProtocol _alpnProtocol = new("postgresql");

Expand Down Expand Up @@ -2040,6 +2040,8 @@ void PerformUserCancellationUnsynchronized()
var cancellationTimeout = Settings.CancellationTimeout;
if (PerformPostgresCancellation() && cancellationTimeout >= 0)
{
// TODO: according to docs, we treat 0 timeout as infinite, yet we do not change the actual value
// We should revisit this here and in NpgsqlReadBuffer
if (cancellationTimeout > 0)
{
ReadBuffer.Timeout = TimeSpan.FromMilliseconds(cancellationTimeout);
Expand Down Expand Up @@ -2760,8 +2762,9 @@ UserAction DoStartUserAction(ConnectorState newState, NpgsqlCommand? command,

// We reset the ReadBuffer.Timeout for every user action, so it wouldn't leak from the previous query or action
// For example, we might have successfully cancelled the previous query (so the connection is not broken)
// But the next time, we call the Prepare, which doesn't set it's own timeout
ReadBuffer.Timeout = TimeSpan.FromSeconds(command?.CommandTimeout ?? Settings.CommandTimeout);
// But the next time, we call the Prepare, which doesn't set its own timeout
var timeoutSeconds = command?.CommandTimeout ?? Settings.CommandTimeout;
ReadBuffer.Timeout = timeoutSeconds > 0 ? TimeSpan.FromSeconds(timeoutSeconds) : Timeout.InfiniteTimeSpan;
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.

Just confirming: from the user's perspective, we're retaining the meaning of CommandTimeout=0 to mean infinite/no timeout, right? (both in the connection string and in NpgsqlCommand.CommandTimeout). In those contexts, and "immediate timeout" makes no sense in any case.

(It's just slightly confusing that we have two contexts/meanings: one user-facing one where 0 means infinite and another internal one where it means immediate. I get why it's like this and it makes sense - maybe good to make it slightly clearer in comments here and there)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just confirming: from the user's perspective, we're retaining the meaning of CommandTimeout=0 to mean infinite/no timeout, right? (both in the connection string and in NpgsqlCommand.CommandTimeout). In those contexts, and "immediate timeout" makes no sense in any case.

Correct, that's why we pass Timeout.InfiniteTimeSpan if command's timeout is 0, since now instead of NpgsqlReadBuffer doing that for us, we have to do that ourselves at each call. Not ideal but then our COPY operations allow an essentially direct access to read buffer's timeout, and it's only two places, so...


return new UserAction(this);
}
Expand Down Expand Up @@ -2896,12 +2899,15 @@ internal async Task<bool> Wait(bool async, int timeout, CancellationToken cancel
await Flush(async, cancellationToken).ConfigureAwait(false);

var keepaliveMs = Settings.KeepAlive * 1000;
var isTimeoutInfinite = timeout <= 0;
while (true)
{
cancellationToken.ThrowIfCancellationRequested();

var timeoutForKeepalive = _isKeepAliveEnabled && (timeout <= 0 || keepaliveMs < timeout);
ReadBuffer.Timeout = TimeSpan.FromMilliseconds(timeoutForKeepalive ? keepaliveMs : timeout);
var timeoutForKeepalive = _isKeepAliveEnabled && (isTimeoutInfinite || keepaliveMs < timeout);
ReadBuffer.Timeout = timeoutForKeepalive
? TimeSpan.FromMilliseconds(keepaliveMs)
: isTimeoutInfinite ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(timeout);
try
{
var msg = await ReadMessageWithNotifications(async).ConfigureAwait(false);
Expand Down Expand Up @@ -2961,7 +2967,11 @@ internal async Task<bool> Wait(bool async, int timeout, CancellationToken cancel
}

if (timeout > 0)
{
timeout -= (keepaliveMs + (int)Stopwatch.GetElapsedTime(keepaliveStartTimestamp).TotalMilliseconds);
// Make sure we don't accidentally set -1 as a timeout (because it's infinite)
timeout = Math.Max(timeout, 0);
}
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/Npgsql/Internal/NpgsqlReadBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,16 @@ sealed partial class NpgsqlReadBuffer : IDisposable
internal ResettableCancellationTokenSource Cts { get; }
readonly MetricsReporter? _metricsReporter;

TimeSpan _preTranslatedTimeout = TimeSpan.Zero;

/// <summary>
/// Timeout for sync and async reads
/// </summary>
internal TimeSpan Timeout
{
get => _preTranslatedTimeout;
get => Cts.Timeout;
set
{
if (_preTranslatedTimeout != value)
if (Cts.Timeout != value)
{
_preTranslatedTimeout = value;

if (value == TimeSpan.Zero)
value = InfiniteTimeSpan;
else if (value < TimeSpan.Zero)
value = TimeSpan.Zero;

Debug.Assert(_underlyingSocket != null);

_underlyingSocket.ReceiveTimeout = (int)value.TotalMilliseconds;
Expand Down Expand Up @@ -189,7 +180,7 @@ int ReadWithTimeout(Span<byte> buffer)

async ValueTask<int> ReadWithTimeoutAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
var finalCt = Timeout != TimeSpan.Zero
var finalCt = Timeout != InfiniteTimeSpan
? Cts.Start(cancellationToken)
: Cts.Reset();

Expand Down Expand Up @@ -289,7 +280,7 @@ static async ValueTask EnsureLong(
buffer.ReadPosition = 0;
}

var finalCt = async && buffer.Timeout != TimeSpan.Zero
var finalCt = async && buffer.Timeout != InfiniteTimeSpan
? buffer.Cts.Start()
: buffer.Cts.Reset();

Expand Down
X Tutup