1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-06 07:49:08 +03:00

shm_mq: After a send fails with SHM_MQ_DETACHED, later ones should too.

Prior to this patch, it was occasionally possible, after shm_mq_sendv
had previously returned SHM_MQ_DETACHED, for a later shm_mq_sendv
operation to fail an assertion instead of just again returning
SHM_MQ_ATTACHED.  From the shm_mq code's point of view, it was
expecting to be called again with the same arguments, since the
previous operation had only partially completed.  However, a caller
who isn't using non-blocking mode won't be prepared to repeat the call
with the same arguments, and this code shouldn't expect that they
will.  Repair in such a way that we'll be OK whether the next call
uses the same arguments or not.

Found by Andreas Seltenreich.  Analysis and sketch of fix by Amit
Kapila.  Patch by me, reviewed by Amit Kapila.
This commit is contained in:
Robert Haas
2016-06-06 14:35:30 -04:00
parent e191a69005
commit 44339b892a

View File

@@ -366,9 +366,15 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
res = shm_mq_send_bytes(mqh, sizeof(Size) - mqh->mqh_partial_bytes, res = shm_mq_send_bytes(mqh, sizeof(Size) - mqh->mqh_partial_bytes,
((char *) &nbytes) +mqh->mqh_partial_bytes, ((char *) &nbytes) +mqh->mqh_partial_bytes,
nowait, &bytes_written); nowait, &bytes_written);
mqh->mqh_partial_bytes += bytes_written;
if (res != SHM_MQ_SUCCESS) if (res == SHM_MQ_DETACHED)
{
/* Reset state in case caller tries to send another message. */
mqh->mqh_partial_bytes = 0;
mqh->mqh_length_word_complete = false;
return res; return res;
}
mqh->mqh_partial_bytes += bytes_written;
if (mqh->mqh_partial_bytes >= sizeof(Size)) if (mqh->mqh_partial_bytes >= sizeof(Size))
{ {
@@ -378,6 +384,9 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
mqh->mqh_length_word_complete = true; mqh->mqh_length_word_complete = true;
} }
if (res != SHM_MQ_SUCCESS)
return res;
/* Length word can't be split unless bigger than required alignment. */ /* Length word can't be split unless bigger than required alignment. */
Assert(mqh->mqh_length_word_complete || sizeof(Size) > MAXIMUM_ALIGNOF); Assert(mqh->mqh_length_word_complete || sizeof(Size) > MAXIMUM_ALIGNOF);
} }
@@ -432,7 +441,17 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
break; break;
} }
} }
res = shm_mq_send_bytes(mqh, j, tmpbuf, nowait, &bytes_written); res = shm_mq_send_bytes(mqh, j, tmpbuf, nowait, &bytes_written);
if (res == SHM_MQ_DETACHED)
{
/* Reset state in case caller tries to send another message. */
mqh->mqh_partial_bytes = 0;
mqh->mqh_length_word_complete = false;
return res;
}
mqh->mqh_partial_bytes += bytes_written; mqh->mqh_partial_bytes += bytes_written;
if (res != SHM_MQ_SUCCESS) if (res != SHM_MQ_SUCCESS)
return res; return res;
@@ -449,6 +468,15 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait)
chunksize = MAXALIGN_DOWN(chunksize); chunksize = MAXALIGN_DOWN(chunksize);
res = shm_mq_send_bytes(mqh, chunksize, &iov[which_iov].data[offset], res = shm_mq_send_bytes(mqh, chunksize, &iov[which_iov].data[offset],
nowait, &bytes_written); nowait, &bytes_written);
if (res == SHM_MQ_DETACHED)
{
/* Reset state in case caller tries to send another message. */
mqh->mqh_length_word_complete = false;
mqh->mqh_partial_bytes = 0;
return res;
}
mqh->mqh_partial_bytes += bytes_written; mqh->mqh_partial_bytes += bytes_written;
offset += bytes_written; offset += bytes_written;
if (res != SHM_MQ_SUCCESS) if (res != SHM_MQ_SUCCESS)