From b9ed3384cb620618c2622e387d1770bb8c43ff13 Mon Sep 17 00:00:00 2001 From: Tiago Natel de Moura Date: Thu, 13 Aug 2020 12:25:52 +0100 Subject: [PATCH] Fixed error handling of prefork callback. Previously, an error during the prefork phase triggered assert: src/nxt_port.c:27 assertion failed: port->pair[0] == -1 and resulted in exiting of the main process. This could be easily reproduced by pushing a configuration with "rootfs", when daemon is running without required permissions. --- src/nxt_main_process.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index a16e44d3..48eb2abb 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -605,25 +605,22 @@ nxt_main_start_process(nxt_task_t *task, nxt_process_t *process) nxt_process_port_add(task, process, port); - nxt_process_use(task, process, -1); - - ret = NXT_ERROR; - tmp_mp = NULL; - ret = nxt_port_socket_init(task, port, 0); if (nxt_slow_path(ret != NXT_OK)) { - goto fail; + goto free_port; } tmp_mp = nxt_mp_create(1024, 128, 256, 32); - if (tmp_mp == NULL) { - goto fail; + if (nxt_slow_path(tmp_mp == NULL)) { + ret = NXT_ERROR; + + goto close_port; } if (init->prefork) { ret = init->prefork(task, process, tmp_mp); if (nxt_slow_path(ret != NXT_OK)) { - goto fail; + goto free_mempool; } } @@ -632,18 +629,22 @@ nxt_main_start_process(nxt_task_t *task, nxt_process_t *process) switch (pid) { case -1: - nxt_port_close(task, port); + ret = NXT_ERROR; break; case 0: /* The child process: return to the event engine work queue loop. */ + nxt_process_use(task, process, -1); + ret = NXT_AGAIN; break; default: /* The main process created a new process. */ + nxt_process_use(task, process, -1); + nxt_port_read_close(port); nxt_port_write_enable(task, port); @@ -651,14 +652,20 @@ nxt_main_start_process(nxt_task_t *task, nxt_process_t *process) break; } -fail: +free_mempool: + + nxt_mp_destroy(tmp_mp); + +close_port: + + if (nxt_slow_path(ret == NXT_ERROR)) { + nxt_port_close(task, port); + } + +free_port: nxt_port_use(task, port, -1); - if (nxt_fast_path(tmp_mp != NULL)) { - nxt_mp_destroy(tmp_mp); - } - return ret; }