Skip to content

Commit e8312b4

Browse files
committed
Don't let timeout interrupts happen unless ImmediateInterruptOK is set.
Serious oversight in commit 16e1b7a: we should not allow an interrupt to take control away from mainline code except when ImmediateInterruptOK is set. Just to be safe, let's adopt the same save-clear-restore dance that's been used for many years in HandleCatchupInterrupt and HandleNotifyInterrupt, so that nothing bad happens if a timeout handler invokes code that tests or even manipulates ImmediateInterruptOK. Per report of "stuck spinlock" failures from Christophe Pettus, though many other symptoms are possible. Diagnosis by Andres Freund.
1 parent 50e5470 commit e8312b4

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/backend/utils/misc/timeout.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,27 @@ static void
259259
handle_sig_alarm(SIGNAL_ARGS)
260260
{
261261
int save_errno = errno;
262+
bool save_ImmediateInterruptOK = ImmediateInterruptOK;
262263

263264
/*
264265
* We may be executing while ImmediateInterruptOK is true (e.g., when
265266
* mainline is waiting for a lock). If SIGINT or similar arrives while
266267
* this code is running, we'd lose control and perhaps leave our data
267-
* structures in an inconsistent state. Hold off interrupts to prevent
268-
* that.
268+
* structures in an inconsistent state. Disable immediate interrupts, and
269+
* just to be real sure, bump the holdoff counter as well. (The reason
270+
* for this belt-and-suspenders-too approach is to make sure that nothing
271+
* bad happens if a timeout handler calls code that manipulates
272+
* ImmediateInterruptOK.)
269273
*
270-
* Note: it's possible for a SIGINT to interrupt handle_sig_alarm even
271-
* before we reach HOLD_INTERRUPTS(); the net effect would be as if the
272-
* SIGALRM event had been silently lost. Therefore error recovery must
273-
* include some action that will allow any lost interrupt to be
274-
* rescheduled. Disabling some or all timeouts is sufficient, or if
275-
* that's not appropriate, reschedule_timeouts() can be called. Also, the
276-
* signal blocking hazard described below applies here too.
274+
* Note: it's possible for a SIGINT to interrupt handle_sig_alarm before
275+
* we manage to do this; the net effect would be as if the SIGALRM event
276+
* had been silently lost. Therefore error recovery must include some
277+
* action that will allow any lost interrupt to be rescheduled. Disabling
278+
* some or all timeouts is sufficient, or if that's not appropriate,
279+
* reschedule_timeouts() can be called. Also, the signal blocking hazard
280+
* described below applies here too.
277281
*/
282+
ImmediateInterruptOK = false;
278283
HOLD_INTERRUPTS();
279284

280285
/*
@@ -330,9 +335,12 @@ handle_sig_alarm(SIGNAL_ARGS)
330335
}
331336

332337
/*
333-
* Re-allow query cancel, and then service any cancel request that arrived
334-
* meanwhile (this might in particular include a cancel request fired by
335-
* one of the timeout handlers).
338+
* Re-allow query cancel, and then try to service any cancel request that
339+
* arrived meanwhile (this might in particular include a cancel request
340+
* fired by one of the timeout handlers). Since we are in a signal
341+
* handler, we mustn't call ProcessInterrupts unless ImmediateInterruptOK
342+
* is set; if it isn't, the cancel will happen at the next mainline
343+
* CHECK_FOR_INTERRUPTS.
336344
*
337345
* Note: a longjmp from here is safe so far as our own data structures are
338346
* concerned; but on platforms that block a signal before calling the
@@ -341,7 +349,9 @@ handle_sig_alarm(SIGNAL_ARGS)
341349
* unblocking any blocked signals.
342350
*/
343351
RESUME_INTERRUPTS();
344-
CHECK_FOR_INTERRUPTS();
352+
ImmediateInterruptOK = save_ImmediateInterruptOK;
353+
if (save_ImmediateInterruptOK)
354+
CHECK_FOR_INTERRUPTS();
345355

346356
errno = save_errno;
347357
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy