From 9b6fc4151715696054e45a664696d82f9d4a6f57 Mon Sep 17 00:00:00 2001 From: zhoujiaqi Date: Mon, 14 Jul 2025 13:45:24 +0800 Subject: [PATCH] FIX: core dump when exit(0) happend in postgres The core: - 0 0x00007f7c24dee387 in raise () from /lib64/libc.so.6 - 1 0x00007f7c24defa78 in abort () from /lib64/libc.so.6 - 2 0x00007f7c252238e9 in __gnu_cxx::__verbose_terminate_handler() [clone .cold] () from /workspace/dist/database/lib/libstdc++.so.6 - 3 0x00007f7c2522f10a in __cxxabiv1::__terminate(void (*)()) () from /workspace/dist/database/lib/libstdc++.so.6 - 4 0x00007f7c2522f175 in std::terminate() () from /workspace/dist/database/lib/libstdc++.so.6 - 5 0x00007f7c2522fe93 in __cxa_pure_virtual () from /workspace/dist/database/lib/libstdc++.so.6 - 6 0x00005577fe3eb0b4 in gpos::CWStringBase::IsValid (this=0x5578034ff1c0 ) at CWStringBase.cpp:50 - 7 0x00005577fe41c23d in gpmd::CMDName::~CMDName (this=0x5578034ff1e0 , __in_chrg=) at CMDName.cpp:78 - 8 0x00007f7c24df1ce9 in __run_exit_handlers () from /lib64/libc.so.6 - 9 0x00007f7c24df1d37 in exit () from /lib64/libc.so.6 - 10 0x00005577fe089979 in proc_exit (code=0) at ipc.c:157 - 11 0x00005577fe0d4b2d in PostgresMain (argc=14, argv=0x5578078d8480, dbname=0x55780793a930 "template1", username=0x5578078dd7b0 "gpadmin") at postgres.c:6152 - 12 0x00005577fde4eb05 in main (argc=14, argv=0x5578078d8480) at main.c:263 In the #6 the object(`class CWStringConst`) is a static variable in the `class CMDName`, and the core happend when we try to call the `~CMDName` to destory the global variable. ``` CWStringConst CMDTypeOidGPDB::m_str = CWStringConst(GPOS_WSZ_LIT("oid")); CMDName CMDTypeOidGPDB::m_mdname(&m_str); ``` The reason for this core is that `m_name` has been destructed before its own class CMDName. I added a variable to prove: ``` CWStringConst::~CWStringConst() { if (m_owns_memory && m_w_str_buffer != &m_empty_wcstr) { GPOS_DELETE_ARRAY(m_w_str_buffer); } is_invalid = true; // once ~CWStringConst() call, current is_invalid will be true, which default is false. } ``` And gdb the core ``` (gdb) f 6 (gdb) p ((CWStringConst *)this)->is_invalid $4 = true // m_name is invalid ``` --- src/backend/gporca/libnaucrates/src/md/CMDName.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/gporca/libnaucrates/src/md/CMDName.cpp b/src/backend/gporca/libnaucrates/src/md/CMDName.cpp index abd29531958..1e9a837c77d 100644 --- a/src/backend/gporca/libnaucrates/src/md/CMDName.cpp +++ b/src/backend/gporca/libnaucrates/src/md/CMDName.cpp @@ -75,7 +75,9 @@ CMDName::CMDName(const CMDName &name) //--------------------------------------------------------------------------- CMDName::~CMDName() { - GPOS_ASSERT(m_name->IsValid()); + // Some of `m_name` is reference only, there is no guarantee + // that the referenced object lives longer than the CMDName + // object when running the destructor of a process. if (m_deep_copy) {