Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Don't CHECK_FOR_INTERRUPTS between WaitLatch and ResetLatch.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 1 Aug 2016 19:13:53 +0000 (15:13 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 1 Aug 2016 19:13:53 +0000 (15:13 -0400)
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.1469996911@sss.pgh.pa.us>

src/backend/storage/ipc/shm_mq.c
src/include/storage/latch.h

index 540f604c3110eba387827db0a445c1dbb7f828ee..9e726eb628ecbcba88039eaa238e921726470ce5 100644 (file)
@@ -763,11 +763,11 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, void *data, bool nowait,
             */
            WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
 
-           /* An interrupt may have occurred while we were waiting. */
-           CHECK_FOR_INTERRUPTS();
-
            /* Reset the latch so we don't spin. */
            ResetLatch(&MyProc->procLatch);
+
+           /* An interrupt may have occurred while we were waiting. */
+           CHECK_FOR_INTERRUPTS();
        }
        else
        {
@@ -860,11 +860,11 @@ shm_mq_receive_bytes(shm_mq *mq, Size bytes_needed, bool nowait,
         */
        WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
 
-       /* An interrupt may have occurred while we were waiting. */
-       CHECK_FOR_INTERRUPTS();
-
        /* Reset the latch so we don't spin. */
        ResetLatch(&MyProc->procLatch);
+
+       /* An interrupt may have occurred while we were waiting. */
+       CHECK_FOR_INTERRUPTS();
    }
 }
 
@@ -966,11 +966,11 @@ shm_mq_wait_internal(volatile shm_mq *mq, PGPROC *volatile * ptr,
            /* Wait to be signalled. */
            WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
 
-           /* An interrupt may have occurred while we were waiting. */
-           CHECK_FOR_INTERRUPTS();
-
            /* Reset the latch so we don't spin. */
            ResetLatch(&MyProc->procLatch);
+
+           /* An interrupt may have occurred while we were waiting. */
+           CHECK_FOR_INTERRUPTS();
        }
    }
    PG_CATCH();
index ab722e756626ebb0d25bfc66a67467f94729d429..fbd9e574566e8d7e837befae8207ca8c82e0bd05 100644 (file)
  * do. Otherwise, if someone sets the latch between the check and the
  * ResetLatch call, you will miss it and Wait will incorrectly block.
  *
+ * Another valid coding pattern looks like:
+ *
+ * for (;;)
+ * {
+ *    if (work to do)
+ *        Do Stuff(); // in particular, exit loop if some condition satisfied
+ *    WaitLatch();
+ *    ResetLatch();
+ * }
+ *
+ * This is useful to reduce latch traffic if it's expected that the loop's
+ * termination condition will often be satisfied in the first iteration;
+ * the cost is an extra loop iteration before blocking when it is not.
+ * What must be avoided is placing any checks for asynchronous events after
+ * WaitLatch and before ResetLatch, as that creates a race condition.
+ *
  * To wake up the waiter, you must first set a global flag or something
  * else that the wait loop tests in the "if (work to do)" part, and call
  * SetLatch *after* that. SetLatch is designed to return quickly if the