Fix two race conditions in processing interrupts
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
While I was working on my Latches vs Interrupts patch set, AI review
pointed out two race conditions that are present on master, too:
1. If a new catchup signal arrives while we're processing a previous
catchup signal, the new signal can be lost.
This is a pretty straightforward race condition.
ReceiveSharedInvalidMessages() clears catchupInterruptPending, after it
has processed all the messages. If another catchup interrupt arrives in
between, catchupInterruptPending is cleared even though there are now
more messages pending, and the "signaled" flag in shared memory is set
again.
This is relatively harmless: the consequence is that the sinval queue
might overflow, causing a cache reset, if nothing else wakes up the
process to read the invalidations. It's also very unlikely to occur in
practice; it requires the process to receive another catchup signal just
after it has caught up. That requires lots of new invalidations, and
even then, some other process is much more likely to be the laggard in
that situation.
But the fix is also pretty simple, see attached patch 0001.
2. Assertion failure if IDLE_STATS_UPDATE_TIMEOUT fires while processing
a catchup signal
I bumped into this while testing the first issue, and had a backend
paused in the middle of processing a catchup signal:
TRAP: failed Assert("s->state == TRANS_DEFAULT || s->state ==
TRANS_COMMIT || s->state == TRANS_ABORT || s->state == TRANS_PREPARE"),
File: "../master-wt/src/backend/access/transam/xact.c", Line: 902, PID:
23381
0 postgres ExceptionalCondition + 216
1 postgres GetCurrentTransactionStopTimestamp + 128
2 postgres pgstat_index_flush_cb + 204
3 postgres pgstat_flush_pending_entries + 248
4 postgres pgstat_report_stat + 436
5 postgres ProcessInterrupts + 3340
6 postgres errfinish + 840
7 postgres ReceiveSharedInvalidMessages + 572
8 postgres AcceptInvalidationMessages + 52
9 postgres AtStart_Cache + 28
10 postgres StartTransaction + 900
11 postgres StartTransactionCommand + 84
12 postgres ProcessCatchupInterrupt + 256
13 postgres ProcessClientReadInterrupt + 112
14 postgres secure_read + 456
15 postgres pq_recvbuf + 260
16 postgres pq_getbyte + 96
17 postgres SocketBackend + 44
18 postgres ReadCommand + 44
19 postgres PostgresMain + 2860
20 postgres BackendInitialize + 0
21 postgres postmaster_child_launch + 464
22 postgres BackendStartup + 304
23 postgres ServerLoop + 372
24 postgres PostmasterMain + 6560
25 postgres main + 924
In this particular case, CHECK_FOR_INTERRUPTS() was called from
errfinish(), which is only called because of a debugging message at
level DEBUG4, but there can be many other CHECK_FOR_INTERRUPTS() calls
in processing invalidations.
In ProcessInterrupts(), the condition for whether it's safe to process
the idle stats timeout looks like this:
/*
* If there are pending stats updates and we currently are truly idle
* (matching the conditions in PostgresMain(), report stats now.
*/
if (IdleStatsUpdateTimeoutPending &&
DoingCommandRead && !IsTransactionOrTransactionBlock())
{
IdleStatsUpdateTimeoutPending = false;
pgstat_report_stat(true);
}
Currently, IsTransactionOrTransactionBlock() returns false when we are
in the middle of StartTransaction(). That doesn't feel right to me, I
don't think a backend should be considered as "truly idle" when it's in
the middle of StartTransaction(). The attached patch 0002 changes
IsTransactionOrTransactionBlock() to return true (meaning not truly
idle) in that state.
Patch 0003 is a test that exercises both of those cases. It's AI
generated and I don't intend to commit it, I'm merely including it so
you can reproduce the failures if you want to. I don't like it because
it's pretty tightly coupled with injection points and the exact code
structure.
Any comments / objections on patches 0001 and 0002?
- Heikki
Attachments:
t259263_10001-Fix-losing-a-sinval-catchup-signal-that-arrives-duri.patchtext/x-patch; charset=UTF-8; name=0001-Fix-losing-a-sinval-catchup-signal-that-arrives-duri.patchDownload+25-8
0002-Don-t-consider-backend-as-fully-idle-when-it-s-start.patchtext/x-patch; charset=UTF-8; name=0002-Don-t-consider-backend-as-fully-idle-when-it-s-start.patchDownload+15-5
0003-Add-a-test-for-losing-a-sinval-catchup-signal.patchtext/x-patch; charset=UTF-8; name=0003-Add-a-test-for-losing-a-sinval-catchup-signal.patchDownload+144-1
Hi Heikki,
Thanks for your patches.
0001 looks good to me.
For 0002
if (s->blockState == TBLOCK_DEFAULT && s->state == TRANS_DEFAULT)
I think that changed the recovery conflict logic because of the following
code in
postgres.c
case RECOVERY_CONFLICT_LOCK:
case RECOVERY_CONFLICT_TABLESPACE:
case RECOVERY_CONFLICT_SNAPSHOT:
/*
* If we aren't in a transaction any longer then ignore.
*/
if (!IsTransactionOrTransactionBlock())
return;report_recovery_conflict(reason);
return;
And it will eventually become a FATAL which changes the current behavior.
Given
the window is quite small, maybe it is worth mentioning in the commit
message/comment?
Thanks,
Shihao
Hi,
On Tue, 17 Nov 2026 at 06:32, shihao zhong <zhong950419@gmail.com> wrote:
Thanks for your patches.
0001 looks good to me.
+1
For 0002
if (s->blockState == TBLOCK_DEFAULT && s->state == TRANS_DEFAULT)
I think that changed the recovery conflict logic because of the following code in
postgres.ccase RECOVERY_CONFLICT_LOCK:
case RECOVERY_CONFLICT_TABLESPACE:
case RECOVERY_CONFLICT_SNAPSHOT:
/*
* If we aren't in a transaction any longer then ignore.
*/
if (!IsTransactionOrTransactionBlock())
return;report_recovery_conflict(reason);
return;And it will eventually become a FATAL which changes the current behavior. Given
the window is quite small, maybe it is worth mentioning in the commit message/comment?
I could repro this.
Pausing an idle backend at sinval-caught-up and triggering a tablespace
conflict kills the session:
FATAL: terminating connection due to conflict with recovery
DETAIL: User was or might have been using tablespace that must be dropped.
Without 0002 it's ignored. Not sure just documenting this is enough,
since the session was idle and never used that tablespace. Maybe we
could leave IsTransactionOrTransactionBlock() as-is and handle this
only for the idle stats check? Though I get the occurence of this should
not be much.
Regards,
Ayush
Hi Ayush,
Thanks for reproducing it. Since the session was idle, I agree that a
note in the commit message is not enough.
Thanks,
Shihao
Maybe we
could leave IsTransactionOrTransactionBlock() as-is and handle this
only for the idle stats check?
Give it a try with your idea, it doesn't look that complex, maybe we should
fix it?
Thanks,
Shihao
Attachments:
nocfbot-0001-Don-t-report-idle-stats-while-starting-a-transaction.patchapplication/octet-stream; name=nocfbot-0001-Don-t-report-idle-stats-while-starting-a-transaction.patchDownload+19-3
Pushed 0001 patch, thanks for the reviews!
On 24/11/2026 04:13, shihao zhong wrote:
Maybe we
could leave IsTransactionOrTransactionBlock() as-is and handle this
only for the idle stats check?Give it a try with your idea, it doesn't look that complex, maybe we
should fix it?
Makes sense. Attached is another version along those lines. It adds a
new function, IsCompletelyOutOfTransaction(), which checks both the
block state and the low-level state. So instead of
"!IsTransactionOrTransactionBlock() && IsTransactionStateDefault()", you
can do just "IsCompletelyOutOfTransaction()". That's easier to read at
the call site, and the new function is a natural place for the comment
on what "completely out of a transaction" means.
AI review found one little issue. In theory, it's a pre-existing issue,
but this patch makes the window for it larger:
ProcessInterrupts() clears InterruptPending when it's called, so once it
has skipped the idle stats report in the middle of the catchup
transaction, nothing processes IdleStatsUpdateTimeoutPending again until
the backend is woken up for some other reason. That defeats the purpose
of the idle stats timeout.
To fix that, ProcessClientReadInterrupt() now re-arms InterruptPending
if IdleStatsUpdateTimeoutPending is still set after processing the
catchup and notify interrupts, and checks for interrupts again. That
feels a little ad hoc to me to be honest, but it seems like the best way
to fix it without a bigger overhaul. And I'm working on a bigger
overhaul, in the "interrupts vs signals" thread, anyway.
What do you think of the attached?
- Heikki
Hi Heikki,
Thanks for pushing 0001.
v2 looks good to me. On a standby, an idle backend paused in the
catchup transaction now survives a tablespace conflict. So Ayush's case
still works.
Thanks,
Shihao