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
It seems that .NET Core 3.0 finally honors cancellation tokens at the socket layer - https://github.com/dotnet/corefx/issues/24430. We should review what we currently do with cancellation (token vs. DbCommand.Cancel()) and consider what to do. One possibility would be to simply pass the cancellation token to the socket layer (but we need to understand the state of things after such a cancellation occurs), and to keep DbCommand.Cancel() for the PostgreSQL-specific cancellation mechanism (via a second connection).
We should probably keep the current behavior or doing PG cancellation, but consider adding a frontend cancellation (i.e. close the socket) if nothing happens for a period of time. This would solve the case of a network partition, and would be similar to what Trigger backend cancellation when a (frontend) timeout occurs #1567 proposes for timeouts.
@YohDeadfall I don't understand what you mean. One point of PostgreSQL cancellation is to be able to continue using the socket (no closing), and to terminate the query on the server side (which just closing the socket would not achieve). Also, unless I'm mistaken any sort of socket-level cancellation (i.e. https://github.com/dotnet/corefx/issues/35012#issuecomment-483779404) would probably leave the connection in an unusable state, because some data has been written/read and we probably lose protocol sync - although I'm not sure about this, we need to investigate.
The more I think about it, the more I think there really are two types of cancellations:
Server-side PostgreSQL cancellation, via a second physical connection - like today. This is already triggered via NpgsqlCommand.Cancel(), or by triggering the async cancellation token. The disadvantage is that this isn't immediate, and there's no guarantee it would actually work (e.g. network partition).
Client-side "cancellation" , at the socket level - this always works, immediately. If what I wrote above is correct, then cancelling the socket operation wouldn't really have any advantage over... just closing the connection.
If the socket is killed client-side, then the PostgreSQL backend process will die as soon as it reads from its own side - which would be after the current query completed. If you want to kill the server process immediately, the 1st type of cancellation is required above.
Basically the 1st kind of cancellation is almost always better - unless there's a network partition, in which case the 2nd kind (which isn't exactly a cancellation) is the only way which would release things. I'm not sure whether we should be providing an API to do automatically try the 1st, and forcibly close the connection after a certain amount of time (or whether this should be implemented by the user).
Also, not that we currently don't support forcible client-side socket closing well - there are some race conditions around that...
Thanks for the explanation. I meant that we can use the same connector for which the socket was canceled to ensure that the backend was stopped and help it if it doesn't.
I'm currently thinking about implementing a client side timeout in streaming replication via CancellationTokenSource.CancelAfter which would obviously depend on passing an optional CancellationToken to asynchronous read operations.
This is a kind of special case because a streaming replication connection is a really long-lived connection where recycling of the socket or connector is pretty much a non-issue. @roji and @YohDeadfall, are there any objections in pursuing this or would you rather direct me in another direction?
As described in #3052 (comment) we have a problem with cancellation. Even if there is a pooled connection it's better to close the socket and lost the connection than terminate another command.
So @YohDeadfall, if I understand this correctly, you are for pursuing cancellation of async read operations via CancellationToken (and by that killing the socket/connector), because the PostrgeSQL way of cancelling commands via a separate connection leads to issues in our code base.
@Brar this sounds right to me - we indeed want an exception to be automatically raised if the network is cut, otherwise read operations (i.e. IAsyncEnumerator.MoveNextAsync) will block forever).
@YohDeadfall this isn't about user-triggered cancellation, but about (socket) timeout, so it's more similar to #607 (definitely not about PG cancellation via a separate connector etc.). Note also that we're talking about replication connections, which are special - these aren't normal connections where the user is executing commands, and where you can accidentally cancel the wrong command etc.
Note that the user will still have the option of passing a cancellation token to the entire replication enumeration process, so it should be possible for them to stop the replication at any point as well.
After reading a bit in #2911 I came to the conclusion that I should probably wait for this PR to get merged before going on with the client side timeout in streaming replication.
Although #2911 will not provide the functionality really needed for this (see #2911 (comment)), it lays some ground and is probably too closely related to conveniently merge changes after it was merged into dev.
In the end we'll probably need to take an additional (optional) CancellationToken as an argument to our internal async methods and link it with the one introduced by #2911
If I'm understanding correctly, plumbing the cancellation token into NpgsqlReadBuffer.Ensure would (for now) only be for the purposes of replication cancellation, right? Regular command execution would continue as today, wiring up the cancellation token to trigger a PostgreSQL server cancellation instead?
@YohDeadfall I'm not sure about saving the token as state variable (on NpgsqlReadBuffer?)... the standard is to pass it down the stack. Handlers can simply pass the default token when doing Ensures.
@YohDeadfall the point with async enumerators is that the same cancellation token applies to the entire enumeration (or enumerator). When you do ExecuteReader, the cancellation token is only valid for that call - once it completes it isn't valid (other async calls on the returned reader accept other tokens).
Instead of passing it through all methods, just save it as a part of the current state and use it internally. The token is used only by IO and not by handlers or other stuff to read data.
I'd prefer to stick with the common CancellationToken cancellationToken = default method argument.
It is pretty much standard across .NET and as a simple-minded person I hate global state.
If I'm understanding correctly, plumbing the cancellation token into NpgsqlReadBuffer.Ensure would (for now) only be for the purposes of replication cancellation, right? Regular command execution would continue as today, wiring up the cancellation token to trigger a PostgreSQL server cancellation instead?
Not necessarily. It depends, whether we already agree on the way forward.
I'd prefer to stick with the common CancellationToken cancellationToken = default method argument.
It is pretty much standard across .NET and as a simple-minded person I hate global state.
I also agree with this because I'm also a simple-minded person 😄
Activity
roji commentedon Apr 16, 2019
See discussion in https://github.com/dotnet/corefx/issues/35012#issuecomment-483779404
YohDeadfall commentedon Apr 17, 2019
With that feature cancellation can be done on the same connection after closing the socket.
roji commentedon Apr 17, 2019
@YohDeadfall I don't understand what you mean. One point of PostgreSQL cancellation is to be able to continue using the socket (no closing), and to terminate the query on the server side (which just closing the socket would not achieve). Also, unless I'm mistaken any sort of socket-level cancellation (i.e. https://github.com/dotnet/corefx/issues/35012#issuecomment-483779404) would probably leave the connection in an unusable state, because some data has been written/read and we probably lose protocol sync - although I'm not sure about this, we need to investigate.
The more I think about it, the more I think there really are two types of cancellations:
NpgsqlCommand.Cancel(), or by triggering the async cancellation token. The disadvantage is that this isn't immediate, and there's no guarantee it would actually work (e.g. network partition).YohDeadfall commentedon Apr 17, 2019
Would it immediately kill the corresponding backend? I saw situations when a fronted died/crashed, but the backend kept working.
roji commentedon Apr 17, 2019
If the socket is killed client-side, then the PostgreSQL backend process will die as soon as it reads from its own side - which would be after the current query completed. If you want to kill the server process immediately, the 1st type of cancellation is required above.
Basically the 1st kind of cancellation is almost always better - unless there's a network partition, in which case the 2nd kind (which isn't exactly a cancellation) is the only way which would release things. I'm not sure whether we should be providing an API to do automatically try the 1st, and forcibly close the connection after a certain amount of time (or whether this should be implemented by the user).
Also, not that we currently don't support forcible client-side socket closing well - there are some race conditions around that...
YohDeadfall commentedon Apr 17, 2019
Thanks for the explanation. I meant that we can use the same connector for which the socket was canceled to ensure that the backend was stopped and help it if it doesn't.
Brar commentedon Jul 9, 2020
I'm currently thinking about implementing a client side timeout in streaming replication via
CancellationTokenSource.CancelAfterwhich would obviously depend on passing an optionalCancellationTokento asynchronous read operations.This is a kind of special case because a streaming replication connection is a really long-lived connection where recycling of the socket or connector is pretty much a non-issue.
@roji and @YohDeadfall, are there any objections in pursuing this or would you rather direct me in another direction?
YohDeadfall commentedon Jul 9, 2020
As described in #3052 (comment) we have a problem with cancellation. Even if there is a pooled connection it's better to close the socket and lost the connection than terminate another command.
Brar commentedon Jul 10, 2020
So @YohDeadfall, if I understand this correctly, you are for pursuing cancellation of async read operations via
CancellationToken(and by that killing the socket/connector), because the PostrgeSQL way of cancelling commands via a separate connection leads to issues in our code base.Right?
roji commentedon Jul 10, 2020
@Brar this sounds right to me - we indeed want an exception to be automatically raised if the network is cut, otherwise read operations (i.e. IAsyncEnumerator.MoveNextAsync) will block forever).
@YohDeadfall this isn't about user-triggered cancellation, but about (socket) timeout, so it's more similar to #607 (definitely not about PG cancellation via a separate connector etc.). Note also that we're talking about replication connections, which are special - these aren't normal connections where the user is executing commands, and where you can accidentally cancel the wrong command etc.
Note that the user will still have the option of passing a cancellation token to the entire replication enumeration process, so it should be possible for them to stop the replication at any point as well.
Brar commentedon Jul 10, 2020
After reading a bit in #2911 I came to the conclusion that I should probably wait for this PR to get merged before going on with the client side timeout in streaming replication.
Although #2911 will not provide the functionality really needed for this (see #2911 (comment)), it lays some ground and is probably too closely related to conveniently merge changes after it was merged into dev.
In the end we'll probably need to take an additional (optional)
CancellationTokenas an argument to our internal async methods and link it with the one introduced by #29116 remaining items
roji commentedon Aug 18, 2020
If I'm understanding correctly, plumbing the cancellation token into NpgsqlReadBuffer.Ensure would (for now) only be for the purposes of replication cancellation, right? Regular command execution would continue as today, wiring up the cancellation token to trigger a PostgreSQL server cancellation instead?
@YohDeadfall I'm not sure about saving the token as state variable (on NpgsqlReadBuffer?)... the standard is to pass it down the stack. Handlers can simply pass the default token when doing Ensures.
YohDeadfall commentedon Aug 18, 2020
Async enumerators is a good example of storing the passed token as a part of a state machine.
roji commentedon Aug 18, 2020
@YohDeadfall the point with async enumerators is that the same cancellation token applies to the entire enumeration (or enumerator). When you do ExecuteReader, the cancellation token is only valid for that call - once it completes it isn't valid (other async calls on the returned reader accept other tokens).
Brar commentedon Aug 18, 2020
I'd prefer to stick with the common
CancellationToken cancellationToken = defaultmethod argument.It is pretty much standard across .NET and as a simple-minded person I hate global state.
Not necessarily. It depends, whether we already agree on the way forward.
roji commentedon Sep 6, 2020
See overall discussion on cancellation and timeout: #1567 (comment)
roji commentedon Sep 6, 2020
I also agree with this because I'm also a simple-minded person 😄
YohDeadfall commentedon Sep 6, 2020
FYI, already supported the idea in the new type handlers which hope will publish soon.
roji commentedon Sep 17, 2020
@vonzshik assigning to you as you expressed an interest in this (#3142 (comment))
Passing the cancellation token to the ReadBuffer (#3158)
[-]Reexamine cancellation[/-][+]Implement async cancellation[/+]Passing the cancellation token to the write buffer (#3163)
Passing the cancellation token from the command to the read and write…