Fix two race conditions in processing interrupts

Поиск
Список
Период
Сортировка
Искать

Fix two race conditions in processing interrupts

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
Вложения:
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

Re: Fix two race conditions in processing interrupts

От:
shihao zhong <zhong950419@gmail.com>
Дата:
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


Re: Fix two race conditions in processing interrupts

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Hi,

On Tue, 17 Nov 2026 at 06:32, shihao zhong  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.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?

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


FAQ