Skip to content

Commit 8ef3d9f

Browse files
committed
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 ec9037d), 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>
1 parent dc6b20c commit 8ef3d9f

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

src/backend/libpq/pqmq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ mq_putmessage(char msgtype, const char *s, size_t len)
172172
break;
173173

174174
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
175-
CHECK_FOR_INTERRUPTS();
176175
ResetLatch(&MyProc->procLatch);
176+
CHECK_FOR_INTERRUPTS();
177177
}
178178

179179
pq_mq_busy = false;

src/backend/storage/ipc/shm_mq.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,11 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, const void *data,
868868
*/
869869
WaitLatch(MyLatch, WL_LATCH_SET, 0);
870870

871-
/* An interrupt may have occurred while we were waiting. */
872-
CHECK_FOR_INTERRUPTS();
873-
874871
/* Reset the latch so we don't spin. */
875872
ResetLatch(MyLatch);
873+
874+
/* An interrupt may have occurred while we were waiting. */
875+
CHECK_FOR_INTERRUPTS();
876876
}
877877
else
878878
{
@@ -965,11 +965,11 @@ shm_mq_receive_bytes(shm_mq *mq, Size bytes_needed, bool nowait,
965965
*/
966966
WaitLatch(MyLatch, WL_LATCH_SET, 0);
967967

968-
/* An interrupt may have occurred while we were waiting. */
969-
CHECK_FOR_INTERRUPTS();
970-
971968
/* Reset the latch so we don't spin. */
972969
ResetLatch(MyLatch);
970+
971+
/* An interrupt may have occurred while we were waiting. */
972+
CHECK_FOR_INTERRUPTS();
973973
}
974974
}
975975

@@ -1071,11 +1071,11 @@ shm_mq_wait_internal(volatile shm_mq *mq, PGPROC *volatile * ptr,
10711071
/* Wait to be signalled. */
10721072
WaitLatch(MyLatch, WL_LATCH_SET, 0);
10731073

1074-
/* An interrupt may have occurred while we were waiting. */
1075-
CHECK_FOR_INTERRUPTS();
1076-
10771074
/* Reset the latch so we don't spin. */
10781075
ResetLatch(MyLatch);
1076+
1077+
/* An interrupt may have occurred while we were waiting. */
1078+
CHECK_FOR_INTERRUPTS();
10791079
}
10801080
}
10811081
PG_CATCH();

src/include/storage/latch.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@
5252
* do. Otherwise, if someone sets the latch between the check and the
5353
* ResetLatch call, you will miss it and Wait will incorrectly block.
5454
*
55+
* Another valid coding pattern looks like:
56+
*
57+
* for (;;)
58+
* {
59+
* if (work to do)
60+
* Do Stuff(); // in particular, exit loop if some condition satisfied
61+
* WaitLatch();
62+
* ResetLatch();
63+
* }
64+
*
65+
* This is useful to reduce latch traffic if it's expected that the loop's
66+
* termination condition will often be satisfied in the first iteration;
67+
* the cost is an extra loop iteration before blocking when it is not.
68+
* What must be avoided is placing any checks for asynchronous events after
69+
* WaitLatch and before ResetLatch, as that creates a race condition.
70+
*
5571
* To wake up the waiter, you must first set a global flag or something
5672
* else that the wait loop tests in the "if (work to do)" part, and call
5773
* SetLatch *after* that. SetLatch is designed to return quickly if the

src/test/modules/test_shm_mq/setup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ wait_for_workers_to_become_ready(worker_state *wstate,
287287
/* Wait to be signalled. */
288288
WaitLatch(MyLatch, WL_LATCH_SET, 0);
289289

290-
/* An interrupt may have occurred while we were waiting. */
291-
CHECK_FOR_INTERRUPTS();
292-
293290
/* Reset the latch so we don't spin. */
294291
ResetLatch(MyLatch);
292+
293+
/* An interrupt may have occurred while we were waiting. */
294+
CHECK_FOR_INTERRUPTS();
295295
}
296296
}
297297
PG_CATCH();

src/test/modules/test_shm_mq/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ test_shm_mq_pipelined(PG_FUNCTION_ARGS)
231231
* for us to do.
232232
*/
233233
WaitLatch(MyLatch, WL_LATCH_SET, 0);
234-
CHECK_FOR_INTERRUPTS();
235234
ResetLatch(MyLatch);
235+
CHECK_FOR_INTERRUPTS();
236236
}
237237
}
238238

0 commit comments

Comments
 (0)