Merge pull request #964 from ctiller/i-am-so-very-lazy

Crash in channel/server creation if grpc_init not called
pull/967/head
Nicolas Noble 10 years ago
commit d3be281353
  1. 2
      src/core/surface/channel.c
  2. 16
      src/core/surface/init.c
  3. 1
      src/core/surface/init.h
  4. 4
      src/core/surface/server.c

@ -39,6 +39,7 @@
#include "src/core/iomgr/iomgr.h" #include "src/core/iomgr/iomgr.h"
#include "src/core/surface/call.h" #include "src/core/surface/call.h"
#include "src/core/surface/client.h" #include "src/core/surface/client.h"
#include "src/core/surface/init.h"
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
@ -63,6 +64,7 @@ grpc_channel *grpc_channel_create_from_filters(
size_t size = size_t size =
sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters); sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters);
grpc_channel *channel = gpr_malloc(size); grpc_channel *channel = gpr_malloc(size);
GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
channel->is_client = is_client; channel->is_client = is_client;
/* decremented by grpc_channel_destroy, and grpc_client_channel_closed if is_client */ /* decremented by grpc_channel_destroy, and grpc_client_channel_closed if is_client */
gpr_ref_init(&channel->refs, 1 + is_client); gpr_ref_init(&channel->refs, 1 + is_client);

@ -40,17 +40,17 @@
#include "src/core/surface/surface_trace.h" #include "src/core/surface/surface_trace.h"
#include "src/core/transport/chttp2_transport.h" #include "src/core/transport/chttp2_transport.h"
static gpr_once g_init = GPR_ONCE_INIT; static gpr_once g_basic_init = GPR_ONCE_INIT;
static gpr_mu g_init_mu; static gpr_mu g_init_mu;
static int g_initializations; static int g_initializations;
static void do_init(void) { static void do_basic_init(void) {
gpr_mu_init(&g_init_mu); gpr_mu_init(&g_init_mu);
g_initializations = 0; g_initializations = 0;
} }
void grpc_init(void) { void grpc_init(void) {
gpr_once_init(&g_init, do_init); gpr_once_init(&g_basic_init, do_basic_init);
gpr_mu_lock(&g_init_mu); gpr_mu_lock(&g_init_mu);
if (++g_initializations == 1) { if (++g_initializations == 1) {
@ -73,3 +73,13 @@ void grpc_shutdown(void) {
} }
gpr_mu_unlock(&g_init_mu); gpr_mu_unlock(&g_init_mu);
} }
int grpc_is_initialized(void) {
int r;
gpr_once_init(&g_basic_init, do_basic_init);
gpr_mu_lock(&g_init_mu);
r = g_initializations > 0;
gpr_mu_unlock(&g_init_mu);
return r;
}

@ -35,5 +35,6 @@
#define GRPC_INTERNAL_CORE_SURFACE_INIT_H #define GRPC_INTERNAL_CORE_SURFACE_INIT_H
void grpc_security_pre_init(void); void grpc_security_pre_init(void);
int grpc_is_initialized(void);
#endif /* GRPC_INTERNAL_CORE_SURFACE_INIT_H */ #endif /* GRPC_INTERNAL_CORE_SURFACE_INIT_H */

@ -44,6 +44,7 @@
#include "src/core/surface/call.h" #include "src/core/surface/call.h"
#include "src/core/surface/channel.h" #include "src/core/surface/channel.h"
#include "src/core/surface/completion_queue.h" #include "src/core/surface/completion_queue.h"
#include "src/core/surface/init.h"
#include "src/core/transport/metadata.h" #include "src/core/transport/metadata.h"
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
@ -612,6 +613,9 @@ grpc_server *grpc_server_create_from_filters(grpc_completion_queue *cq,
int census_enabled = grpc_channel_args_is_census_enabled(args); int census_enabled = grpc_channel_args_is_census_enabled(args);
grpc_server *server = gpr_malloc(sizeof(grpc_server)); grpc_server *server = gpr_malloc(sizeof(grpc_server));
GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
memset(server, 0, sizeof(grpc_server)); memset(server, 0, sizeof(grpc_server));
if (cq) addcq(server, cq); if (cq) addcq(server, cq);

Loading…
Cancel
Save