Repository navigation
Refactor cancellation and timeout one more time :) - #3318
Conversation
|
One thing I just realized, is that this new system will reset cancellation between operations inside a single command execution. So Instead of the previous ResetCancellation which was called in ExecuteReader, and then wasn't called again in NextResult/Read, we now do the equivalent of ResetCancellation on every call. I'm not sure this has any actual bad consequences - in theory if a PG cancellation is sent during ExecuteReader, we'll forget that fact when we do NextResult and may send it again. But is that really problematic in practice? This also connects to the notes above on user action. We can have another overload of StartCancellableOperation (or whatever) which is subordinate, i.e. only registers the callback but doesn't otherwise reset the state. |
|
(I also don't dispose the registration returned from StartCancellableOperation in command execution... will clean all that up once I get some first feedback from @vonzshik) |
You're calling it for every |
vonzshik
left a comment
There was a problem hiding this comment.
Except that part with the cancellation reset on the reader, I think it's great!
Great, thanks @vonzshik! I'll keep hacking at this then, and request another review when it's done. |
|
Don't forget to edit the commit name on squash. |
|
@vonzshik @YohDeadfall just pushed a lot of work to this PR - I think it should be close to OK. If looks big because it removes all the cancellation tokens from the type handlers etc., but actual changes are less huge.
I tried to do more or less self-contained commits, so you may want to review them one by one - but some stuff leaked between the commits (we'll squash everything anyway). |
|
(need to rebase to make the CI errors go away, but I will wait for you to review etc.) |
vonzshik
left a comment
There was a problem hiding this comment.
Pretty cool, added some notes
|
@vonzshik am going to squash and rebase this, so start seeing how the build does in CI |
71897ee to
013681e
Compare
* The cancellation is no longer passed down the stack to the I/O layer; in all cases cancellation is done by registering a callback on the token. This unifies the flow for PG cancellation and non-PG cancellation. * Introduce notion of cancellable operations, which can contain nested cancellable operations. User actions implicitly represent a cancellable operation, making simple, one-operation cases very easy. * Make the up-front IsCancellationRequested check when starting a cancellable operation. This reduces the ceremony around each and every operation. * Various other refactoring to make the code simpler. Part of npgsql#3166
013681e to
b0e59c9
Compare
| var columnStream = new NpgsqlReadBuffer.ColumnStream(buf); | ||
|
|
||
| // Cancellation is handled at the replication level - we don't want every ReadAsync | ||
| var columnStream = new NpgsqlReadBuffer.ColumnStream(connector, startCancellableOperations: false); |
There was a problem hiding this comment.
@vonzshik FYI this is the one possible exception to the rule about user actions with tokens not having nested operations... The entire replication is a user action with a cancellation token, but if the user is doing physical replication (not logical), they get a Stream back which they can read from (sequentially); so ReadAsync on that may accept cancellation tokens.
This is the only case I'm aware of in Npgsql where it may make sense to have two linked cancellation tokens when reading. As this is a really really niche edge case, I've simply hacked it to ignore the token passed to the stream - you can only cancel via the token passed when starting replication.
OK, I think I finally have something nice working. In a nutshell:
This a draft - @vonzshik I wanted some early feedback from you. If this is a good direction, here's the stuff still to be done by me: