skynet_context: skynet_context_new 改为返回 handle 避免调用者拿到悬垂指针 (#2095)

* skynet_context: skynet_context_new 改为返回 handle 避免调用者拿到悬垂指针

* 推迟 skynet_handle_grab(logger_handle) 到 bootstrap 失败阶段

---------

Co-authored-by: efve.zff <efve.zff@alibaba-inc.com>
This commit is contained in:
EfveZombie
2025-10-14 18:00:58 +08:00
committed by GitHub
parent d4b78b7ff1
commit 7386688320
3 changed files with 54 additions and 55 deletions

View File

@@ -206,10 +206,10 @@ start(int thread) {
create_thread(&pid[1], thread_timer, m);
create_thread(&pid[2], thread_socket, m);
static int weight[] = {
static int weight[] = {
-1, -1, -1, -1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, };
struct worker_parm wp[thread];
for (i=0;i<thread;i++) {
@@ -224,19 +224,19 @@ start(int thread) {
}
for (i=0;i<thread+3;i++) {
pthread_join(pid[i], NULL);
pthread_join(pid[i], NULL);
}
free_monitor(m);
}
static void
bootstrap(struct skynet_context * logger, const char * cmdline) {
bootstrap(uint32_t logger_handle, const char * cmdline) {
int sz = strlen(cmdline);
char name[sz+1];
char args[sz+1];
int arg_pos;
sscanf(cmdline, "%s", name);
sscanf(cmdline, "%s", name);
arg_pos = strlen(name);
if (arg_pos < sz) {
while(cmdline[arg_pos] == ' ') {
@@ -246,15 +246,19 @@ bootstrap(struct skynet_context * logger, const char * cmdline) {
} else {
args[0] = '\0';
}
struct skynet_context *ctx = skynet_context_new(name, args);
if (ctx == NULL) {
skynet_error(NULL, "Bootstrap error : %s\n", cmdline);
skynet_context_dispatchall(logger);
const uint32_t handle = skynet_context_new(name, args);
if (handle == 0) {
struct skynet_context *logger = skynet_handle_grab(logger_handle);
if (logger != NULL) {
skynet_error(NULL, "Bootstrap error : %s\n", cmdline);
skynet_context_dispatchall(logger);
skynet_context_release(logger);
}
exit(1);
}
}
void
void
skynet_start(struct skynet_config * config) {
// register SIGHUP for log file reopen
struct sigaction sa;
@@ -276,15 +280,15 @@ skynet_start(struct skynet_config * config) {
skynet_socket_init();
skynet_profile_enable(config->profile);
struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);
if (ctx == NULL) {
const uint32_t logger_handle = skynet_context_new(config->logservice, config->logger);
if (logger_handle == 0) {
fprintf(stderr, "Can't launch %s service\n", config->logservice);
exit(1);
}
skynet_handle_namehandle(skynet_context_handle(ctx), "logger");
skynet_handle_namehandle(logger_handle, "logger");
bootstrap(ctx, config->bootstrap);
bootstrap(logger_handle, config->bootstrap);
start(config->thread);