Skip to content

Commit e921546

Browse files
committed
Fix attach-related race condition in shm_mq_send_bytes.
Spotted by Antonin Houska.
1 parent 8c7bb02 commit e921546

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/backend/storage/ipc/shm_mq.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -681,33 +681,37 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, void *data, bool nowait,
681681
return SHM_MQ_DETACHED;
682682
}
683683

684-
if (available == 0)
684+
if (available == 0 && !mqh->mqh_counterparty_attached)
685685
{
686-
shm_mq_result res;
687-
688686
/*
689687
* The queue is full, so if the receiver isn't yet known to be
690688
* attached, we must wait for that to happen.
691689
*/
692-
if (!mqh->mqh_counterparty_attached)
690+
if (nowait)
693691
{
694-
if (nowait)
692+
if (shm_mq_get_receiver(mq) == NULL)
695693
{
696-
if (shm_mq_get_receiver(mq) == NULL)
697-
{
698-
*bytes_written = sent;
699-
return SHM_MQ_WOULD_BLOCK;
700-
}
701-
}
702-
else if (!shm_mq_wait_internal(mq, &mq->mq_receiver,
703-
mqh->mqh_handle))
704-
{
705-
mq->mq_detached = true;
706694
*bytes_written = sent;
707-
return SHM_MQ_DETACHED;
695+
return SHM_MQ_WOULD_BLOCK;
708696
}
709-
mqh->mqh_counterparty_attached = true;
710697
}
698+
else if (!shm_mq_wait_internal(mq, &mq->mq_receiver,
699+
mqh->mqh_handle))
700+
{
701+
mq->mq_detached = true;
702+
*bytes_written = sent;
703+
return SHM_MQ_DETACHED;
704+
}
705+
mqh->mqh_counterparty_attached = true;
706+
707+
/*
708+
* The receiver may have read some data after attaching, so we
709+
* must not wait without rechecking the queue state.
710+
*/
711+
}
712+
else if (available == 0)
713+
{
714+
shm_mq_result res;
711715

712716
/* Let the receiver know that we need them to read some data. */
713717
res = shm_mq_notify_receiver(mq);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy