1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-26 01:22:12 +03:00

Fix attach-related race condition in shm_mq_send_bytes.

Spotted by Antonin Houska.
This commit is contained in:
Robert Haas
2015-08-07 09:04:07 -05:00
parent 8c7bb02409
commit e9215461d2

View File

@ -681,16 +681,12 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, void *data, bool nowait,
return SHM_MQ_DETACHED; return SHM_MQ_DETACHED;
} }
if (available == 0) if (available == 0 && !mqh->mqh_counterparty_attached)
{ {
shm_mq_result res;
/* /*
* The queue is full, so if the receiver isn't yet known to be * The queue is full, so if the receiver isn't yet known to be
* attached, we must wait for that to happen. * attached, we must wait for that to happen.
*/ */
if (!mqh->mqh_counterparty_attached)
{
if (nowait) if (nowait)
{ {
if (shm_mq_get_receiver(mq) == NULL) if (shm_mq_get_receiver(mq) == NULL)
@ -707,7 +703,15 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, void *data, bool nowait,
return SHM_MQ_DETACHED; return SHM_MQ_DETACHED;
} }
mqh->mqh_counterparty_attached = true; mqh->mqh_counterparty_attached = true;
/*
* The receiver may have read some data after attaching, so we
* must not wait without rechecking the queue state.
*/
} }
else if (available == 0)
{
shm_mq_result res;
/* Let the receiver know that we need them to read some data. */ /* Let the receiver know that we need them to read some data. */
res = shm_mq_notify_receiver(mq); res = shm_mq_notify_receiver(mq);