From 9356b47903b7cee3ea5f1325dc69574179f75edf Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 2 Feb 2016 22:33:43 -0800 Subject: clean up the clean up code --- src/core/security/server_secure_chttp2.c | 36 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c index ee3443c828..aec07e22b2 100644 --- a/src/core/security/server_secure_chttp2.c +++ b/src/core/security/server_secure_chttp2.c @@ -191,6 +191,7 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, gpr_log(GPR_ERROR, "Unable to create secure server with credentials of type %s.", creds->type); + // UNREF sc goto error; } sc->channel_args = grpc_server_get_channel_args(server); @@ -198,6 +199,7 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, /* resolve address */ resolved = grpc_blocking_resolve_address(addr, "https"); if (!resolved) { + // UNREF sc goto error; } state = gpr_malloc(sizeof(*state)); @@ -205,9 +207,21 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, grpc_closure_init(&state->destroy_closure, destroy_done, state); tcp = grpc_tcp_server_create(&state->destroy_closure); if (!tcp) { + // UNREF sc + // destroy resolved + // free state goto error; } + state->server = server; + state->tcp = tcp; + state->sc = sc; + state->creds = grpc_server_credentials_ref(creds); + + state->is_shutdown = 0; + gpr_mu_init(&state->mu); + gpr_ref_init(&state->refcount, 1); + for (i = 0; i < resolved->naddrs; i++) { port_temp = grpc_tcp_server_add_port( tcp, (struct sockaddr *)&resolved->addrs[i].addr, @@ -224,6 +238,7 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, if (count == 0) { gpr_log(GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs); + // UNREF tcp goto error; } if (count != resolved->naddrs) { @@ -233,14 +248,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, } grpc_resolved_addresses_destroy(resolved); - state->server = server; - state->tcp = tcp; - state->sc = sc; - state->creds = grpc_server_credentials_ref(creds); - - state->is_shutdown = 0; - gpr_mu_init(&state->mu); - gpr_ref_init(&state->refcount, 1); /* Register with the server only upon success */ grpc_server_add_listener(&exec_ctx, server, state, start, destroy); @@ -250,17 +257,18 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, /* Error path: cleanup and return */ error: - if (sc) { - GRPC_SECURITY_CONNECTOR_UNREF(sc, "server"); - } if (resolved) { grpc_resolved_addresses_destroy(resolved); } if (tcp) { grpc_tcp_server_unref(&exec_ctx, tcp); - } - if (state) { - gpr_free(state); + } else { + if (sc) { + GRPC_SECURITY_CONNECTOR_UNREF(sc, "server"); + } + if (state) { + gpr_free(state); + } } grpc_exec_ctx_finish(&exec_ctx); return 0; -- cgit v1.2.3 From 70fa0da7f128bb2f5d6e4962352a7b2b4279e106 Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 2 Feb 2016 23:22:48 -0800 Subject: add a test --- test/core/surface/server_chttp2_test.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/core/surface/server_chttp2_test.c b/test/core/surface/server_chttp2_test.c index ec7df6f0e3..0a3652a27a 100644 --- a/test/core/surface/server_chttp2_test.c +++ b/test/core/surface/server_chttp2_test.c @@ -32,7 +32,14 @@ */ #include +#include +#include +#include #include +#include +#include "src/core/security/credentials.h" +#include "src/core/tsi/fake_transport_security.h" +#include "test/core/util/port.h" #include "test/core/util/test_config.h" void test_unparsable_target(void) { @@ -40,10 +47,31 @@ void test_unparsable_target(void) { GPR_ASSERT(port == 0); } +void test_add_same_port_twice() { + int port = grpc_pick_unused_port_or_die(); + char *addr = NULL; + grpc_completion_queue *cq = grpc_completion_queue_create(NULL); + grpc_server *server = grpc_server_create(NULL, NULL); + grpc_server_credentials *fake_creds = + grpc_fake_transport_security_server_credentials_create(); + gpr_join_host_port(&addr, "localhost", port); + GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr, fake_creds) == 0); + + grpc_server_credentials_release(fake_creds); + gpr_free(addr); + grpc_server_shutdown_and_notify(server, cq, NULL); + grpc_completion_queue_pluck(cq, NULL, gpr_inf_future(GPR_CLOCK_REALTIME), + NULL); + grpc_server_destroy(server); + grpc_completion_queue_destroy(cq); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); grpc_init(); test_unparsable_target(); + test_add_same_port_twice(); grpc_shutdown(); return 0; } -- cgit v1.2.3 From 9632716848deae8b63ab9d982c6a761f5bb7ccab Mon Sep 17 00:00:00 2001 From: yang-g Date: Tue, 2 Feb 2016 23:25:59 -0800 Subject: remove debug commments --- src/core/security/server_secure_chttp2.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/core/security/server_secure_chttp2.c b/src/core/security/server_secure_chttp2.c index aec07e22b2..92daab9bf1 100644 --- a/src/core/security/server_secure_chttp2.c +++ b/src/core/security/server_secure_chttp2.c @@ -191,7 +191,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, gpr_log(GPR_ERROR, "Unable to create secure server with credentials of type %s.", creds->type); - // UNREF sc goto error; } sc->channel_args = grpc_server_get_channel_args(server); @@ -199,7 +198,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, /* resolve address */ resolved = grpc_blocking_resolve_address(addr, "https"); if (!resolved) { - // UNREF sc goto error; } state = gpr_malloc(sizeof(*state)); @@ -207,9 +205,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, grpc_closure_init(&state->destroy_closure, destroy_done, state); tcp = grpc_tcp_server_create(&state->destroy_closure); if (!tcp) { - // UNREF sc - // destroy resolved - // free state goto error; } @@ -217,7 +212,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, state->tcp = tcp; state->sc = sc; state->creds = grpc_server_credentials_ref(creds); - state->is_shutdown = 0; gpr_mu_init(&state->mu); gpr_ref_init(&state->refcount, 1); @@ -238,7 +232,6 @@ int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr, if (count == 0) { gpr_log(GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs); - // UNREF tcp goto error; } if (count != resolved->naddrs) { -- cgit v1.2.3