Re: Fix two race conditions in processing interrupts

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: shihao zhong <zhong950419(at)gmail(dot)com>, Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix two race conditions in processing interrupts
Date: 2026-11-26 14:16:55
Message-ID: 603a9be4-70f4-44c8-ba42-bae08062c5cb@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v2-0001-Don-t-report-idle-stats-while-starting-a-transact.patch text/x-patch 6.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhijie Hou 2026-11-26 14:39:40 Fix race between copying and dropping a replication slot
Previous Message Jelte Fennema-Nio 2026-11-26 14:10:59 Re: Reapply graceful socket shutdown on Windows (revert 29992a6a509)