Skip to content

Conversation

@zpencer
Copy link
Contributor

@zpencer zpencer commented Oct 24, 2017

This achieves two things:

  • communicates that CancellableContext is a resource that must be freed
  • provides the ability to use the try-with-resources idiom to cancel the context when we finish up some blocking work. For users, this may be a better API than runAndCancel and callAndCancel.

@ejona86 and I considered adding a try-with-resources API that attaches the CancellableContext before the try, and both cancels as well as detaches at the close. But the problem is that the close runs before any catch statements, so users will not be able to use the exception as the cancellation cause unless they use a nested try/catch.

We also considered adding a try-with-resources API to attach/detach a context without cancelling, so that the two try statements in the example will look more symmetrical. But the syntactic sugar it provides is very minor.

Internal use cases that kick off work asynchronously and use ListenableFuture will use an internal helper utility. We can not easily open source that code because grpc-context must have minimal dependencies and can not use guava.

@zpencer
Copy link
Contributor Author

zpencer commented Oct 24, 2017

CC: @adriancole

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Oct 24, 2017 via email

Copy link
Contributor

@zhangkun83 zhangkun83 left a comment

Choose a reason for hiding this comment

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

LGTM

* cancelled.
*
* <p>This class must be cleaned up by either calling {@link #close} or {@link #cancel}.
* {@link #close} is equivalent to calling {@code cancel(null)}. It is safe to call the methods
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe also in cancel()'s javadoc refer back to close().

* more than once, but only the first call will have any effect.
*
* <p>Blocking code can use the try-with-resources idiom:
* <pre>
Copy link
Member

Choose a reason for hiding this comment

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

Get rid of the extra newlines. Take a look at the generated documentation to see how it looks.

@ejona86
Copy link
Member

ejona86 commented Oct 25, 2017

Concerning catching the exception: I think I may encourage people not to (and just cancel(null)/close()).

Basically there's two different operations at play here: cancellation (cancel with error) and cleanup (cancel with null). My hypothesis is that generally when using try-with-resources (aka, you're using blocking-style APIs), you have no need to cancel, even if an exception was thrown. The only time "you're cancelling" is when the Deadline is reached, which provides an exception.

Now, that's not at all true when doing async work. But if you're doing async work you're much less likely to use try-with-resources.

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Oct 25, 2017 via email

@zpencer zpencer merged commit 255643b into grpc:master Oct 25, 2017
@zpencer zpencer deleted the cancellablecontext-closeable branch February 10, 2018 00:55
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants