From ce002bd4498a83c244f56331e1ab255f860c4c9d Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Fri, 13 May 2016 22:20:02 +0200 Subject: [PATCH] core: allow ClientCall.cancel before start. Fixes #1536 (#1822) --- core/src/main/java/io/grpc/ClientCall.java | 13 +++++++------ .../main/java/io/grpc/internal/ClientCallImpl.java | 2 +- .../main/java/io/grpc/internal/ClientStream.java | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/io/grpc/ClientCall.java b/core/src/main/java/io/grpc/ClientCall.java index 3b9dcd016d..c1720f8153 100644 --- a/core/src/main/java/io/grpc/ClientCall.java +++ b/core/src/main/java/io/grpc/ClientCall.java @@ -34,7 +34,7 @@ package io.grpc; import javax.annotation.Nullable; /** - * An instance of a call to to a remote method. A call will send zero or more + * An instance of a call to a remote method. A call will send zero or more * request messages to the server and receive zero or more response messages back. * *

Instances are created @@ -44,9 +44,9 @@ import javax.annotation.Nullable; * reasons for doing so would be the need to interact with flow-control or when acting as a generic * proxy for arbitrary operations. * - *

{@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. + *

{@link #start} must be called prior to calling any other methods, with the exception of + * {@link #cancel}. Whereas {@link #cancel} must not be followed by any other methods, + * but can be called more than once, while only the first one has effect. * *

No generic method for determining message receipt or providing acknowledgement is provided. * Applications are expected to utilize normal payload messages for such signals, as a response @@ -126,8 +126,9 @@ public abstract class ClientCall { /** * Start a call, using {@code responseListener} for processing response messages. * - *

It must be called prior to any other method on this class. Since {@link Metadata} is not - * thread-safe, the caller must not access {@code headers} after this point. + *

It must be called prior to any other method on this class, except for {@link #cancel} which + * may be called at any time. Since {@link Metadata} is not thread-safe, the caller must not + * access {@code headers} after this point. * * @param responseListener receives response messages * @param headers which can contain extra call metadata, e.g. authentication credentials. diff --git a/core/src/main/java/io/grpc/internal/ClientCallImpl.java b/core/src/main/java/io/grpc/internal/ClientCallImpl.java index 75d7f2474f..d9083c3619 100644 --- a/core/src/main/java/io/grpc/internal/ClientCallImpl.java +++ b/core/src/main/java/io/grpc/internal/ClientCallImpl.java @@ -311,7 +311,7 @@ final class ClientCallImpl extends ClientCall cancelCalled = true; try { // Cancel is called in exception handling cases, so it may be the case that the - // stream was never successfully created. + // stream was never successfully created or start has never been called. if (stream != null) { Status status = Status.CANCELLED; if (message != null) { diff --git a/core/src/main/java/io/grpc/internal/ClientStream.java b/core/src/main/java/io/grpc/internal/ClientStream.java index 9d875a2a15..c639ea0bc4 100644 --- a/core/src/main/java/io/grpc/internal/ClientStream.java +++ b/core/src/main/java/io/grpc/internal/ClientStream.java @@ -43,8 +43,9 @@ public interface ClientStream extends Stream { /** * Abnormally terminates the stream. After calling this method, no further messages will be * sent or received, however it may still be possible to receive buffered messages for a brief - * period until {@link ClientStreamListener#closed} is called. This method is safe to be called - * at any time and multiple times and from any thread. + * period until {@link ClientStreamListener#closed} is called. This method may only be called + * after {@link #start}, but else is safe to be called at any time and multiple times and + * from any thread. * * @param reason must be non-OK */