From d4c59b82d885aaa8560dfbba82321edd515e18ca Mon Sep 17 00:00:00 2001 From: TotaJ Date: Sat, 17 Jul 2021 15:59:10 +0800 Subject: [PATCH] Fix stop gaussdb failed when init thread failed. --- src/gausskernel/process/main/main.cpp | 1 + .../process/postmaster/postmaster.cpp | 16 ++++++++++++++-- .../process/threadpool/knl_thread.cpp | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/gausskernel/process/main/main.cpp b/src/gausskernel/process/main/main.cpp index e99587383..d3a528646 100644 --- a/src/gausskernel/process/main/main.cpp +++ b/src/gausskernel/process/main/main.cpp @@ -114,6 +114,7 @@ int main(int argc, char* argv[]) PmTopMemoryContext = t_thrd.top_mem_cxt; knl_thread_init(MASTER_THREAD); + t_thrd.bn = NULL; t_thrd.fake_session = create_session_context(t_thrd.top_mem_cxt, 0); t_thrd.fake_session->status = KNL_SESS_FAKE; diff --git a/src/gausskernel/process/postmaster/postmaster.cpp b/src/gausskernel/process/postmaster/postmaster.cpp index c27c5b165..c601ad65c 100755 --- a/src/gausskernel/process/postmaster/postmaster.cpp +++ b/src/gausskernel/process/postmaster/postmaster.cpp @@ -10074,6 +10074,20 @@ int GaussDbThreadMain(knl_thread_arg* arg) /* Do this sooner rather than later... */ IsUnderPostmaster = true; /* we are a postmaster subprocess now */ Assert(thread_role == arg->role); + /* + * We should set bn at the beginning of this function, cause if we meet some error before set bn, then we + * can't set t_thrd.bn->dead_end to true(check gs_thread_exit), which will lead CleanupBackend failed. + * The logic of get child slot refer to PortInitialize -> read_backend_variables -> restore_backend_variables + */ + int childSlot = 0; + if (arg != NULL) { + if (arg->role == RPC_WORKER) { + childSlot = backend_save_para.MyPMChildSlot; + } else { + childSlot = ((BackendParameters*)arg->save_para)->MyPMChildSlot; + } + } + t_thrd.bn = GetBackend(childSlot); /* Check this thread will use reserved memory or not */ is_memory_backend_reserved(arg); /* Initialize the Memory Protection at the thread level */ @@ -10130,8 +10144,6 @@ int GaussDbThreadMain(knl_thread_arg* arg) PortInitialize(&port, arg); - t_thrd.bn = GetBackend(t_thrd.proc_cxt.MyPMChildSlot); - /* We don't need read GUC variables */ if (!FencedUDFMasterMode) { /* Read in remaining GUC variables */ diff --git a/src/gausskernel/process/threadpool/knl_thread.cpp b/src/gausskernel/process/threadpool/knl_thread.cpp index 7cc764e2d..d1308dd7a 100644 --- a/src/gausskernel/process/threadpool/knl_thread.cpp +++ b/src/gausskernel/process/threadpool/knl_thread.cpp @@ -1478,11 +1478,11 @@ void knl_thread_mot_init() void knl_thread_init(knl_thread_role role) { + /* doesn't init t_thrd.bn, check GaussDbThreadMain for detail */ t_thrd.role = role; t_thrd.subrole = NO_SUBROLE; t_thrd.proc = NULL; t_thrd.pgxact = NULL; - t_thrd.bn = NULL; t_thrd.myLogicTid = 10000; t_thrd.fake_session = NULL; t_thrd.threadpool_cxt.reaper_dead_session = false;