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

Commitd0e7f95

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 parenta295684 commitd0e7f95

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
intsave_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 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
@@ -805,6 +805,10 @@ ProcKill(int code, Datum arg)
805805

806806
Assert(MyProc!=NULL);
807807

808+
/* not safe if forked by system(), etc. */
809+
if (MyProc->pid!= (int)getpid())
810+
elog(PANIC,"ProcKill() called in child process");
811+
808812
/* Make sure we're out of the sync rep lists */
809813
SyncRepCleanupAtProcExit();
810814

@@ -929,6 +933,10 @@ AuxiliaryProcKill(int code, Datum arg)
929933

930934
Assert(proctype >=0&&proctype<NUM_AUXILIARY_PROCS);
931935

936+
/* not safe if forked by system(), etc. */
937+
if (MyProc->pid!= (int)getpid())
938+
elog(PANIC,"AuxiliaryProcKill() called in child process");
939+
932940
auxproc=&AuxiliaryProcs[proctype];
933941

934942
Assert(MyProc==auxproc);

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

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

37053705

3706+
/*
3707+
* Write a message to STDERR using only async-signal-safe functions. This can
3708+
* be used to safely emit a message from a signal handler.
3709+
*
3710+
* TODO: It is likely possible to safely do a limited amount of string
3711+
* interpolation (e.g., %s and %d), but that is not presently supported.
3712+
*/
3713+
void
3714+
write_stderr_signal_safe(constchar*str)
3715+
{
3716+
intnwritten=0;
3717+
intntotal=strlen(str);
3718+
3719+
while (nwritten<ntotal)
3720+
{
3721+
intrc;
3722+
3723+
rc=write(STDERR_FILENO,str+nwritten,ntotal-nwritten);
3724+
3725+
/* Just give up on error. There isn't much else we can do. */
3726+
if (rc==-1)
3727+
return;
3728+
3729+
nwritten+=rc;
3730+
}
3731+
}
3732+
3733+
37063734
/*
37073735
* is_log_level_output -- is elevel logically >= log_min_level?
37083736
*

‎src/include/utils/elog.h

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

416+
/*
417+
* Write a message to STDERR using only async-signal-safe functions. This can
418+
* be used to safely emit a message from a signal handler.
419+
*/
420+
externvoidwrite_stderr_signal_safe(constchar*fmt);
421+
416422
#endif/* ELOG_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp