From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Harris <harmic@gmail.com>
Cc: Simon Riggs <simon.riggs@enterprisedb.com>
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Undetected Deadlock
Date: Sun, 06 Feb 2022 17:57:10 -0500
Message-ID: <794618.1644188230@sss.pgh.pa.us> (raw)
In-Reply-To: <CADofcAVWMdOfyND0Mf6agsr9mOtjy+Fe=yNGMRdnSP1NYZx6qg@mail.gmail.com>
References: <CADofcAWbMrvgwSMqO4iG_iD3E2v8ZUrC-_crB41my=VMM02-CA@mail.gmail.com>
<202201252311.4jc4gpdzwwu7@alvherre.pgsql>
<CADofcAXSn_oS3MOxS=1epiXirwETOB_BpBmM8w4=F3GaQe+cwA@mail.gmail.com>
<CADofcAV29umxzTFPuJu+Cvvd5cZKkQxK0TZmpVJenWjok-=gYw@mail.gmail.com>
<CADofcAVJdqqsm45R2LTihNaVhZ4sdSJkBXEwEV7tJ8BQ+s4gRA@mail.gmail.com>
<CANbhV-FoXqh3YWuJLoH-36Mb+gwXG4XQdSy=9x-Zv72BFGhVww@mail.gmail.com>
<272367.1643906187@sss.pgh.pa.us>
<CADofcAVWMdOfyND0Mf6agsr9mOtjy+Fe=yNGMRdnSP1NYZx6qg@mail.gmail.com>
Michael Harris <harmic@gmail.com> writes:
>> If Michael's analysis were accurate, I'd agree that there is a robustness
>> issue, but I don't think there is. See timeout.c:220:
> Actually that only sets a new timer after the nearest timeout has expired.
Mmm, yeah, you're right: as long as we keep on canceling timeout
requests before they are reached, this code won't notice a problem.
We need to check against signal_due_at, not only the next timeout
request, more or less as attached. Do you want to try this and see
if it actually adds any robustness with your buggy code?
> I was thinking that to improve robustness, we could add a check for
> `now < signal_due_at` to schedule_alarm line 300:
> if (signal_pending && now < signal_due_at && nearest_timeout >= signal_due_at)
> return;
I don't think that merging this issue into that test is appropriate;
the logic is already complicated and hard to explain. Also, it seems
to me that some slop is needed, since as mentioned nearby, the kernel's
opinion of when to fire the interrupt is likely to be a bit later
than ours. I'm not wedded to the 10ms slop proposed below, but it
seems like it's probably in the right ballpark.
> One other thing that struck me when reading this code: the variable
> signal_due_at is not declared as volatile sig_atomic_t, even though it
> is read from / written to by the signal handler. Maybe that could
> cause problems?
Hm. I'm not really convinced there's any issue, since signal_due_at
is only examined when signal_pending is true. Still, it's probably
better for all these variables to be volatile, so done.
> One other question: I've fixed our custom function, so that it
> correctly restores any interval timers that were running, but of
> course if our function takes longer than the remaining time on the
> postgres timer the signal will be delivered late. Beyond the fact that
> a deadlock or statement timeout will take longer than expected, are
> there any other negative consequences? Are any of the timeouts
> time-critical such that being delayed by a few seconds would cause a
> problem?
They're certainly not supposed to be that time-critical; any code that
is expecting such a thing is going to have lots of problems already.
PG is not a hard-real-time system ;-)
regards, tom lane
Attachments:recover-from-lost-interrupt-1.patch (text/x-diff, 1.8K) downloaddiff --git a/src/backend/utils/misc/timeout.c b/src/backend/utils/misc/timeout.cindex ac57073033..f38c85df74 100644--- a/src/backend/utils/misc/timeout.c+++ b/src/backend/utils/misc/timeout.c@@ -71,11 +71,12 @@ static volatile sig_atomic_t alarm_enabled = false;
/*
* State recording if and when we next expect the interrupt to fire.
+ * (signal_due_at is valid only when signal_pending is true.)
* Note that the signal handler will unconditionally reset signal_pending to
* false, so that can change asynchronously even when alarm_enabled is false.
*/
static volatile sig_atomic_t signal_pending = false;
-static TimestampTz signal_due_at = 0; /* valid only when signal_pending */+static volatile TimestampTz signal_due_at = 0;
/*****************************************************************************
@@ -217,10 +218,21 @@ schedule_alarm(TimestampTz now)
MemSet(&timeval, 0, sizeof(struct itimerval));
+ /*+ * If we think there's a signal pending, but current time is more than+ * 10ms past when the signal was due, then assume that the timeout+ * request got lost somehow and re-enable it. (10ms corresponds to+ * the worst-case timeout granularity on modern systems.) It won't+ * hurt us if the interrupt does manage to fire between now and when+ * we reach the setitimer() call.+ */+ if (signal_pending && now > signal_due_at + 10 * 1000)+ signal_pending = false;+
/*
* Get the time remaining till the nearest pending timeout. If it is
* negative, assume that we somehow missed an interrupt, and force
- * signal_pending off. This gives us a chance to recover if the+ * signal_pending off. This gives us another chance to recover if the
* kernel drops a timeout request for some reason.
*/
nearest_timeout = active_timeouts[0]->fin_time;
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, harmic@gmail.com, simon.riggs@enterprisedb.com, alvherre@alvh.no-ip.org
Subject: Re: Undetected Deadlock
In-Reply-To: <794618.1644188230@sss.pgh.pa.us>
References: <CADofcAWbMrvgwSMqO4iG_iD3E2v8ZUrC-_crB41my=VMM02-CA@mail.gmail.com> <202201252311.4jc4gpdzwwu7@alvherre.pgsql> <CADofcAXSn_oS3MOxS=1epiXirwETOB_BpBmM8w4=F3GaQe+cwA@mail.gmail.com> <CADofcAV29umxzTFPuJu+Cvvd5cZKkQxK0TZmpVJenWjok-=gYw@mail.gmail.com> <CADofcAVJdqqsm45R2LTihNaVhZ4sdSJkBXEwEV7tJ8BQ+s4gRA@mail.gmail.com> <CANbhV-FoXqh3YWuJLoH-36Mb+gwXG4XQdSy=9x-Zv72BFGhVww@mail.gmail.com> <272367.1643906187@sss.pgh.pa.us> <CADofcAVWMdOfyND0Mf6agsr9mOtjy+Fe=yNGMRdnSP1NYZx6qg@mail.gmail.com> <794618.1644188230@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