aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar apolcyn <apolcyn@google.com>2017-07-31 17:10:42 -0700
committerGravatar GitHub <noreply@github.com>2017-07-31 17:10:41 -0700
commitb667f12cef5efd2b379efd7a0bbddd2acd59ad0c (patch)
treec0674f12f583783e8b60a15f31f6cb921af0b8b6
parent3188ae5cafda7f86904f1940703fd5ee9659fb3b (diff)
parent088e85ca647ffecc06ca961933d7532ed75d1b8d (diff)
Merge pull request #11979 from apolcyn/ensure_cancel_with_status_memory_api
Add comment and test for API of a parameter to grpc_call_cancel_with_status
-rw-r--r--include/grpc/grpc.h6
-rw-r--r--src/core/lib/surface/call.c2
-rw-r--r--test/core/end2end/tests/cancel_with_status.c8
3 files changed, 14 insertions, 2 deletions
diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h
index 2cf8de0a2d..943d6e4891 100644
--- a/include/grpc/grpc.h
+++ b/include/grpc/grpc.h
@@ -296,7 +296,11 @@ GRPCAPI grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved);
If a status has not been received for the call, set it to the status code
and description passed in.
Importantly, this function does not send status nor description to the
- remote endpoint. */
+ remote endpoint.
+ Note that \a description doesn't need be a static string.
+ It doesn't need to be alive after the call to
+ grpc_call_cancel_with_status completes.
+ */
GRPCAPI grpc_call_error grpc_call_cancel_with_status(grpc_call *call,
grpc_status_code status,
const char *description,
diff --git a/src/core/lib/surface/call.c b/src/core/lib/surface/call.c
index 04613f17e3..c57d0894c2 100644
--- a/src/core/lib/surface/call.c
+++ b/src/core/lib/surface/call.c
@@ -644,6 +644,8 @@ static void cancel_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c,
static grpc_error *error_from_status(grpc_status_code status,
const char *description) {
+ // copying 'description' is needed to ensure the grpc_call_cancel_with_status
+ // guarantee that can be short-lived.
return grpc_error_set_int(
grpc_error_set_str(GRPC_ERROR_CREATE_FROM_COPIED_STRING(description),
GRPC_ERROR_STR_GRPC_MESSAGE,
diff --git a/test/core/end2end/tests/cancel_with_status.c b/test/core/end2end/tests/cancel_with_status.c
index d659d1173a..fd26fd122e 100644
--- a/test/core/end2end/tests/cancel_with_status.c
+++ b/test/core/end2end/tests/cancel_with_status.c
@@ -25,6 +25,7 @@
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
#include <grpc/support/time.h>
#include <grpc/support/useful.h>
#include "src/core/lib/support/string.h"
@@ -138,7 +139,12 @@ static void simple_request_body(grpc_end2end_test_config config,
error = grpc_call_start_batch(c, ops, num_ops, tag(1), NULL);
GPR_ASSERT(GRPC_CALL_OK == error);
- grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL);
+ char *dynamic_string = gpr_strdup("xyz");
+ grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED,
+ (const char *)dynamic_string, NULL);
+ // The API of \a description allows for it to be a dynamic/non-const
+ // string, test this guarantee.
+ gpr_free(dynamic_string);
CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
cq_verify(cqv);