pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Thomas Munro <thomas.munro@enterprisedb.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 15:45:02 -0500
Message-ID: <16701.1515444302@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+TgmobTOp154RTcM7HNoUhaT3-m1DCsjEx1oVNqvhuz7PGUbA@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>
	<CA+TgmobTOp154RTcM7HNoUhaT3-m1DCsjEx1oVNqvhuz7PGUbA@mail.gmai! l.com>

Robert Haas <robertmhaas@gmail.com> writes:
> I guess my aversion to converting the existing Assert-test into an
> elog test was really a concern that we'd be countenancing the use of
> CVs in any coding pattern more complicated than a very simple
> test-and-wait loop.  Suppose someone were to propose adding runtime
> checks that when we release a spinlock, it is held by the process that
> tried to release it.  Someone might reply that such a check ought to
> be unnecessary because the code protected by a spinlock ought to be a
> short, straight-line critical section and therefore we should be able
> to spot any such coding error by inspection.

Right, *because* we have the rule that spinlock-protected code must be
short straight-line segments.  There is certainly no such rule for
LWLocks, and I'm not sure why you think that CVs have more restricted
use-cases than LWLocks.

There is a potential issue, if you call random code inside the wait loop,
that that code might reset the process latch and thereby lose a signal for
the CV.  As far as I can see at the moment, that'd only be a hazard for
code executed before the loop's ConditionVariableSleep, not after, so even
that can be avoided if you code properly.  In any case, the proposed
generalization of CVs poses no such hazard: yes, we might reset the latch,
but if so the outer CV loop will see that it's lost its prepared sleep
state and will be forced to recheck its exit condition.

> It's often the case that mechanisms like this end up getting
> used profitably in a lot of places not imagined by their original
> creator, and that might be the case here.

Yeah, that's exactly my expectation.  If I thought we were only going to
have five uses of condition variables forevermore, I wouldn't be putting
much time into them.

> I think an extremely *likely* programming error when programming with
> CVs is to have a code path where ConditionVariableCancelSleep() does
> not get called.  The proposed change could make such mistakes much
> less noticeable.

Actually, the proposed change would turn it into barely an error at all.
The only advantage of cancelling a sleep immediately is that you avoid
possibly getting a no-longer-useful latch event later.

			regards, tom lane




view thread (31+ messages)  latest in thread

Message-ID: <16701.1515444302@sss.pgh.pa.us>
Permalink:  ../16701.1515444302@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16701.1515444302@sss.pgh.pa.us

 · 

reply

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, robertmhaas@gmail.com, thomas.munro@enterprisedb.com, andres@anarazel.de
  Subject: Re: Condition variable live lock
  In-Reply-To: <16701.1515444302@sss.pgh.pa.us>
  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> <16701.1515444302@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