postgresql/src/backend/postmaster
Nathan Bossart 54fc9dca5b Avoid calling proc_exit() in processes forked by system().
The SIGTERM handler for the startup process immediately calls
proc_exit() for the duration of the restore_command, i.e., a call
to system().  This system() call forks a new process to execute the
shell command, and this child process inherits the parent's signal
handlers.  If both the parent and child processes receive SIGTERM,
both will attempt to call proc_exit().  This can end badly.  For
example, both processes will try to remove themselves from the
PGPROC shared array.

To fix this problem, this commit adds a check in
StartupProcShutdownHandler() to see whether MyProcPid == getpid().
If they match, this is the parent process, and we can proc_exit()
like before.  If they do not match, this is a child process, and we
just emit a message to STDERR (in a signal safe manner) and
_exit(), thereby skipping any problematic exit callbacks.

This commit also adds checks in proc_exit(), ProcKill(), and
AuxiliaryProcKill() that verify they are not being called within
such child processes.

Suggested-by: Andres Freund
Reviewed-by: Thomas Munro, Andres Freund
Discussion: https://postgr.es/m/Y9nGDSgIm83FHcad%40paquier.xyz
Discussion: https://postgr.es/m/20230223231503.GA743455%40nathanxps13
Backpatch-through: 11
2023-10-17 10:42:12 -05:00
..
Makefile Partially deduplicate interrupt handling for background processes. 2019-12-17 13:14:28 -05:00
autovacuum.c Handle DROP DATABASE getting interrupted 2023-07-13 13:03:33 -07:00
bgworker.c Be more careful about barriers when releasing BackgroundWorkerSlots. 2021-05-15 12:21:06 -04:00
bgwriter.c Flush WAL stats in bgwriter 2023-10-02 12:50:32 +03:00
checkpointer.c Wake up for latches in CheckpointWriteDelay(). 2022-03-16 13:57:07 +13:00
fork_process.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
interrupt.c Update copyright for 2021 2021-01-02 13:06:25 -05:00
pgarch.c Make archiver process handle barrier events. 2021-06-18 17:57:09 +09:00
pgstat.c Fix performance regression from session statistics. 2021-09-16 02:10:57 -07:00
postmaster.c Initialize ListenSocket array earlier. 2023-08-29 09:12:24 +03:00
startup.c Avoid calling proc_exit() in processes forked by system(). 2023-10-17 10:42:12 -05:00
syslogger.c Initial pgindent and pgperltidy run for v14. 2021-05-12 13:14:10 -04:00
walwriter.c Revert "Avoid creating archive status ".ready" files too early" 2021-09-04 12:14:30 -04:00