Skip to content

Commit ebe3344

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 62acbda commit ebe3344

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/backend/storage/ipc/procsignal.c

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

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

src/backend/storage/lmgr/proc.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ ProcKill(int code, Datum arg)
760760
{
761761
/* use volatile pointer to prevent code rearrangement */
762762
volatile PROC_HDR *procglobal = ProcGlobal;
763+
PGPROC *proc;
763764

764765
Assert(MyProc != NULL);
765766

@@ -784,26 +785,29 @@ ProcKill(int code, Datum arg)
784785
*/
785786
LWLockReleaseAll();
786787

787-
/* Release ownership of the process's latch, too */
788-
DisownLatch(&MyProc->procLatch);
788+
/*
789+
* Clear MyProc first; then disown the process latch. This is so that
790+
* signal handlers won't try to clear the process latch after it's no
791+
* longer ours.
792+
*/
793+
proc = MyProc;
794+
MyProc = NULL;
795+
DisownLatch(&proc->procLatch);
789796

790797
SpinLockAcquire(ProcStructLock);
791798

792799
/* Return PGPROC structure (and semaphore) to appropriate freelist */
793800
if (IsAnyAutoVacuumProcess())
794801
{
795-
MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
796-
procglobal->autovacFreeProcs = MyProc;
802+
proc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
803+
procglobal->autovacFreeProcs = proc;
797804
}
798805
else
799806
{
800-
MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
801-
procglobal->freeProcs = MyProc;
807+
proc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
808+
procglobal->freeProcs = proc;
802809
}
803810

804-
/* PGPROC struct isn't mine anymore */
805-
MyProc = NULL;
806-
807811
/* Update shared estimate of spins_per_delay */
808812
procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
809813

@@ -832,6 +836,7 @@ AuxiliaryProcKill(int code, Datum arg)
832836
{
833837
int proctype = DatumGetInt32(arg);
834838
PGPROC *auxproc PG_USED_FOR_ASSERTS_ONLY;
839+
PGPROC *proc;
835840

836841
Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
837842

@@ -842,16 +847,19 @@ AuxiliaryProcKill(int code, Datum arg)
842847
/* Release any LW locks I am holding (see notes above) */
843848
LWLockReleaseAll();
844849

845-
/* Release ownership of the process's latch, too */
846-
DisownLatch(&MyProc->procLatch);
850+
/*
851+
* Clear MyProc first; then disown the process latch. This is so that
852+
* signal handlers won't try to clear the process latch after it's no
853+
* longer ours.
854+
*/
855+
proc = MyProc;
856+
MyProc = NULL;
857+
DisownLatch(&proc->procLatch);
847858

848859
SpinLockAcquire(ProcStructLock);
849860

850861
/* Mark auxiliary proc no longer in use */
851-
MyProc->pid = 0;
852-
853-
/* PGPROC struct isn't mine anymore */
854-
MyProc = NULL;
862+
proc->pid = 0;
855863

856864
/* Update shared estimate of spins_per_delay */
857865
ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);

0 commit comments

Comments
 (0)