Skip to content

Commit

Permalink
fix memory leak issue
Browse files Browse the repository at this point in the history
  • Loading branch information
w-gc committed Jan 16, 2025
1 parent 4c33f88 commit 237146a
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions src/brpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ ServerOptions::ServerOptions()
, rtmp_service(NULL)
, redis_service(NULL)
, bthread_tag(BTHREAD_TAG_DEFAULT)
, rpc_pb_message_factory(new DefaultRpcPBMessageFactory())
, rpc_pb_message_factory(NULL)
, ignore_eovercrowded(false) {
if (s_ncore > 0) {
num_threads = s_ncore + 1;
Expand Down Expand Up @@ -424,7 +424,10 @@ Server::Server(ProfilerLinker)
, _eps_bvar(&_nerror_bvar)
, _concurrency(0)
, _concurrency_bvar(cast_no_barrier_int, &_concurrency)
,_has_progressive_read_method(false) {
, _has_progressive_read_method(false) {
// `rpc_pb_message_factory` is created here because it is possible
// for users to visit it at any time after `Server` created
_options.rpc_pb_message_factory = new DefaultRpcPBMessageFactory();
BAIDU_CASSERT(offsetof(Server, _concurrency) % 64 == 0,
Server_concurrency_must_be_aligned_by_cacheline);
}
Expand Down Expand Up @@ -782,6 +785,47 @@ static bool OptionsAvailableOverRdma(const ServerOptions* opt) {
static AdaptiveMaxConcurrency g_default_max_concurrency_of_method(0);
static bool g_default_ignore_eovercrowded(false);


inline void copy_server_option(ServerOptions& dst, const ServerOptions& src) {
if (&dst == &src) {
return;
}

#define FREE_PTR_IF_NOT_REUSED(ptr) \
if (dst.ptr != src.ptr) { \
delete dst.ptr; \
dst.ptr = NULL; \
}

FREE_PTR_IF_NOT_REUSED(nshead_service);

#ifdef ENABLE_THRIFT_FRAMED_PROTOCOL
FREE_PTR_IF_NOT_REUSED(thrift_service);
#endif

FREE_PTR_IF_NOT_REUSED(baidu_master_service);
FREE_PTR_IF_NOT_REUSED(http_master_service);
FREE_PTR_IF_NOT_REUSED(rpc_pb_message_factory);

if (dst.pid_file != src.pid_file && !dst.pid_file.empty()) {
unlink(dst.pid_file.c_str());
}

if (dst.server_owns_auth) {
FREE_PTR_IF_NOT_REUSED(auth);
}

if (dst.server_owns_interceptor) {
FREE_PTR_IF_NOT_REUSED(interceptor);
}

FREE_PTR_IF_NOT_REUSED(redis_service);

#undef FREE_PTR_IF_NOT_REUSED

dst = src;
}

int Server::StartInternal(const butil::EndPoint& endpoint,
const PortRange& port_range,
const ServerOptions *opt) {
Expand Down Expand Up @@ -813,14 +857,26 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
}
return -1;
}

if (opt) {
_options = *opt;
copy_server_option(_options, *opt);
} else {
// Don't forget to release `rpc_pb_message_factory` before overwriting `_options`
delete _options.rpc_pb_message_factory;
_options.rpc_pb_message_factory = NULL;

// Always reset to default options explicitly since `_options'
// may be the options for the last run or even bad options
_options = ServerOptions();
}

// Create the resource if:
// 1. `_options` copied from user and user forgot to create
// 2. `_options` created by our
if (!_options.rpc_pb_message_factory) {
_options.rpc_pb_message_factory = new DefaultRpcPBMessageFactory();
}

if (!_options.h2_settings.IsValid(true/*log_error*/)) {
LOG(ERROR) << "Invalid h2_settings";
return -1;
Expand Down

0 comments on commit 237146a

Please sign in to comment.