Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit038aa89

Browse files
committed
shm_mq: Third attempt at fixing nowait behavior in shm_mq_receive.
Commita1480ec purported to fix theproblems with commitb2ccb5f, but itdidn't completely fix them. The problem is that the checks wereperformed in the wrong order, leading to a race condition. If thesender attached, sent a message, and detached after the receivercalled shm_mq_get_sender and before the receiver calledshm_mq_counterparty_gone, we'd incorrectly return SHM_MQ_DETACHEDbefore all messages were read. Repair by reversing the order ofoperations, and add a long comment explaining why this new logic is(hopefully) correct.
1 parent11e7f9d commit038aa89

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

‎src/backend/storage/ipc/shm_mq.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,26 @@ shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
405405
{
406406
if (nowait)
407407
{
408+
intcounterparty_gone;
409+
410+
/*
411+
* We shouldn't return at this point at all unless the sender
412+
* hasn't attached yet. However, the correct return value depends
413+
* on whether the sender is still attached. If we first test
414+
* whether the sender has ever attached and then test whether the
415+
* sender has detached, there's a race condition: a sender that
416+
* attaches and detaches very quickly might fool us into thinking
417+
* the sender never attached at all. So, test whether our
418+
* counterparty is definitively gone first, and only afterwards
419+
* check whether the sender ever attached in the first place.
420+
*/
421+
counterparty_gone=shm_mq_counterparty_gone(mq,mqh->mqh_handle);
408422
if (shm_mq_get_sender(mq)==NULL)
409423
{
410-
if (shm_mq_counterparty_gone(mq,mqh->mqh_handle))
424+
if (counterparty_gone)
411425
returnSHM_MQ_DETACHED;
412-
returnSHM_MQ_WOULD_BLOCK;
426+
else
427+
returnSHM_MQ_WOULD_BLOCK;
413428
}
414429
}
415430
elseif (!shm_mq_wait_internal(mq,&mq->mq_sender,mqh->mqh_handle)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp