Latch reset ordering bug in condition_variable.c

From: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Latch reset ordering bug in condition_variable.c
Date: 2017-02-09 11:01:25
Message-ID: CAEepm=3a4VaPFnmwcdyUH8gE5_hW4tRvXQkpfQyrzgDQ9gJCYw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

ConditionVariablePrepareToSleep() has a race that can leave you
hanging, introduced by me in the v4 patch. The problem is that that
function resets our latch after adding our process to the wakeup list.
With the right timing, the following sequence can happen:

1. ConditionVariablePrepareToSleep() adds us to the wakeup list.
2. Some other process calls ConditionVariableSignal(). It removes us
from the wakeup list and sets our latch.
3. ConditionVariablePrepareToSleep() resets our latch.
4. We enter (or continue) our predicate loop. Our exit condition
happens not to be true yet, so we call ConditionVariableSleep().
5. ConditionVariableSleep() never returns because WaitEventSet()
blocks. Our latch is not set, yet we are no longer in the wakeup list
so ConditionalVariableSignal() will never set it.

We should reset the latch first. Then there is no way to reach
ConditionVariableSleep() with neither a set latch nor an entry in the
wakeup queue.

See attached. Thoughts?

--
Thomas Munro
http://www.enterprisedb.com

Attachment Content-Type Size
fix-condition-variable-race.patch application/octet-stream 919 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2017-02-09 11:17:00 Re: pg_basebackup -R
Previous Message Ashutosh Bapat 2017-02-09 10:50:26 Re: Partition-wise join for join between (declaratively) partitioned tables