Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eXJbB-00046h-LP for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jan 2018 04:27:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eXJbB-0004SN-8Y for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jan 2018 04:27:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eXJbA-0004SD-VB for pgsql-hackers@lists.postgresql.org; Fri, 05 Jan 2018 04:27:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eXJb8-0003Ma-C4 for pgsql-hackers@postgresql.org; Fri, 05 Jan 2018 04:27:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w054R986017055; Thu, 4 Jan 2018 23:27:09 -0500 From: Tom Lane To: Andres Freund cc: Robert Haas , Thomas Munro , Pg Hackers Subject: Re: Condition variable live lock In-reply-to: <20180104205447.mdaub47aunp3h3mq@alap3.anarazel.de> References: <20171229193843.tsbob2xsjfmxkq2g@alap3.anarazel.de> <20180104205447.mdaub47aunp3h3mq@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Thu, 04 Jan 2018 12:54:47 -0800" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <26399.1515125769.0@sss.pgh.pa.us> Date: Thu, 04 Jan 2018 23:27:09 -0500 Message-ID: <17054.1515126429@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <26399.1515125769.1@sss.pgh.pa.us> Andres Freund writes: > On 2018-01-04 12:39:47 -0500, Robert Haas wrote: >>> Given that the proclist_contains() checks in condition_variable.c are >>> already racy, I think it might be feasible to collect all procnos to >>> signal while holding the spinlock, and then signal all of them in one >>> go. >> That doesn't seem very nice at all. Not only does it violate the >> coding rule against looping while holding a spinlock, but it seems >> that it would require allocating memory while holding one, which is a >> non-starter. > We could just use a sufficiently sized buffer beforehand. There's an > obvious upper boundary, so that shouldn't be a big issue. I share Robert's discomfort with that solution, but it seems to me there might be a better way. The attached patch uses our own cvWaitLink as a sentinel to detect when we've woken everybody who was on the wait list before we arrived. That gives exactly the desired semantics, not just an approximation to them. Now, the limitation with this is that we can't be waiting for any *other* condition variable, because then we'd be trashing our state about that variable. As coded, we can't be waiting for the target CV either, but that case could actually be handled if we needed to, as per the comment. I do not know if this is likely to be a problematic limitation ... discuss. (The patch does survive check-world, FWIW.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="remove-live-lock-in-CV-broadcast.patch"; charset="us-ascii" Content-ID: <26399.1515125769.2@sss.pgh.pa.us> Content-Description: remove-live-lock-in-CV-broadcast.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/lmgr/condition_variable.c b/src/backend/s= torage/lmgr/condition_variable.c index 41378d6..0d08eb5 100644 *** a/src/backend/storage/lmgr/condition_variable.c --- b/src/backend/storage/lmgr/condition_variable.c *************** int *** 214,228 **** ConditionVariableBroadcast(ConditionVariable *cv) { int nwoken =3D 0; = /* ! * Let's just do this the dumbest way possible. We could try to dequeu= e ! * all the sleepers at once to save spinlock cycles, but it's a bit har= d ! * to get that right in the face of possible sleep cancelations, and we ! * don't want to loop holding the mutex. */ ! while (ConditionVariableSignal(cv)) ++nwoken; = return nwoken; } --- 214,273 ---- ConditionVariableBroadcast(ConditionVariable *cv) { int nwoken =3D 0; + int pgprocno =3D MyProc->pgprocno; = /* ! * In some use-cases, it is common for awakened processes to immediatel= y ! * re-queue themselves. If we just naively try to reduce the wakeup li= st ! * to empty, we'll get into a potentially-indefinite loop against such = a ! * process. The semantics we really want are just to be sure that we h= ave ! * wakened all processes that were in the list at entry. We can use ou= r ! * own cvWaitLink as a sentinel to detect when we've finished. ! * ! * A seeming flaw in this approach is that someone else might signal th= e ! * CV and in doing so remove our sentinel entry. But that's fine: sinc= e ! * CV waiters are always added and removed in order, that must mean tha= t ! * every previous waiter has been wakened, so we're done. We'd get an ! * extra "set" on our latch, which is slightly inefficient but harmless= . ! * ! * This coding assumes (and asserts) that we never broadcast on a CV th= at ! * we are also waiting for. If that turns out to be necessary, we coul= d ! * just remove our entry and insert it at the end, and it still works a= s a ! * sentinel. */ ! = ! /* We should not be attempting a CV wait. */ ! Assert(cv_sleep_target =3D=3D NULL); ! = ! /* Establish the sentinel entry. */ ! SpinLockAcquire(&cv->mutex); ! Assert(!proclist_contains(&cv->wakeup, pgprocno, cvWaitLink)); ! proclist_push_tail(&cv->wakeup, pgprocno, cvWaitLink); ! SpinLockRelease(&cv->mutex); ! = ! for (;;) ! { ! bool aminlist; ! PGPROC *proc =3D NULL; ! = ! /* ! * Check if the sentinel is still there, and if so, remove the first ! * queue entry (which might be the sentinel, or not). ! */ ! SpinLockAcquire(&cv->mutex); ! aminlist =3D proclist_contains(&cv->wakeup, pgprocno, cvWaitLink); ! if (aminlist) ! proc =3D proclist_pop_head_node(&cv->wakeup, cvWaitLink); ! SpinLockRelease(&cv->mutex); ! = ! /* If we or anyone else removed the sentinel, we're done. */ ! if (!aminlist || proc =3D=3D MyProc) ! break; ! = ! /* We found someone sleeping, so set their latch to wake them up. */ ! SetLatch(&proc->procLatch); ++nwoken; + } = return nwoken; } ------- =_aaaaaaaaaa0--