diff --git a/src/perf_counter/perf_counters.cpp b/src/perf_counter/perf_counters.cpp index 732cfd4b01..9d47b6542f 100644 --- a/src/perf_counter/perf_counters.cpp +++ b/src/perf_counter/perf_counters.cpp @@ -46,6 +46,12 @@ namespace dsn { perf_counters::perf_counters() { + // make shared_io_service destructed after perf_counters, + // because shared_io_service will destruct the timer created by perf_counters + // It will produce heap-use-after-free error if shared_io_service destructed in front of + // perf_counters + tools::shared_io_service::instance(); + _perf_counters_cmd = command_manager::instance().register_command( {"perf-counters"}, "perf-counters - query perf counters, filtered by OR of POSIX basic regular expressions", diff --git a/src/runtime/service_api_c.cpp b/src/runtime/service_api_c.cpp index 60ffc7aaca..33e67ab376 100644 --- a/src/runtime/service_api_c.cpp +++ b/src/runtime/service_api_c.cpp @@ -348,6 +348,18 @@ bool run(const char *config_file, bool is_server, std::string &app_list) { + // We put the loading of configuration at the beginning of this func. + // Because in dsn_global_init(), it calls perf_counters::instance(), which calls + // shared_io_service::instance(). And in the cstor of shared_io_service, it calls + // dsn_config_get_value_uint64() to load the corresponding configs. That will make + // dsn_config_get_value_uint64() get wrong value if we put dsn_config_load at behind of + // dsn_global_init() + if (!dsn_config_load(config_file, config_arguments)) { + printf("Fail to load config file %s\n", config_file); + return false; + } + dsn::flags_initialize(); + dsn_global_init(); dsn_core_init(); ::dsn::task::set_tls_dsn_context(nullptr, nullptr); @@ -358,13 +370,6 @@ bool run(const char *config_file, dsn_all.engine = &::dsn::service_engine::instance(); dsn_all.magic = 0xdeadbeef; - if (!dsn_config_load(config_file, config_arguments)) { - printf("Fail to load config file %s\n", config_file); - return false; - } - - dsn::flags_initialize(); - // pause when necessary if (dsn_config_get_value_bool("core", "pause_on_start",