From 088e85ca647ffecc06ca961933d7532ed75d1b8d Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Fri, 28 Jul 2017 14:53:20 -0700 Subject: [PATCH] add comment and test for memory api of grpc_call_cancel_with_status --- include/grpc/grpc.h | 6 +++++- src/core/lib/surface/call.c | 2 ++ test/core/end2end/tests/cancel_with_status.c | 8 +++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index 2cf8de0a2d3..943d6e4891f 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 2365d27307c..0883ea7739b 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 d659d1173a2..fd26fd122e9 100644 --- a/test/core/end2end/tests/cancel_with_status.c +++ b/test/core/end2end/tests/cancel_with_status.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #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);