Skip to content

Commit cbd9619

Browse files
committed
Block signals while computing the sleep time in postmaster's main loop.
DetermineSleepTime() was previously called without blocked signals. That's not good, because it allows signal handlers to interrupt its workings. DetermineSleepTime() was added in 9.3 with the addition of background workers (da07a1e), where it only read from BackgroundWorkerList. Since 9.4, where dynamic background workers were added (7f7485a), the list is also manipulated in DetermineSleepTime(). That's bad because the list now can be persistently corrupted if modified by both a signal handler and DetermineSleepTime(). This was discovered during the investigation of hangs on buildfarm member anole. It's unclear whether this bug is the source of these hangs or not, but it's worth fixing either way. I have confirmed that it can cause crashes. It luckily looks like this only can cause problems when bgworkers are actively used. Discussion: 20140929193733.GB14400@awork2.anarazel.de Backpatch to 9.3 where background workers were introduced.
1 parent 9adda98 commit cbd9619

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,8 @@ DetermineSleepTime(struct timeval * timeout)
15021502

15031503
/*
15041504
* Main idle loop of postmaster
1505+
*
1506+
* NB: Needs to be called with signals blocked
15051507
*/
15061508
static int
15071509
ServerLoop(void)
@@ -1523,34 +1525,38 @@ ServerLoop(void)
15231525
/*
15241526
* Wait for a connection request to arrive.
15251527
*
1528+
* We block all signals except while sleeping. That makes it safe for
1529+
* signal handlers, which again block all signals while executing, to
1530+
* do nontrivial work.
1531+
*
15261532
* If we are in PM_WAIT_DEAD_END state, then we don't want to accept
1527-
* any new connections, so we don't call select() at all; just sleep
1528-
* for a little bit with signals unblocked.
1533+
* any new connections, so we don't call select(), and just sleep.
15291534
*/
15301535
memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
15311536

1532-
PG_SETMASK(&UnBlockSig);
1533-
15341537
if (pmState == PM_WAIT_DEAD_END)
15351538
{
1539+
PG_SETMASK(&UnBlockSig);
1540+
15361541
pg_usleep(100000L); /* 100 msec seems reasonable */
15371542
selres = 0;
1543+
1544+
PG_SETMASK(&BlockSig);
15381545
}
15391546
else
15401547
{
15411548
/* must set timeout each time; some OSes change it! */
15421549
struct timeval timeout;
15431550

1551+
/* Needs to run with blocked signals! */
15441552
DetermineSleepTime(&timeout);
15451553

1554+
PG_SETMASK(&UnBlockSig);
1555+
15461556
selres = select(nSockets, &rmask, NULL, NULL, &timeout);
1547-
}
15481557

1549-
/*
1550-
* Block all signals until we wait again. (This makes it safe for our
1551-
* signal handlers to do nontrivial work.)
1552-
*/
1553-
PG_SETMASK(&BlockSig);
1558+
PG_SETMASK(&BlockSig);
1559+
}
15541560

15551561
/* Now check the select() result */
15561562
if (selres < 0)

0 commit comments

Comments
 (0)