Skip to content

Commit 1405a2d

Browse files
committed
Fast exit in autovacuum process.
1 parent 49297ef commit 1405a2d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/deeplake_pg/pg_deeplake.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
#include <deeplake_api/deeplake_api.hpp>
66
#include <deeplake_core/deeplake_index_type.hpp>
77

8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
#include <storage/ipc.h>
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
18+
namespace {
19+
20+
// Exit handler that uses _exit() to avoid C++ static destructor crashes.
21+
// PostgreSQL background workers (autovacuum, parallel workers, etc.) can crash
22+
// during normal exit when C++ static objects are destroyed in unpredictable order.
23+
void deeplake_quick_exit(int code, Datum arg)
24+
{
25+
_exit(code);
26+
}
27+
28+
} // anonymous namespace
29+
830
namespace pg {
931

1032
QueryDesc* query_info::current_query_desc = nullptr;
@@ -339,6 +361,10 @@ void init_deeplake()
339361
}
340362
initialized = true;
341363

364+
// Register exit handler first (runs last due to LIFO order) to use _exit()
365+
// and avoid C++ static destructor crashes in background workers.
366+
on_proc_exit(deeplake_quick_exit, 0);
367+
342368
constexpr int THREAD_POOL_MULTIPLIER = 8; // Threads per CPU core for async operations
343369
deeplake_api::initialize(std::make_shared<pg::logger_adapter>(), THREAD_POOL_MULTIPLIER * base::system_report::cpu_cores());
344370

0 commit comments

Comments
 (0)