Re: Condition variable live lock
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Thomas Munro <thomas.munro@enterprisedb.com>,
Andres Freund <andres@anarazel.de>,
Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2018-01-06T21:00:24Z
Lists: pgsql-hackers
Attachments
- tighten-proclist-assertions.patch (text/x-diff) patch
- cv-cosmetic-changes.patch (text/x-diff) patch
I wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
>> No opinion without seeing what you propose to change.
> OK, will put out a proposal.
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. I do not like the
fact that it's impossible to tell whether a proclist_node is in any
proclist or not. Initially, a proclist_node contains zeroes which is
a distinguishable state, but proclist_delete_offset resets it to
next = prev = INVALID_PGPROCNO which looks the same as a node that's in a
singleton list. We should have it reset to the initial state of zeroes
instead, and then we can add assertions to proclist_push_xxx that the
supplied node is not already in a list. Hence, I propose the first
attached patch which tightens things up in proclist.h and then removes
the !proclist_contains test in ConditionVariablePrepareToSleep; the
assertion in proclist_push_tail supersedes that.
The second attached patch is the cosmetic changes I want to make in
condition_variable.c/.h.
I still think that we ought to change the Asserts on cv_sleep_target in
ConditionVariablePrepareToSleep and ConditionVariableSleep to be full
test-and-elog tests. Those are checking a global correctness property
("global" meaning "interactions between completely unrelated modules can
break this"), and they'd be extremely cheap compared to the rest of what
those functions are doing, so I think insisting that they be Asserts is
penny wise and pound foolish. Anybody besides Robert want to vote on
that?
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. This seems unwise to me. I think we should
adjust the WaitLatch call to include WL_POSTMASTER_DEATH as a wake
condition and just do a summary proc_exit(1) if it sees that. I'd even
argue that that is a back-patchable bug fix.
regards, tom lane
Commits
-
Add WL_EXIT_ON_PM_DEATH pseudo-event.
- cfdf4dc4fc96 12.0 landed
-
While waiting for a condition variable, detect postmaster death.
- 80259d4dbf47 11.0 landed
- d56a5f994c21 10.2 landed
-
Fix race condition during replication origin drop.
- 8a906204aec4 11.0 landed
- 1f5adbd799cf 10.2 landed
-
Allow ConditionVariable[PrepareTo]Sleep to auto-switch between CVs.
- 4af2190eb04b 10.2 landed
- 13db3b936359 11.0 landed
-
Cosmetic improvements in condition_variable.[hc].
- e35dba475a44 11.0 landed
-
Improve error detection capability in proclists.
- ea8e1bbc5384 11.0 landed
-
Remove return values of ConditionVariableSignal/Broadcast.
- ccf312a4488a 11.0 landed
-
Reorder steps in ConditionVariablePrepareToSleep for more safety.
- 83fe2708d668 10.2 landed
- 3cac0ec85992 11.0 landed
-
Rewrite ConditionVariableBroadcast() to avoid live-lock.
- aced5a92bf46 11.0 landed
- 1c77e990833a 10.2 landed
-
Add parallel-aware hash joins.
- 1804284042e6 11.0 cited