Fix error handling in channel initialization.

pull/8795/head
Mark D. Roth 8 years ago
parent 494790b84e
commit e62605f41e
  1. 9
      src/core/lib/channel/channel_stack_builder.c
  2. 23
      src/core/lib/surface/channel.c
  3. 12
      test/core/surface/channel_create_test.c

@ -259,15 +259,22 @@ grpc_error *grpc_channel_stack_builder_finish(
destroy_arg == NULL ? *result : destroy_arg, filters, num_filters, destroy_arg == NULL ? *result : destroy_arg, filters, num_filters,
builder->args, builder->transport, builder->name, channel_stack); builder->args, builder->transport, builder->name, channel_stack);
if (error != GRPC_ERROR_NONE) {
grpc_channel_stack_destroy(exec_ctx, channel_stack);
gpr_free(*result);
*result = NULL;
} else {
// run post-initialization functions // run post-initialization functions
i = 0; i = 0;
for (filter_node *p = builder->begin.next; p != &builder->end; p = p->next) { for (filter_node *p = builder->begin.next; p != &builder->end;\
p = p->next) {
if (p->init != NULL) { if (p->init != NULL) {
p->init(channel_stack, grpc_channel_stack_element(channel_stack, i), p->init(channel_stack, grpc_channel_stack_element(channel_stack, i),
p->init_arg); p->init_arg);
} }
i++; i++;
} }
}
grpc_channel_stack_builder_destroy(builder); grpc_channel_stack_builder_destroy(builder);
gpr_free((grpc_channel_filter **)filters); gpr_free((grpc_channel_filter **)filters);

@ -86,37 +86,36 @@ grpc_channel *grpc_channel_create(grpc_exec_ctx *exec_ctx, const char *target,
const grpc_channel_args *input_args, const grpc_channel_args *input_args,
grpc_channel_stack_type channel_stack_type, grpc_channel_stack_type channel_stack_type,
grpc_transport *optional_transport) { grpc_transport *optional_transport) {
bool is_client = grpc_channel_stack_type_is_client(channel_stack_type);
grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create(); grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create();
grpc_channel_stack_builder_set_channel_arguments(builder, input_args); grpc_channel_stack_builder_set_channel_arguments(builder, input_args);
grpc_channel_stack_builder_set_target(builder, target); grpc_channel_stack_builder_set_target(builder, target);
grpc_channel_stack_builder_set_transport(builder, optional_transport); grpc_channel_stack_builder_set_transport(builder, optional_transport);
grpc_channel *channel;
grpc_channel_args *args;
if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) { if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) {
grpc_channel_stack_builder_destroy(builder); grpc_channel_stack_builder_destroy(builder);
return NULL; return NULL;
} }
args = grpc_channel_args_copy( grpc_channel_args *args = grpc_channel_args_copy(
grpc_channel_stack_builder_get_channel_arguments(builder)); grpc_channel_stack_builder_get_channel_arguments(builder));
grpc_channel *channel;
grpc_error *error = grpc_channel_stack_builder_finish( grpc_error *error = grpc_channel_stack_builder_finish(
exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, NULL, exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, NULL,
(void **)&channel); (void **)&channel);
if (error != GRPC_ERROR_NONE) { if (error != GRPC_ERROR_NONE) {
grpc_channel_stack_destroy(exec_ctx, (grpc_channel_stack *)channel); const char* msg = grpc_error_string(error);
gpr_free(channel); gpr_log(GPR_ERROR, "channel stack builder failed: %s", msg);
return NULL; grpc_error_free_string(msg);
GRPC_ERROR_UNREF(error);
goto done;
} }
memset(channel, 0, sizeof(*channel)); memset(channel, 0, sizeof(*channel));
channel->target = gpr_strdup(target); channel->target = gpr_strdup(target);
channel->is_client = is_client; channel->is_client = grpc_channel_stack_type_is_client(channel_stack_type);
gpr_mu_init(&channel->registered_call_mu); gpr_mu_init(&channel->registered_call_mu);
channel->registered_calls = NULL; channel->registered_calls = NULL;
grpc_compression_options_init(&channel->compression_options); grpc_compression_options_init(&channel->compression_options);
if (args) {
for (size_t i = 0; i < args->num_args; i++) { for (size_t i = 0; i < args->num_args; i++) {
if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) { if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
if (args->args[i].type != GRPC_ARG_STRING) { if (args->args[i].type != GRPC_ARG_STRING) {
@ -169,9 +168,9 @@ grpc_channel *grpc_channel_create(grpc_exec_ctx *exec_ctx, const char *target,
0x1; /* always support no compression */ 0x1; /* always support no compression */
} }
} }
grpc_channel_args_destroy(args);
}
done:
grpc_channel_args_destroy(args);
return channel; return channel;
} }

@ -31,9 +31,14 @@
* *
*/ */
#include <string.h>
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include "src/core/ext/client_channel/resolver_registry.h" #include "src/core/ext/client_channel/resolver_registry.h"
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/surface/channel.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
void test_unknown_scheme_target(void) { void test_unknown_scheme_target(void) {
@ -44,6 +49,13 @@ void test_unknown_scheme_target(void) {
chan = grpc_insecure_channel_create("blah://blah", NULL, NULL); chan = grpc_insecure_channel_create("blah://blah", NULL, NULL);
GPR_ASSERT(chan != NULL); GPR_ASSERT(chan != NULL);
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
grpc_channel_element *elem =
grpc_channel_stack_element(grpc_channel_get_channel_stack(chan), 0);
GPR_ASSERT(0 == strcmp(elem->filter->name, "lame-client"));
grpc_exec_ctx_finish(&exec_ctx);
grpc_channel_destroy(chan); grpc_channel_destroy(chan);
} }

Loading…
Cancel
Save