Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitac1dfc3

Browse files
Avoid calling proc_exit() in processes forked by system().
The SIGTERM handler for the startup process immediately callsproc_exit() for the duration of the restore_command, i.e., a callto system(). This system() call forks a new process to execute theshell command, and this child process inherits the parent's signalhandlers. If both the parent and child processes receive SIGTERM,both will attempt to call proc_exit(). This can end badly. Forexample, both processes will try to remove themselves from thePGPROC shared array.To fix this problem, this commit adds a check inStartupProcShutdownHandler() 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 wejust 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(), andAuxiliaryProcKill() that verify they are not being called withinsuch child processes.Suggested-by: Andres FreundReviewed-by: Thomas Munro, Andres FreundDiscussion:https://postgr.es/m/Y9nGDSgIm83FHcad%40paquier.xyzDiscussion:https://postgr.es/m/20230223231503.GA743455%40nathanxps13Backpatch-through: 11
1 parent1e67817 commitac1dfc3

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

‎src/backend/postmaster/startup.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
#include"postgres.h"
2121

22+
#include<unistd.h>
23+
2224
#include"access/xlog.h"
2325
#include"libpq/pqsignal.h"
2426
#include"miscadmin.h"
@@ -91,7 +93,20 @@ StartupProcShutdownHandler(SIGNAL_ARGS)
9193
intsave_errno=errno;
9294

9395
if (in_restore_command)
94-
proc_exit(1);
96+
{
97+
/*
98+
* If we are in a child process (e.g., forked by system() in
99+
* RestoreArchivedFile()), we don't want to call any exit callbacks.
100+
* The parent will take care of that.
101+
*/
102+
if (MyProcPid== (int)getpid())
103+
proc_exit(1);
104+
else
105+
{
106+
write_stderr_signal_safe("StartupProcShutdownHandler() called in child process\n");
107+
_exit(1);
108+
}
109+
}
95110
else
96111
shutdown_requested= true;
97112
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 inton_proc_exit_index,
103103
void
104104
proc_exit(intcode)
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
@@ -821,6 +821,10 @@ ProcKill(int code, Datum arg)
821821

822822
Assert(MyProc!=NULL);
823823

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

@@ -945,6 +949,10 @@ AuxiliaryProcKill(int code, Datum arg)
945949

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

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

950958
Assert(MyProc==auxproc);

‎src/backend/utils/error/elog.c

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

34593459

3460+
/*
3461+
* Write a message to STDERR using only async-signal-safe functions. This can
3462+
* be used to safely emit a message from a signal handler.
3463+
*
3464+
* TODO: It is likely possible to safely do a limited amount of string
3465+
* interpolation (e.g., %s and %d), but that is not presently supported.
3466+
*/
3467+
void
3468+
write_stderr_signal_safe(constchar*str)
3469+
{
3470+
intnwritten=0;
3471+
intntotal=strlen(str);
3472+
3473+
while (nwritten<ntotal)
3474+
{
3475+
intrc;
3476+
3477+
rc=write(STDERR_FILENO,str+nwritten,ntotal-nwritten);
3478+
3479+
/* Just give up on error. There isn't much else we can do. */
3480+
if (rc==-1)
3481+
return;
3482+
3483+
nwritten+=rc;
3484+
}
3485+
}
3486+
3487+
34603488
/*
34613489
* is_log_level_output -- is elevel logically >= log_min_level?
34623490
*

‎src/include/utils/elog.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,10 @@ extern void set_syslog_parameters(const char *ident, int facility);
436436
*/
437437
externvoidwrite_stderr(constchar*fmt,...)pg_attribute_printf(1,2);
438438

439+
/*
440+
* Write a message to STDERR using only async-signal-safe functions. This can
441+
* be used to safely emit a message from a signal handler.
442+
*/
443+
externvoidwrite_stderr_signal_safe(constchar*fmt);
444+
439445
#endif/* ELOG_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp