pgsql: Don't CHECK_FOR_INTERRUPTS between WaitLatch and ResetLatch.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Don't CHECK_FOR_INTERRUPTS between WaitLatch and ResetLatch.
Date: 2016-08-01 19:14:09
Message-ID: E1bUIfB-0006DM-44@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Don't CHECK_FOR_INTERRUPTS between WaitLatch and ResetLatch.

This coding pattern creates a race condition, because if an interesting
interrupt happens after we've checked InterruptPending but before we reset
our latch, the latch-setting done by the signal handler would get lost,
and then we might block at WaitLatch in the next iteration without ever
noticing the interrupt condition. You can put the CHECK_FOR_INTERRUPTS
before WaitLatch or after ResetLatch, but not between them.

Aside from fixing the bugs, add some explanatory comments to latch.h
to perhaps forestall the next person from making the same mistake.

In HEAD, also replace gather_readnext's direct call of
HandleParallelMessages with CHECK_FOR_INTERRUPTS. It does not seem clean
or useful for this one caller to bypass ProcessInterrupts and go straight
to HandleParallelMessages; not least because that fails to consider the
InterruptPending flag, resulting in useless work both here
(if InterruptPending isn't set) and in the next CHECK_FOR_INTERRUPTS call
(if it is).

This thinko seems to have been introduced in the initial coding of
storage/ipc/shm_mq.c (commit ec9037df2), and then blindly copied into all
the subsequent parallel-query support logic. Back-patch relevant hunks
to 9.4 to extirpate the error everywhere.

Discussion: <1661(dot)1469996911(at)sss(dot)pgh(dot)pa(dot)us>

Branch
------
REL9_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/45e5496042c86e49ed5395573251b7c955de3b62

Modified Files
--------------
src/backend/storage/ipc/shm_mq.c | 18 +++++++++---------
src/include/storage/latch.h | 16 ++++++++++++++++
2 files changed, 25 insertions(+), 9 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2016-08-01 20:12:07 pgsql: Minor cleanup for access/transam/parallel.c.
Previous Message Fujii Masao 2016-08-01 17:44:46 pgsql: Remove unused arguments from pg_replication_origin_xact_reset fu