]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Avoid calling proc_exit() in processes forked by system().
authorNathan Bossart <nathan@postgresql.org>
Tue, 17 Oct 2023 15:42:06 +0000 (10:42 -0500)
committerNathan Bossart <nathan@postgresql.org>
Tue, 17 Oct 2023 15:42:06 +0000 (10:42 -0500)
commitc9265ae80b6aa02daef620465800b03967433a39
tree867f3af7c07b6ca20a464862500894c75584d793
parent0d1a7cd14ee130ae5a1b31dd80a6aadfe9da67de
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
src/backend/postmaster/startup.c
src/backend/storage/ipc/ipc.c
src/backend/storage/lmgr/proc.c
src/backend/utils/error/elog.c
src/include/utils/elog.h