Remove allowance of ClientStream.sendCancel() before start()
It looks like it will trigger a NullPointerException. This could maybe be handled in AbstractClientStream, but I'm unsure if that's the best place.
It seems like the OkHttp transport handles this case in the OkHttpClientStream. However, not really. OkHttpClientStream has two start methods: start(ClientStreamListener) and start(Integer streamId). If start(ClientStreamListener) is not called before cancel, an Exception is thrown here. So there is a need for a separate fix I think?
In Netty, we can't do the same in NettyClientHandler, as transportReportStatus needs to be called from within the Netty event loop. Therefore, the way I understand it, we can either do null checking in NettyClientHandler or introduce CancelNeverCreatedClientStreamCommand 😄 and handle it separately?
Thoughts @ejona86 ?
We may not need to support stream.cancel() before stream.start() any more, since the main source of cancel is only registered after start(). It looks like ClientCallImpl.cancel is safe because stream is created within call.start().
Maybe we should audit of all calls to stream.cancel() and prove they can't be done before stream.start(). If things look safe we remove support for the scenario. That seems like a win.
@ejona86 that sounds also good to me. I can do that.
If things look safe we remove support for the scenario.
That is, update the API docs of internal.ClientStream?
Updating the API docs sounds fine. There may be some clean-up/simplification possible to OkHttpClientStream.
sounds good. I ll get on it!
@ejona86
I spent some time looking at it and I didn't find an issue where cancel() could be called before start().
Only thing I noticed is, that once we implement ManagedChannel.shutdownNow(), this will involve canceling existing streams, right? So there might then be a race between cancel() and start().
Say between the time ClientCall.start() creates a new ClientStream and the time start() is called on that ClientStream, another thread might call ManagedChannel.shutdownNow(). Now, a channel is thread-safe but a stream is not. I am not sure how all of this will play out. It's just something I noticed.
Anyways, I ll open a PR this Friday when I am back from my trip, as I won't be able to respond to comments before that anyways.
One thing worth pointing out: At the highest level surface API, it is cleaner to allow the application to cancel() before start(). Now, this does not have to be plumbed all the way through the implementation and definitely not on the wire, but on the surface API, it makes sense to allow arbitrary order between the two instead of forcing the application developer to explicitly track whether the call has started on all cancellation codepaths.
@a11r, yes, we want to continue supporting cancel() before start() in our public API, to ease exception handling. I will note that our cancel() on ClientCall is not thread-safe and does require coordination. Context-based cancellation does not. We've debated whether to have cancel() be thread-safe, but it has remained non-thread-safe.
@buchgr shutdownNow() should end up being implemented on each transport, so cancel() won't be used for that case. Implementing it in the transports allows re-using the list of rpcs/streams and allows us to avoid races by using the existing synchronization that transports have in place.
@ejona86
yes, we want to continue supporting cancel() before start() in our public API, to ease exception handling.
Wait. ClientCall.cancel(...) is not supported before ClientCall.start() per API docs.
* <p>{@link #start start()} must be called prior to calling any other methods. {@link #cancel} must
* not be followed by any other methods, but can be called more than once, while only the first one
* has effect.
https://github.com/grpc/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java#L47
@buchgr, the first part of those docs should probably be updated:
{@link #start start()} must be called prior to calling any other methods.
The part about only being able to call cancel after cancel would still be true. ClientCallImpl.cancel(...) already supports cancel before start().
I see. So this issue changed to: 1) Update ClientCall docs to support cancel() before start() 2) Update ClientStream docs to require start() to be called before cancel() 3) Make any necessary code changes for 1) and 2) to be implemented correctly?
The part about only being able to call cancel after cancel would still be true. what? :)
@buchgr, yes that sounds right. With the belief that no code changes are necessary for (1) to be implemented correctly.