Skip to content

Commit 8f1537d

Browse files
committed
Fix possibility of self-deadlock in ResolveRecoveryConflictWithBufferPin().
The tests added in 9f8a050 failed nearly reliably on FreeBSD in CI, and occasionally on the buildfarm. That turns out to be caused not by a bug in the test, but by a longstanding bug in recovery conflict handling. The standby timeout handler, used by ResolveRecoveryConflictWithBufferPin(), executed SendRecoveryConflictWithBufferPin() inside a signal handler. A bad idea, because the deadlock timeout handler (or a spurious latch set) could have interrupted ProcWaitForSignal(). If unlucky that could cause a self-deadlock on ProcArrayLock, if the deadlock check is in SendRecoveryConflictWithBufferPin()->CancelDBBackends(). To fix, set a flag in StandbyTimeoutHandler(), and check the flag in ResolveRecoveryConflictWithBufferPin(). Subsequently the recovery conflict tests will be backpatched. Discussion: https://postgr.es/m/20220413002626.udl7lll7f3o7nre7@alap3.anarazel.de Backpatch: 10-
1 parent 21e1844 commit 8f1537d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/backend/storage/ipc/standby.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static HTAB *RecoveryLockLists;
4646

4747
/* Flags set by timeout handlers */
4848
static volatile sig_atomic_t got_standby_deadlock_timeout = false;
49+
static volatile sig_atomic_t got_standby_delay_timeout = false;
4950
static volatile sig_atomic_t got_standby_lock_timeout = false;
5051

5152
static void ResolveRecoveryConflictWithVirtualXIDs(VirtualTransactionId *waitlist,
@@ -793,7 +794,8 @@ ResolveRecoveryConflictWithBufferPin(void)
793794
}
794795

795796
/*
796-
* Wait to be signaled by UnpinBuffer().
797+
* Wait to be signaled by UnpinBuffer() or for the wait to be interrupted
798+
* by one of the timeouts established above.
797799
*
798800
* We assume that only UnpinBuffer() and the timeout requests established
799801
* above can wake us up here. WakeupRecovery() called by walreceiver or
@@ -802,7 +804,9 @@ ResolveRecoveryConflictWithBufferPin(void)
802804
*/
803805
ProcWaitForSignal(PG_WAIT_BUFFER_PIN);
804806

805-
if (got_standby_deadlock_timeout)
807+
if (got_standby_delay_timeout)
808+
SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
809+
else if (got_standby_deadlock_timeout)
806810
{
807811
/*
808812
* Send out a request for hot-standby backends to check themselves for
@@ -828,6 +832,7 @@ ResolveRecoveryConflictWithBufferPin(void)
828832
* individually, but that'd be slower.
829833
*/
830834
disable_all_timeouts(false);
835+
got_standby_delay_timeout = false;
831836
got_standby_deadlock_timeout = false;
832837
}
833838

@@ -887,8 +892,8 @@ CheckRecoveryConflictDeadlock(void)
887892
*/
888893

889894
/*
890-
* StandbyDeadLockHandler() will be called if STANDBY_DEADLOCK_TIMEOUT
891-
* occurs before STANDBY_TIMEOUT.
895+
* StandbyDeadLockHandler() will be called if STANDBY_DEADLOCK_TIMEOUT is
896+
* exceeded.
892897
*/
893898
void
894899
StandbyDeadLockHandler(void)
@@ -898,16 +903,11 @@ StandbyDeadLockHandler(void)
898903

899904
/*
900905
* StandbyTimeoutHandler() will be called if STANDBY_TIMEOUT is exceeded.
901-
* Send out a request to release conflicting buffer pins unconditionally,
902-
* so we can press ahead with applying changes in recovery.
903906
*/
904907
void
905908
StandbyTimeoutHandler(void)
906909
{
907-
/* forget any pending STANDBY_DEADLOCK_TIMEOUT request */
908-
disable_timeout(STANDBY_DEADLOCK_TIMEOUT, false);
909-
910-
SendRecoveryConflictWithBufferPin(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
910+
got_standby_delay_timeout = true;
911911
}
912912

913913
/*

0 commit comments

Comments
 (0)