Skip to content

Throw OperationCancelledException with correct CancellationToken from Dns.GetHostAddressesAsync#6364

Merged
vonzshik merged 2 commits intomainfrom
6362-dns-throw-oce-with-correct-token-fix
Dec 9, 2025
Merged

Throw OperationCancelledException with correct CancellationToken from Dns.GetHostAddressesAsync#6364
vonzshik merged 2 commits intomainfrom
6362-dns-throw-oce-with-correct-token-fix

Conversation

@vonzshik
Copy link
Contributor

@vonzshik vonzshik commented Dec 8, 2025

Fixes #6362

@vonzshik vonzshik requested a review from roji as a code owner December 8, 2025 16:11
}
catch (OperationCanceledException oce) when (
oce.CancellationToken == combinedToken && !cancellationToken.IsCancellationRequested)
catch (OperationCanceledException)
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if there's value in bubbling up the specific OperationCanceledException thrown by Dns.GetHostAddressesAsync, rather than a new one generated by ThrowIfCancellationRequested below (I think that was probably the thinking behind the current logic...)... I'm not sure there is.

But to keep the behavior that way, it may be a bit better to just keep the !cancellationToken.IsCancellationRequested in the when for the timeout (or am I misunderstanding the issue)?

In any case, I'm also OK with the change as-is and will approve.

Copy link
Contributor Author

@vonzshik vonzshik Dec 9, 2025

Choose a reason for hiding this comment

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

The issue is that OperationCancelledException has a property CancellationToken. And because we use CancellationTokenSource the token there will not be the one user passed to NpgsqlConnection.OpenAsync, but from that CancellationTokenSource. Most of the time no one really cares what token it has, but in our case we ourselves actually do care:

catch (OperationCanceledException oce) when (cancellationToken.IsCancellationRequested && oce.CancellationToken == cancellationToken)
{
if (connector is not null)
pool.Return(connector);
throw;
}

Copy link
Member

Choose a reason for hiding this comment

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

OK. Am on vacation and don't have time to review more deeply, and in any case the change looks OK, just a bit odd. No problem for merging.

@vonzshik vonzshik merged commit 19863c1 into main Dec 9, 2025
17 checks passed
@vonzshik vonzshik deleted the 6362-dns-throw-oce-with-correct-token-fix branch December 9, 2025 09:58
vonzshik added a commit that referenced this pull request Dec 9, 2025
… Dns.GetHostAddressesAsync (#6364)

Fixes #6362

(cherry picked from commit 19863c1)
@vonzshik
Copy link
Contributor Author

vonzshik commented Dec 9, 2025

Backported to 10.0.1 via aa0e138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong exception thrown when cancellation request while opening a aconnection

2 participants