Skip to content

Handle timeouts for NpgsqlConnector.DoWaitAsync() - #3142

Merged
roji merged 11 commits into
npgsql:mainfrom
vonzshik:TimeoutHandlingForWaitAsync
Sep 26, 2020
Merged

roji merged 11 commits into
npgsql:mainfrom
vonzshik:TimeoutHandlingForWaitAsync

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Closes #3140

@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.

A few thoughts... the WaitAsync is using a timer in order to send the keepalive - this is different from Wait, which simply waits for the timeout to fire and then sends the keepalive; the difference is because up to now we had no async timeouts.

We can now change both methods to use the same technique, and possibly even to merge them together. I think that the sync technique (timing out and then sending the keepalive) is safer since it doesn't involve the concurrency that the timer brings in (in fact, I'm pretty sure there are some race conditions around this with the current code).

Note also how WaitAsync registers performKeepAlive to execute when the user cancellation token is triggered... This is a hacky way to unblock the async read when the user cancels, because again we had no proper cancellation (which we now have). I think we should clean that up as well and simply have the cancellation token trigger cancellation of the read.

Hope all of the above makes sense... and also that you're OK with a wider refactor/cleanup of how waiting works.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@vonzshik

Copy link
Copy Markdown
Contributor Author

A few thoughts... the WaitAsync is using a timer in order to send the keepalive - this is different from Wait, which simply waits for the timeout to fire and then sends the keepalive; the difference is because up to now we had no async timeouts.

Oh, so that's why the WaitAsync works so differently from the Wait.

Hope all of the above makes sense... and also that you're OK with a wider refactor/cleanup of how waiting works.

No problems here 😁

@roji

roji commented Sep 12, 2020

Copy link
Copy Markdown
Member

(@vonzshik just to be sure that we work efficiently, when you've made changes and a PR is ready for me to look again, you can request a re-review (the arrows next to my name in the reviewers above)

@vonzshik

Copy link
Copy Markdown
Contributor Author

(@vonzshik just to be sure that we work efficiently, when you've made changes and a PR is ready for me to look again, you can request a re-review (the arrows next to my name in the reviewers above)

Got it!

Concerning this PR, while it's mostly done, I have some troubles with cancelling the read by using cancellation token. Right now, we have no way to cancel the async read (there is an issue about remedying this #2437). So, I'm not sure if this PR should be put on hold, merged as is, or maybe use some temporary hack.

@roji

roji commented Sep 12, 2020

Copy link
Copy Markdown
Member

Let's put this on hold until #2437 is done, this way we do everything right in one go.

@Brar

Brar commented Sep 14, 2020

Copy link
Copy Markdown
Member

Let's put this on hold until #2437 is done, this way we do everything right in one go.

Is anybody actually working on resolving #2437?

I get that in #2437 (comment) @YohDeadfall wrote:

FYI, already supported the idea in the new type handlers which hope will publish soon.

But then again in #2439 (comment) @YohDeadfall wrote:

Unfortunately, I don't have enough time to a very large work on the type handling system

So I'm kind of unsure whether there is actually some progress on the issue that is blocking this PR and (partially) my replication related work.
I can certainly take care of it but want to make sure that there isn't somebody working on it simultaneously.

@Brar

Brar commented Sep 14, 2020

Copy link
Copy Markdown
Member

BTW: I have an experimental branch where I simply added a CancellationToken to all low level reading methods that could potentially block on a socket and wired it up to the public interface (not taking care of active query cancellation though). to see how this would look like.
This is not a final (or even my suggested) implementation but possibly something we can talk about or build upon.

@roji

roji commented Sep 14, 2020

Copy link
Copy Markdown
Member

Is anybody actually working on resolving #2437?

Am not sure, but I think that issue is orthogonal to @YohDeadfall's new type handlers (which are being punted for 6.0).

You can definitely work on this if you want - though maybe @vonzshik is interested in this as he's been looking at cancellation and timeouts recently. Note also that there's an interaction between #2437 and #3130 - it's probably best to wait until the latter is merged.

@Brar

Brar commented Sep 14, 2020

Copy link
Copy Markdown
Member

Note also that there's an interaction between #2437 and #3130

Everything is intertwined around async cancellation that's why we need to coordinate our work a lot.

You can definitely work on this if you want - though maybe @vonzshik is interested in this as he's been looking at cancellation and timeouts recently.

I'm not particularly interested in doing it (enough on my plate already), so @vonzshik if you feel like taking care of this too, please say so and go ahead!

@vonzshik

Copy link
Copy Markdown
Contributor Author

#2437 is something, that's on my radar (together with #1146). So, after #3130 is done, I'll be glad to continue @Brar's work.

@vonzshik
vonzshik force-pushed the TimeoutHandlingForWaitAsync branch from cf92a8f to 4133df2 Compare September 24, 2020 07:34
@vonzshik
vonzshik requested a review from roji September 24, 2020 07:55

@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.

Looks almost ready to merge.

Comment thread test/Npgsql.Tests/NotificationTests.cs Resolved
Comment thread test/Npgsql.Tests/NotificationTests.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved

@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.

Some small comments.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Outdated
if (timeout > 0 && async)
throw new NotSupportedException("WaitAsync with timeout isn't supported when used on .NET Framework. Please consider moving to .NET Core.");

if (timeout <= 0 && cancellationToken.CanBeCanceled)

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.

Shouldn't we always through for CanBeCanceled, regardless of timeout?

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.

There is a case with a KeepAlive. While the CancellationToken doesn't cancel write or read from a socket, we do still have some checks for cancellationToken.ThrowIfCancellationRequested().

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.

Hmm, OK... It's not very important, but these checks don't seem to be enough to say that WaitAsync actually supports the cancellation tokens - the really interesting case is to actually cancel a read operation, which we can't do on this TFM. So I'm leaning towards throwing immediately for any non-None cancellation token.

But I'm fine to merge either way - let me know which way you want to do it.

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.

Oh, I see you actually already did this. Merging.

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, not really - it's still working the same. We check for the timeout, and if it is less or equal to zero, then we check for the cancellation token. I don't mind if you do change it.

@roji
roji merged commit 04313e0 into npgsql:main Sep 26, 2020
@YohDeadfall

Copy link
Copy Markdown
Contributor

Thank you for the work on cancellation! Since we announce events and features in Twitter it would be cool to know your profile there. It allows us to promote our contributors (:

@manandre, @warcha, we would like to hear your profile names too if you wish to be credited in Twitter (:

@manandre

Copy link
Copy Markdown
Collaborator

Thanks for the promotion!
Twitter: @manandre94

@vonzshik

Copy link
Copy Markdown
Contributor Author

That's very cool. Thank you, @YohDeadfall.
My twitter handle is the same one as here - @vonzshik.

@roji

roji commented Sep 27, 2020

Copy link
Copy Markdown
Member

Just did a tweet :) Thanks for suggesting this @YohDeadfall!

@YohDeadfall

Copy link
Copy Markdown
Contributor

You even could tweet yourself using hashtag #npgsql and we will spread it across the network. Sending us links to the tweets is fine too because we won't miss them (:

@roji

roji commented Sep 27, 2020

Copy link
Copy Markdown
Member

(@warcha just waiting for your Twitter handle too!)

@YohDeadfall

YohDeadfall commented Sep 27, 2020 •

Copy link
Copy Markdown
Contributor

Sent a guess with 99.(9)% probably privately.

@roji

roji commented Sep 27, 2020 •

Copy link
Copy Markdown
Member

Will wait for @warcha to confirm they want to be publicly mentioned etc.

@warcha

warcha commented Sep 27, 2020

Copy link
Copy Markdown
Contributor

Thanks guys for your generous offer, unfortuntately I'm not on twitter so I'm all good here. Thanks again.

@vonzshik
vonzshik deleted the TimeoutHandlingForWaitAsync branch September 28, 2020 06:49
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.

Handle timeouts for WaitAsync

6 participants