Skip to content

Commit 798243a

Browse files
committed
Clear MyProc and MyProcSignalState before they become invalid.
Evidence from buildfarm member crake suggests that the new test_shm_mq module is routinely crashing the server due to the arrival of a SIGUSR1 after the shared memory segment has been unmapped. Although processes using the new dynamic background worker facilities are more likely to receive a SIGUSR1 around this time, the problem is also possible on older branches, so I'm back-patching the parts of this change that apply to older branches as far as they apply. It's already generally the case that code checks whether these pointers are NULL before deferencing them, so the important thing is mostly to make sure that they do get set to NULL before they become invalid. But in master, there's one case in procsignal_sigusr1_handler that lacks a NULL guard, so add that. Patch by me; review by Tom Lane.
1 parent d17a667 commit 798243a

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/backend/storage/ipc/procsignal.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ CleanupProcSignalState(int status, Datum arg)
142142
slot = &ProcSignalSlots[pss_idx - 1];
143143
Assert(slot == MyProcSignalSlot);
144144

145+
/*
146+
* Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
147+
* won't try to access it after it's no longer ours (and perhaps even
148+
* after we've unmapped the shared memory segment).
149+
*/
150+
MyProcSignalSlot = NULL;
151+
145152
/* sanity check */
146153
if (slot->pss_pid != MyProcPid)
147154
{

src/backend/storage/lmgr/proc.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ ProcKill(int code, Datum arg)
661661
{
662662
/* use volatile pointer to prevent code rearrangement */
663663
volatile PROC_HDR *procglobal = ProcGlobal;
664+
PGPROC *proc;
664665

665666
Assert(MyProc != NULL);
666667

@@ -671,23 +672,28 @@ ProcKill(int code, Datum arg)
671672
*/
672673
LWLockReleaseAll();
673674

675+
/*
676+
* Clear MyProc first before after putting it back on the global list,
677+
* so that signal handlers won't try to access it after it's no longer
678+
* ours.
679+
*/
680+
proc = MyProc;
681+
MyProc = NULL;
682+
674683
SpinLockAcquire(ProcStructLock);
675684

676685
/* Return PGPROC structure (and semaphore) to appropriate freelist */
677686
if (IsAnyAutoVacuumProcess())
678687
{
679-
MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
680-
procglobal->autovacFreeProcs = MyProc;
688+
proc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
689+
procglobal->autovacFreeProcs = proc;
681690
}
682691
else
683692
{
684-
MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
685-
procglobal->freeProcs = MyProc;
693+
proc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
694+
procglobal->freeProcs = proc;
686695
}
687696

688-
/* PGPROC struct isn't mine anymore */
689-
MyProc = NULL;
690-
691697
/* Update shared estimate of spins_per_delay */
692698
procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
693699

@@ -716,6 +722,7 @@ AuxiliaryProcKill(int code, Datum arg)
716722
{
717723
int proctype = DatumGetInt32(arg);
718724
PGPROC *auxproc;
725+
PGPROC *proc;
719726

720727
Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
721728

@@ -726,13 +733,18 @@ AuxiliaryProcKill(int code, Datum arg)
726733
/* Release any LW locks I am holding (see notes above) */
727734
LWLockReleaseAll();
728735

736+
/*
737+
* Clear MyProc first before after putting it back on the global list,
738+
* so that signal handlers won't try to access it after it's no longer
739+
* ours.
740+
*/
741+
proc = MyProc;
742+
MyProc = NULL;
743+
729744
SpinLockAcquire(ProcStructLock);
730745

731746
/* Mark auxiliary proc no longer in use */
732-
MyProc->pid = 0;
733-
734-
/* PGPROC struct isn't mine anymore */
735-
MyProc = NULL;
747+
proc->pid = 0;
736748

737749
/* Update shared estimate of spins_per_delay */
738750
ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);

0 commit comments

Comments
 (0)