From: Tom Lane <tgl@sss.pgh.pa.us>
To: Thomas Munro <thomas.munro@enterprisedb.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Pg Hackers <pgsql-hackers@postgresql.org>
Subject: Re: Condition variable live lock
Date: Mon, 08 Jan 2018 18:50:33 -0500
Message-ID: <19947.1515455433@sss.pgh.pa.us> (raw)
In-Reply-To: <CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com>
References: <CAEepm=0NWKehYw7NDoUSf8juuKOPRnCyY3vuaSvhrEWsOTAa3w@mail.gmail.com>
<CAEepm=1_S2Ly3Q53yViq29RVJmvaUw8hXs5_ekg_E1uHrNtXGQ@mail.gmail.com>
<20171229193843.tsbob2xsjfmxkq2g@alap3.anarazel.de>
<CA+TgmoZykfhWOQWqFcCHL2a-=QVigssd0og1x2AU6jtJsLN-iw@mail.gmail.com>
<20180104205447.mdaub47aunp3h3mq@alap3.anarazel.de>
<17054.1515126429@sss.pgh.pa.us>
<CAEepm=10Es09VfW656CXMnN0LENrAD4Z1Pn8Uxb9rpmDp+0i3Q@mail.gmail.com>
<21761.1515132657@sss.pgh.pa.us>
<CAEepm=0zg2dSqHdMbf6ATB9Q4MPCBQfuH23z_00bWEV3wbX-rw@mail.gmail.com>
<23234.1515134575@sss.pgh.pa.us>
<CAEepm=2xt_M0PquwBKC6SPm0gpmCUajGsiQ7PLTBAbSuo4O34g@mail.gmail.com>
<15204.1515166090@sss.pgh.pa.us>
<14191.1515173593@sss.pgh.pa.us>
<9342.1515181117@sss.pgh.pa.us>
<CA+TgmoZS8wr0R4BYD7CfM4CKEbyhi4XmkcORqsLL1CmJMyxOXg@mail.gmail.com>
<10713.1515182599@sss.pgh.pa.us>
<26266.1515272424@sss.pgh.pa.us>
<28942.1515275038@sss.pgh.pa.us>
<2437.1515368316@sss.pgh.pa.us>
<CAEepm=3MAW-LVDPmF+6nAUewC3_8mD3gcUPi-VrdBWnCQh7Qrw@mail.gmai! l.com>
<31228.1515378851@sss.pgh.pa.us>
<CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com>
Thomas Munro <thomas.munro@enterprisedb.com> writes:
> On Sun, Jan 7, 2018 at 10:00 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> I began with the intention of making no non-cosmetic changes, but then
>> I started to wonder why ConditionVariablePrepareToSleep bothers with a
>> !proclist_contains test, when the calling process surely ought not be
>> in the list -- or any other list -- since it wasn't prepared to sleep.
>> And that led me down a different rabbit hole ending in the conclusion
>> that proclist.h could stand some improvements too.
> +1
>> The second attached patch is the cosmetic changes I want to make in
>> condition_variable.c/.h.
> +1
I pushed these, with an addition to ConditionVariablePrepareToSleep's
comment emphasizing that you're supposed to call it before the loop test;
this because some desultory digging found that one of the existing callers
is violating that rule. replorigin_drop() in replication/logical/origin.c
has
cv = &state->origin_cv;
LWLockRelease(ReplicationOriginLock);
ConditionVariablePrepareToSleep(cv);
ConditionVariableSleep(cv, WAIT_EVENT_REPLICATION_ORIGIN_DROP);
ConditionVariableCancelSleep();
goto restart;
and unless I'm much mistaken, that is flat broken. Somebody could
change the ReplicationState's acquired_by before we reach
ConditionVariablePrepareToSleep, in which case we won't get any signal
for that and will just sleep here indefinitely.
It would still be broken if we removed the PrepareToSleep call, but
at least it'd be a busy-wait not a hang.
Not sure about a convenient fix. One idea is to move the
PrepareToSleep call inside the hold on ReplicationOriginLock, which
would fix things only if the various signallers of origin_cv did the
signalling while holding that lock, which they do not. It's not clear
to me whether making them do so inside that lock would be problematic
for performance.
We can't just move the prepare/cancel sleep calls to around this whole
loop, because we do not know outside the loop which CV is to be waited
on. So another idea is to get rid of that and have just one CV for all
ReplicationStates.
Or (and I'm sure Robert sees this coming), if we applied my proposed
patch to let ConditionVariableSleep auto-switch to a different CV,
then we could fix this code by simply removing the PrepareToSleep and
moving the ConditionVariableCancelSleep call to below the loop.
This would work even in the perhaps-unlikely case where the slot as
identified by roident changed positions, though you'd get an additional
trip through the loop (a/k/a test of the exit condition) if that
happened.
>> Another loose end that I'm seeing here is that while a process waiting on
>> a condition variable will respond to a cancel or die interrupt, it will
>> not notice postmaster death.
> Yeah.
I'll respond to that separately to keep it from getting confused with
this replorigin_drop() bug.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: tgl@sss.pgh.pa.us, thomas.munro@enterprisedb.com, robertmhaas@gmail.com, andres@anarazel.de
Subject: Re: Condition variable live lock
In-Reply-To: <19947.1515455433@sss.pgh.pa.us>
References: <CAEepm=0NWKehYw7NDoUSf8juuKOPRnCyY3vuaSvhrEWsOTAa3w@mail.gmail.com> <20171229193843.tsbob2xsjfmxkq2g@alap3.anarazel.de> <CA+TgmoZykfhWOQWqFcCHL2a-=QVigssd0og1x2AU6jtJsLN-iw@mail.gmail.com> <20180104205447.mdaub47aunp3h3mq@alap3.anarazel.de> <17054.1515126429@sss.pgh.pa.us> <CAEepm=10Es09VfW656CXMnN0LENrAD4Z1Pn8Uxb9rpmDp+0i3Q@mail.gmail.com> <21761.1515132657@sss.pgh.pa.us> <CAEepm=0zg2dSqHdMbf6ATB9Q4MPCBQfuH23z_00bWEV3wbX-rw@mail.gmail.com> <23234.1515134575@sss.pgh.pa.us> <CAEepm=2xt_M0PquwBKC6SPm0gpmCUajGsiQ7PLTBAbSuo4O34g@mail.gmail.com> <15204.1515166090@sss.pgh.pa.us> <14191.1515173593@sss.pgh.pa.us> <9342.1515181117@sss.pgh.pa.us> <CA+TgmoZS8wr0R4BYD7CfM4CKEbyhi4XmkcORqsLL1CmJMyxOXg@mail.gmail.com> <10713.1515182599@sss.pgh.pa.us> <26266.1515272424@sss.pgh.pa.us> <28942.1515275038@sss.pgh.pa.us> <2437.1515368316@sss.pgh.pa.us> <31228.1515378851@sss.pgh.pa.us> <CAEepm=2LqHzizbe7muD7-2yHUbTOoF7Q+qkSD5Q41kuhttRTwA@mail.gmail.com> <19947.1515455433@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox