pgsql: Reorder steps in ConditionVariablePrepareToSleep for more safety

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Reorder steps in ConditionVariablePrepareToSleep for more safety
Date: 2018-01-06 00:42:57
Message-ID: E1eXcZd-0003ux-1T@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Reorder steps in ConditionVariablePrepareToSleep for more safety.

In the admittedly-very-unlikely case that AddWaitEventToSet fails,
ConditionVariablePrepareToSleep would error out after already having
set cv_sleep_target, which is probably bad, and after having already
set cv_wait_event_set, which is very bad. Transaction abort might or
might not clean up cv_sleep_target properly; but there is nothing
that would be aware that the WaitEventSet wasn't fully constructed,
so that all future condition variable sleeps would be broken.
We can easily guard against these hazards with slight restructuring.

Back-patch to v10 where condition_variable.c was introduced.

Discussion: https://postgr.es/m/CAEepm=0NWKehYw7NDoUSf8juuKOPRnCyY3vuaSvhrEWsOTAa3w@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3cac0ec85992829c160bdd8a370dd4676d42f58c

Modified Files
--------------
src/backend/storage/lmgr/condition_variable.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2018-01-06 01:33:31 pgsql: Remove return values of ConditionVariableSignal/Broadcast.
Previous Message Tom Lane 2018-01-06 00:21:40 pgsql: Rewrite ConditionVariableBroadcast() to avoid live-lock.