Install system exit callback only after initialization of shared memory.
authorMuhammad Usama <m.usama@gmail.com>
Sun, 23 Jul 2023 17:34:05 +0000 (22:34 +0500)
committerMuhammad Usama <m.usama@gmail.com>
Sun, 23 Jul 2023 17:41:45 +0000 (22:41 +0500)
When the on-exit callback gets called because of a failure to acquire
shared memory. The cleanup function can produce a segfault while accessing
process_info, that lives in shared memory.
Although we can also fix this by bailing out from the exit callback when
process_info is NULL but installing the function after successful initialization
of shared memory is a better approach as the rest of the system always assumes
the process_info can never be NULL, and also, there is nothing to
clean up before child processes are spawned.

src/main/pgpool_main.c

index 252ea6da23b646e91ab17f2229e2286b513ee520..66c342034830fd40b07941c1bc0cbd68d17bed02 100644 (file)
@@ -313,11 +313,6 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps)
         */
        read_status_file(discard_status);
 
-       /*
-        * install the call back for preparation of system exit
-        */
-       on_system_exit(system_will_go_down, (Datum) NULL);
-
        /* set unix domain socket path for connections to pgpool */
        for (i = 0; i < pool_config->num_unix_socket_directories; i++)
        {
@@ -394,6 +389,11 @@ PgpoolMain(bool discard_status, bool clear_memcache_oidmaps)
 
        initialize_shared_mem_objects(clear_memcache_oidmaps);
 
+       /*
+        * install the callback for preparation of system exit
+        */
+       on_system_exit(system_will_go_down, (Datum) NULL);
+
        /* setup signal handlers */
        pool_signal(SIGCHLD, reap_handler);
        pool_signal(SIGUSR1, sigusr1_handler);