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

Commit54fc9dc

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 parentf6e1ee3 commit54fc9dc

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"
@@ -102,7 +104,20 @@ StartupProcShutdownHandler(SIGNAL_ARGS)
102104
intsave_errno=errno;
103105

104106
if (in_restore_command)
105-
proc_exit(1);
107+
{
108+
/*
109+
* If we are in a child process (e.g., forked by system() in
110+
* RestoreArchivedFile()), we don't want to call any exit callbacks.
111+
* The parent will take care of that.
112+
*/
113+
if (MyProcPid== (int)getpid())
114+
proc_exit(1);
115+
else
116+
{
117+
write_stderr_signal_safe("StartupProcShutdownHandler() called in child process\n");
118+
_exit(1);
119+
}
120+
}
106121
else
107122
shutdown_requested= true;
108123
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
@@ -826,6 +826,10 @@ ProcKill(int code, Datum arg)
826826

827827
Assert(MyProc!=NULL);
828828

829+
/* not safe if forked by system(), etc. */
830+
if (MyProc->pid!= (int)getpid())
831+
elog(PANIC,"ProcKill() called in child process");
832+
829833
/* Make sure we're out of the sync rep lists */
830834
SyncRepCleanupAtProcExit();
831835

@@ -955,6 +959,10 @@ AuxiliaryProcKill(int code, Datum arg)
955959

956960
Assert(proctype >=0&&proctype<NUM_AUXILIARY_PROCS);
957961

962+
/* not safe if forked by system(), etc. */
963+
if (MyProc->pid!= (int)getpid())
964+
elog(PANIC,"AuxiliaryProcKill() called in child process");
965+
958966
auxproc=&AuxiliaryProcs[proctype];
959967

960968
Assert(MyProc==auxproc);

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

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

35923592

3593+
/*
3594+
* Write a message to STDERR using only async-signal-safe functions. This can
3595+
* be used to safely emit a message from a signal handler.
3596+
*
3597+
* TODO: It is likely possible to safely do a limited amount of string
3598+
* interpolation (e.g., %s and %d), but that is not presently supported.
3599+
*/
3600+
void
3601+
write_stderr_signal_safe(constchar*str)
3602+
{
3603+
intnwritten=0;
3604+
intntotal=strlen(str);
3605+
3606+
while (nwritten<ntotal)
3607+
{
3608+
intrc;
3609+
3610+
rc=write(STDERR_FILENO,str+nwritten,ntotal-nwritten);
3611+
3612+
/* Just give up on error. There isn't much else we can do. */
3613+
if (rc==-1)
3614+
return;
3615+
3616+
nwritten+=rc;
3617+
}
3618+
}
3619+
3620+
35933621
/*
35943622
* Adjust the level of a recovery-related message per trace_recovery_messages.
35953623
*

‎src/include/utils/elog.h

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

456+
/*
457+
* Write a message to STDERR using only async-signal-safe functions. This can
458+
* be used to safely emit a message from a signal handler.
459+
*/
460+
externvoidwrite_stderr_signal_safe(constchar*fmt);
461+
456462
#endif/* ELOG_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp