Skip to content

Passing the cancellation token to the write buffer - #3163

Merged
roji merged 10 commits into
npgsql:mainfrom
vonzshik:WritingCancellationToken
Sep 21, 2020
Merged

roji merged 10 commits into
npgsql:mainfrom
vonzshik:WritingCancellationToken

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Related to #2437.

@Brar Brar left a comment

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.

This looks pretty good to me.
One minor oversight with respect to coding style and one part that I don't really understand which is probably my fault.

It's funny how the CancellationToken-PRs always boil down to a gazillion lines of monkey code where all you do is adding and handing over cancellation tokens and being extremely careful not to miss a place where we go async and one single method with actual implementation changes (NpgsqlWriteBuffer.FlushAsync() in this case).

Comment thread src/Npgsql/NpgsqlWriteBuffer.Stream.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@vonzshik
vonzshik requested a review from Brar September 20, 2020 09:08
@roji

roji commented Sep 20, 2020

Copy link
Copy Markdown
Member

@vonzshik if you rebase this on the latest main, it should fully pass the build (I fixed something).

@vonzshik
vonzshik force-pushed the WritingCancellationToken branch from 78c09dd to d547c40 Compare September 20, 2020 09:14
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@roji roji mentioned this pull request Sep 20, 2020
16 tasks done

@roji roji left a comment

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.

Thanks @vonzshik, looks very close - please see some questions below.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlWriteBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
}
catch (OperationCanceledException e)
{
// User requested the cancellation - ignoring the exception, as it's working as expected,

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.

I'm also a bit confused by this (like @Brar was) 🤣

Is there any reason to handle things this way, rather than just let the exception bubble naturally? If cancellation (or timeout) is triggered during write, the connector gets broken inside Flush - just like any other I/O error which could occur, no?

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.

I'm also a bit confused by this (like @Brar was) 🤣

That's perfectly normal, this case is definitely not trivial 😁

So, user is calling NpgsqlCommand.ExecuteReaderAsync, and there happens 3 relevant things:

  • The send task is 'send' (may be sync or async).
  • Sync part of the send task is checked for an exception.
  • NpgsqlDataReader is initialized and we're calling for the next result set.

After the sync part of the send task is checked, the only time user can get the exception from it is when NpgsqlDataReader is closed (which in case of the cancellation would be when the connection is disposed).

The exception path should look like this:
ReadBuffer->Connector->DataReader->Command->orm/user

But, if we don's swallow the exception from the send task, we also would get it while disposing the connection.

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.

In simpler terms, send task runs parallel to the main thread (which tries to read from the ReadBuffer). So, any exception from the ReadBuffer will automatically bubble up to the user, but an exception from send task can be observed only when we're closing the reader. Which, in case when we get an exception from the ReadBuffer, is when we dispose the connection.

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.

Sounds good, you certainly have a handle on this complex piece of code - I forgot the important point that only the sync part of the sending happens here. This is related to #1323, which is a very old issue on how to handle later exceptions occurring in the async phase of the write (of which cancellation would be an example). As in that issue, in practice that's not really a problem since almost all write exceptions will also cause the connection to break.

One note though... you're specifically swallowing OperationCanceledException, but as I wrote above any I/O exception could be generated in the async write phase, causing an exception to be raised both from ExecuteReader and from the read side. In other words, unless I'm mistaken, cancellation is just a specific case in a general problem of exceptions during write; I'm not sure we should make an attempt to swallow only OperationCanceledException. Unless I've missed something and cancellation is special, it's probably better to not swallow for now and possibly revisit as part of #1323. If we do keep swallowing, we should probably emit a Debug log message.

Am approving this and am OK with merging either way, but tell me what you think about the above (and consider removing the swallowing).

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.

Well, that's definitely not a simple problem.

My thoughts:

  • As a user, I would like to have as much information about an error as possible. And that means, to have an exception from the read side and an exception from the write side. But as the only way to do this is to use something akin to AggregateException (and it's a nightmare to deal with it), there is nothing we can do.
  • The only alternative to swallowing, is to return the exception to the user (it would be a second OperationCancelledException and it also would be while disposing the connection). Also not an option.
  • I actually do think cancellation is special, because we're the source for it. We've requested it, we've got the OperationCancelledException and we've ignored it as it serves no purpose but to confirm that we were successful. Also a reason why I don't see a point in logging it - there would be a point, when we have too many messages to deal with. But, I guess it's normal for the debug level logs.

I've added the swallowing for every exception (and logging it to the Debug log). But I do agree with you, it's far from an ideal solution and should be revisited as part of #1323.

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.

@vonzshik I've merged the PR to unblock us - after all this remaining discussion isn't essential (and #1323 exists to track all these things).

I actually do think cancellation is special, because we're the source for it. We've requested it, we've got the OperationCancelledException and we've ignored it as it serves no purpose but to confirm that we were successful.

I'm not sure I follow this... The standard .NET behavior is for this exception to get thrown to the user, even if though they requested the cancellation - this is the way of confirming that the cancellation was successful. In any case, I'm not sure why the above should mean that we should swallow this specific exception type but not others - after all a OperationCancelledException does get thrown (as it should), the question is only how/when it should be thrown.

Also a reason why I don't see a point in logging it - there would be a point, when we have too many messages to deal with. But, I guess it's normal for the debug level logs.

Yeah - this is only for extra debug information in case the mechanism doesn't work as well as we think. It mainly doesn't hurt.

There's one thing I still don't understand about the current implementation as it is. When cancellation (or any I/O exception) happens during later (async) writing, the connector breaks. As part of that the reader gets closed, the exception gets thrown and swallowed when we await _sendTask, and that's it. Now, when the user actually attempts to read something from the reader, don't they just get "reader is closed" behavior, without any OperationCancelledException ever bubbling up? For example, if they call NpgsqlDataReader.Read right after the write cancellation occurred, they'll just get a false return value, indicating that no more rows are available - which seems quite bad. Bubbling up the exception from _sendTask may be the only sane thing to do, at least at this point. Am I missing something, what do you think?

More long-term, for correct behavior I don't think there's a way to avoid minimal synchronization between writing and reading - which happens completely concurrently. We may need a special "broken by writing" state which the reader identifies, and which causes it to surface the original breaking exception etc. But that's definitely out of scope here.

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.

Actually, I just realized that whether we swallow _sendTask exceptions or not doesn't matter (for the purpose of exposing the exception to the user)... Since the reader closing is called when breaking the connector, any exceptions bubbling up from there NpgsqlDataReader.Close would be swallowed instead by NpgsqlConnector.Cleanup, so the user wouldn't see it anyway. It's good to swallow as you've added, because this way at least the NpgsqlDataReader state is set correctly.

It would be good to hear your opinion of the above - we may want to address #1323 earlier than expected.

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.

I think the most important thing is, we both agree that the swallowing is not a longterm solution. The discussion about the OperationCancelledException doesn't really matter until we solve the problem with the send task.

I've done some digging around exception handling for the ReadBuffer and the WriteBuffer. I'll post my findings at #1323.

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.

Agree, looking forward to seeing your thoughts in #1323, and your next PR for cancellation.

@roji
roji merged commit 48273d7 into npgsql:main Sep 21, 2020
@vonzshik
vonzshik deleted the WritingCancellationToken branch September 28, 2020 06:48
@roji roji mentioned this pull request Nov 15, 2020
2 of 7 tasks
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.

3 participants