From 7818e1bb188c5a11d58dd9586ccb868836ca9596 Mon Sep 17 00:00:00 2001 From: Kiran Date: Wed, 27 Sep 2023 06:20:30 -0400 Subject: [PATCH] Fix segfault if root task is NULL (#51471) In `jl_print_task_backtraces()`. Follow-on to https://github.com/JuliaLang/julia/pull/51430. --- src/stackwalk.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/stackwalk.c b/src/stackwalk.c index f08ff559e3f041..ffe00466a0ce47 100644 --- a/src/stackwalk.c +++ b/src/stackwalk.c @@ -1169,23 +1169,30 @@ JL_DLLEXPORT void jl_print_task_backtraces(int show_done) JL_NOTSAFEPOINT continue; } jl_ptls_t ptls2 = allstates[i]; - if (ptls2 == NULL) + if (ptls2 == NULL) { continue; + } small_arraylist_t *live_tasks = &ptls2->heap.live_tasks; size_t n = mtarraylist_length(live_tasks); + int t_state = JL_TASK_STATE_DONE; jl_task_t *t = ptls2->root_task; - int t_state = jl_atomic_load_relaxed(&t->_state); + if (t != NULL) + t_state = jl_atomic_load_relaxed(&t->_state); jl_safe_printf("==== Thread %d created %zu live tasks\n", ptls2->tid + 1, n + (t_state != JL_TASK_STATE_DONE)); if (show_done || t_state != JL_TASK_STATE_DONE) { jl_safe_printf(" ---- Root task (%p)\n", ptls2->root_task); - jl_safe_printf(" (sticky: %d, started: %d, state: %d, tid: %d)\n", - t->sticky, t->started, t_state, - jl_atomic_load_relaxed(&t->tid) + 1); - if (t->stkbuf != NULL) - jlbacktracet(t); - else - jl_safe_printf(" no stack\n"); + if (t != NULL) { + jl_safe_printf(" (sticky: %d, started: %d, state: %d, tid: %d)\n", + t->sticky, t->started, t_state, + jl_atomic_load_relaxed(&t->tid) + 1); + if (t->stkbuf != NULL) { + jlbacktracet(t); + } + else { + jl_safe_printf(" no stack\n"); + } + } jl_safe_printf(" ---- End root task\n"); }