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

Commit4efe26c

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 parent0279f62 commit4efe26c

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
@@ -501,11 +501,26 @@ shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
501501
{
502502
if (nowait)
503503
{
504+
intcounterparty_gone;
505+
506+
/*
507+
* We shouldn't return at this point at all unless the sender
508+
* hasn't attached yet. However, the correct return value depends
509+
* on whether the sender is still attached. If we first test
510+
* whether the sender has ever attached and then test whether the
511+
* sender has detached, there's a race condition: a sender that
512+
* attaches and detaches very quickly might fool us into thinking
513+
* the sender never attached at all. So, test whether our
514+
* counterparty is definitively gone first, and only afterwards
515+
* check whether the sender ever attached in the first place.
516+
*/
517+
counterparty_gone=shm_mq_counterparty_gone(mq,mqh->mqh_handle);
504518
if (shm_mq_get_sender(mq)==NULL)
505519
{
506-
if (shm_mq_counterparty_gone(mq,mqh->mqh_handle))
520+
if (counterparty_gone)
507521
returnSHM_MQ_DETACHED;
508-
returnSHM_MQ_WOULD_BLOCK;
522+
else
523+
returnSHM_MQ_WOULD_BLOCK;
509524
}
510525
}
511526
elseif (!shm_mq_wait_internal(mq,&mq->mq_sender,mqh->mqh_handle)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp