Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d198171

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 637fab6 commit d198171

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/backend/storage/ipc/procsignal.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ CleanupProcSignalState(int status, Datum arg)
149149
slot = &ProcSignalSlots[pss_idx - 1];
150150
Assert(slot == MyProcSignalSlot);
151151

152+
/*
153+
* Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
154+
* won't try to access it after it's no longer ours (and perhaps even
155+
* after we've unmapped the shared memory segment).
156+
*/
157+
MyProcSignalSlot = NULL;
158+
152159
/* sanity check */
153160
if (slot->pss_pid != MyProcPid)
154161
{
@@ -285,7 +292,7 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
285292
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
286293
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
287294

288-
if (set_latch_on_sigusr1)
295+
if (set_latch_on_sigusr1 && MyProc != NULL)
289296
SetLatch(&MyProc->procLatch);
290297

291298
latch_sigusr1_handler();

src/backend/storage/lmgr/proc.c

+25-17
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ ProcKill(int code, Datum arg)
773773
{
774774
/* use volatile pointer to prevent code rearrangement */
775775
volatile PROC_HDR *procglobal = ProcGlobal;
776+
PGPROC *proc;
776777

777778
Assert(MyProc != NULL);
778779

@@ -797,31 +798,34 @@ ProcKill(int code, Datum arg)
797798
*/
798799
LWLockReleaseAll();
799800

800-
/* Release ownership of the process's latch, too */
801-
DisownLatch(&MyProc->procLatch);
801+
/*
802+
* Clear MyProc first; then disown the process latch. This is so that
803+
* signal handlers won't try to clear the process latch after it's no
804+
* longer ours.
805+
*/
806+
proc = MyProc;
807+
MyProc = NULL;
808+
DisownLatch(&proc->procLatch);
802809

803810
SpinLockAcquire(ProcStructLock);
804811

805812
/* Return PGPROC structure (and semaphore) to appropriate freelist */
806813
if (IsAnyAutoVacuumProcess())
807814
{
808-
MyProc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
809-
procglobal->autovacFreeProcs = MyProc;
815+
proc->links.next = (SHM_QUEUE *) procglobal->autovacFreeProcs;
816+
procglobal->autovacFreeProcs = proc;
810817
}
811818
else if (IsBackgroundWorker)
812819
{
813-
MyProc->links.next = (SHM_QUEUE *) procglobal->bgworkerFreeProcs;
814-
procglobal->bgworkerFreeProcs = MyProc;
820+
proc->links.next = (SHM_QUEUE *) procglobal->bgworkerFreeProcs;
821+
procglobal->bgworkerFreeProcs = proc;
815822
}
816823
else
817824
{
818-
MyProc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
819-
procglobal->freeProcs = MyProc;
825+
proc->links.next = (SHM_QUEUE *) procglobal->freeProcs;
826+
procglobal->freeProcs = proc;
820827
}
821828

822-
/* PGPROC struct isn't mine anymore */
823-
MyProc = NULL;
824-
825829
/* Update shared estimate of spins_per_delay */
826830
procglobal->spins_per_delay = update_spins_per_delay(procglobal->spins_per_delay);
827831

@@ -850,6 +854,7 @@ AuxiliaryProcKill(int code, Datum arg)
850854
{
851855
int proctype = DatumGetInt32(arg);
852856
PGPROC *auxproc PG_USED_FOR_ASSERTS_ONLY;
857+
PGPROC *proc;
853858

854859
Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
855860

@@ -860,16 +865,19 @@ AuxiliaryProcKill(int code, Datum arg)
860865
/* Release any LW locks I am holding (see notes above) */
861866
LWLockReleaseAll();
862867

863-
/* Release ownership of the process's latch, too */
864-
DisownLatch(&MyProc->procLatch);
868+
/*
869+
* Clear MyProc first; then disown the process latch. This is so that
870+
* signal handlers won't try to clear the process latch after it's no
871+
* longer ours.
872+
*/
873+
proc = MyProc;
874+
MyProc = NULL;
875+
DisownLatch(&proc->procLatch);
865876

866877
SpinLockAcquire(ProcStructLock);
867878

868879
/* Mark auxiliary proc no longer in use */
869-
MyProc->pid = 0;
870-
871-
/* PGPROC struct isn't mine anymore */
872-
MyProc = NULL;
880+
proc->pid = 0;
873881

874882
/* Update shared estimate of spins_per_delay */
875883
ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);

0 commit comments

Comments
 (0)