Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better handle canceled contexts in queries #5

Merged
merged 1 commit into from
Jul 16, 2021

Conversation

Stebalien
Copy link
Member

  1. Make sure we don't block forever writing the result.
  2. Make sure we stop the query when the context is canceled.
  3. Allow receiving one result after the context is canceled, the query could have been writing it and may not have checked if the context was canceled yet.

received := 0
for range ch {
received++
require.LessOrEqual(t, received, 1, "expected to receive at most one result after the context was canceled")
Copy link
Member

Choose a reason for hiding this comment

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

Thats a very strange set of semantics to expect here. I guess it makes sense, but is this really what we want the blockstore interface to guarantee?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we don't really have any guarantees. The blockstore is allowed to buffer responses.

Copy link
Member Author

Choose a reason for hiding this comment

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

I.e., it could give us 10 items. I guess I can just make the test allow that to be more flexible?

Copy link

Choose a reason for hiding this comment

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

Certainly not specified by the spec, but I assume this is only testing one blockstore implementation, so you can make more assumptions :)

Copy link
Member

@whyrusleeping whyrusleeping left a comment

Choose a reason for hiding this comment

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

The bugfix looks correct, but the test feels flaky (if only because its a weird assertion).

If you are fine with the weirdness of the test, then LGTM

Copy link

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

SGTM

case ret <- cid.NewCidV1(codec, mh):
case <-ctx.Done():
return
}
}
case ctx.Err() != nil:
Copy link

Choose a reason for hiding this comment

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

IMO this ctx check is now kinda redundant, as it's surrounded by another two "are we cancelled" checks.

Copy link

Choose a reason for hiding this comment

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

And in turn you could also replace the naked switch with just a good old if err != nil check.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO this ctx check is now kinda redundant, as it's surrounded by another two "are we cancelled" checks.

It's to avoid logging in that case. But I agree.

received := 0
for range ch {
received++
require.LessOrEqual(t, received, 1, "expected to receive at most one result after the context was canceled")
Copy link

Choose a reason for hiding this comment

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

Certainly not specified by the spec, but I assume this is only testing one blockstore implementation, so you can make more assumptions :)

@Stebalien
Copy link
Member Author

The bugfix looks correct, but the test feels flaky (if only because its a weird assertion).

I agree. I'll make it a little more flexible.

1. Make sure we don't block forever writing the result.
2. Make sure we stop the query when the context is canceled.
3. Allow receiving a few results after the context is canceled, the query
   could have been writing it and may not have checked if the context
   was canceled yet.
@Stebalien Stebalien force-pushed the fix/context-cancel-query branch from 0c5a316 to 3823254 Compare July 16, 2021 18:18
@Stebalien Stebalien merged commit 35fbc40 into main Jul 16, 2021
@Stebalien
Copy link
Member Author

Ok, I gave it a flex of "10". We need to cancel within 10 results.

@Stebalien Stebalien deleted the fix/context-cancel-query branch July 16, 2021 18:19
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