From 9641fb0ef1d708bb9ec8c00ea5ec694829e4fd67 Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Thu, 23 Jul 2020 14:25:21 +0300 Subject: [PATCH] Fixing various router crashes on exit caused by runtime pool free. Currently, the router exits without waiting for the worker threads to stop. There is a short gap between the runtime memory pool's free and the exit, during which a worker thread may try to access a runtime structure. In turn, this may cause a crash. For now, it is better to keep this memory allocated. --- src/nxt_runtime.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index 5aa061dd..694ce74d 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -527,7 +527,7 @@ nxt_runtime_stop_all_processes(nxt_task_t *task, nxt_runtime_t *rt) static void nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) { - int status; + int status, engine_count; nxt_runtime_t *rt; nxt_process_t *process; nxt_event_engine_t *engine; @@ -571,14 +571,25 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) } nxt_runtime_process_loop; - if (rt->port_by_type[rt->type] != NULL) { - nxt_port_use(task, rt->port_by_type[rt->type], -1); - } - - nxt_thread_mutex_destroy(&rt->processes_mutex); - status = rt->status; - nxt_mp_destroy(rt->mem_pool); + + engine_count = 0; + + nxt_queue_each(engine, &rt->engines, nxt_event_engine_t, link) { + + engine_count++; + + } nxt_queue_loop; + + if (engine_count <= 1) { + if (rt->port_by_type[rt->type] != NULL) { + nxt_port_use(task, rt->port_by_type[rt->type], -1); + } + + nxt_thread_mutex_destroy(&rt->processes_mutex); + + nxt_mp_destroy(rt->mem_pool); + } nxt_debug(task, "exit: %d", status);