Re: Condition variable live lock
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Thomas Munro <thomas.munro@enterprisedb.com>
Cc: Robert Haas <robertmhaas@gmail.com>, Andres Freund <andres@anarazel.de>,
Pg Hackers <pgsql-hackers@postgresql.org>
Date: 2018-01-09T00:02:23Z
Lists: pgsql-hackers
Thomas Munro <thomas.munro@enterprisedb.com> writes: > On Sun, Jan 7, 2018 at 10:00 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote: >> 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. > Yeah. As far as I know so far, every place where we wait on a > WaitEventSet falls into one of 4 categories when it comes to > postmaster death: > 1. We proc_exit(1) if WL_POSTMASTER_DEATH is returned. > 2. We ereport(FATAL) if WL_POSTMASTER_DEATH is returned. > 3. We asked for WL_POSTMASTER_DEATH in the WaitEventSet, but we don't > actually bother checking for it in the returned value. Instead we > call PostmasterIsAlive() every time through the loop (pgarch.c, > syncrep.c, walsender.c and walreceiver.c). > 4. We didn't ask for WL_POSTMASTER_DEATH. > Do I have that right? I guess category 3 is suboptimal especially on > some clunky but loveable kernels[1] and all cases of 4 including this > one are probably bugs. That makes me wonder why we don't change the > WaitEventSet API so that it calls proc_exit(1) for you by default if > you didn't ask to receive WL_POSTMASTER_DEATH explicitly, to handle > category 1 for free. If you asked for WL_POSTMASTER_DEATH explicitly > then we could return it, to support category 2 callers that want to do > something different. Just a thought. Yeah, that's worth thinking about, because I'm pretty sure that we have had this type of bug before. We have to be a bit careful because Andres is thinking of using the WaitEventState support in the postmaster loop, where it definitely shouldn't check WL_POSTMASTER_DEATH, and it is probably also possible to reach some of those calls in standalone backends, which shouldn't either. So I'm imagining: 1. The WaitEventSet code always includes WL_POSTMASTER_DEATH checking if IsUnderPostmaster --- and conversely, if !IsUnderPostmaster, it should ignore any caller request for that. 2. If caller specifies WL_POSTMASTER_DEATH then we'll return that flag bit, otherwise just exit(1) --- or should the default be ereport(FATAL)? 3. Remove explicit specification of WL_POSTMASTER_DEATH anywhere that the default handling is OK, which is probably almost everywhere. Get rid of those now-redundant PostmasterIsAlive checks, too. I'm not real sure BTW why we have some callers that ereport and some that just exit(1). Seems like it would be better to be consistent, though I'm not entirely sure which behavior to standardize on. (Of course, this would only be an appropriate thing to do in HEAD. In v10 I think we should do the narrow fix I suggested up top.) 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