Skip to content

Commit e2e1690

Browse files
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
1 parent 07eb22a commit e2e1690

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

src/backend/postmaster/startup.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,20 @@ StartupProcShutdownHandler(SIGNAL_ARGS)
132132
int save_errno = errno;
133133

134134
if (in_restore_command)
135-
proc_exit(1);
135+
{
136+
/*
137+
* If we are in a child process (e.g., forked by system() in
138+
* RestoreArchivedFile()), we don't want to call any exit callbacks.
139+
* The parent will take care of that.
140+
*/
141+
if (MyProcPid == (int) getpid())
142+
proc_exit(1);
143+
else
144+
{
145+
write_stderr_signal_safe("StartupProcShutdownHandler() called in child process\n");
146+
_exit(1);
147+
}
148+
}
136149
else
137150
shutdown_requested = true;
138151
WakeupRecovery();

src/backend/storage/ipc/ipc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ static int on_proc_exit_index,
103103
void
104104
proc_exit(int code)
105105
{
106+
/* not safe if forked by system(), etc. */
107+
if (MyProcPid != (int) getpid())
108+
elog(PANIC, "proc_exit() called in child process");
109+
106110
/* Clean up everything that must be cleaned up */
107111
proc_exit_prepare(code);
108112

src/backend/storage/lmgr/proc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,10 @@ ProcKill(int code, Datum arg)
822822

823823
Assert(MyProc != NULL);
824824

825+
/* not safe if forked by system(), etc. */
826+
if (MyProc->pid != (int) getpid())
827+
elog(PANIC, "ProcKill() called in child process");
828+
825829
/* Make sure we're out of the sync rep lists */
826830
SyncRepCleanupAtProcExit();
827831

@@ -946,6 +950,10 @@ AuxiliaryProcKill(int code, Datum arg)
946950

947951
Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
948952

953+
/* not safe if forked by system(), etc. */
954+
if (MyProc->pid != (int) getpid())
955+
elog(PANIC, "AuxiliaryProcKill() called in child process");
956+
949957
auxproc = &AuxiliaryProcs[proctype];
950958

951959
Assert(MyProc == auxproc);

src/backend/utils/error/elog.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,6 +3416,34 @@ write_stderr(const char *fmt,...)
34163416
}
34173417

34183418

3419+
/*
3420+
* Write a message to STDERR using only async-signal-safe functions. This can
3421+
* be used to safely emit a message from a signal handler.
3422+
*
3423+
* TODO: It is likely possible to safely do a limited amount of string
3424+
* interpolation (e.g., %s and %d), but that is not presently supported.
3425+
*/
3426+
void
3427+
write_stderr_signal_safe(const char *str)
3428+
{
3429+
int nwritten = 0;
3430+
int ntotal = strlen(str);
3431+
3432+
while (nwritten < ntotal)
3433+
{
3434+
int rc;
3435+
3436+
rc = write(STDERR_FILENO, str + nwritten, ntotal - nwritten);
3437+
3438+
/* Just give up on error. There isn't much else we can do. */
3439+
if (rc == -1)
3440+
return;
3441+
3442+
nwritten += rc;
3443+
}
3444+
}
3445+
3446+
34193447
/*
34203448
* is_log_level_output -- is elevel logically >= log_min_level?
34213449
*

src/include/utils/elog.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,10 @@ extern void set_syslog_parameters(const char *ident, int facility);
433433
*/
434434
extern void write_stderr(const char *fmt,...) pg_attribute_printf(1, 2);
435435

436+
/*
437+
* Write a message to STDERR using only async-signal-safe functions. This can
438+
* be used to safely emit a message from a signal handler.
439+
*/
440+
extern void write_stderr_signal_safe(const char *fmt);
441+
436442
#endif /* ELOG_H */

0 commit comments

Comments
 (0)