Skip to content

Commit 214c7a4

Browse files
committed
Fix some more bugs in signal handlers and process shutdown logic.
WalSndKill was doing things exactly backwards: it should first clear MyWalSnd (to stop signal handlers from touching MyWalSnd->latch), then disown the latch, and only then mark the WalSnd struct unused by clearing its pid field. Also, WalRcvSigUsr1Handler and worker_spi_sighup failed to preserve errno, which is surely a requirement for any signal handler. Per discussion of recent buildfarm failures. Back-patch as far as the relevant code exists.
1 parent 7e1531a commit 214c7a4

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

contrib/worker_spi/worker_spi.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "tcop/utility.h"
4343

4444
PG_MODULE_MAGIC;
45+
4546
PG_FUNCTION_INFO_V1(worker_spi_launch);
4647

4748
void _PG_init(void);
@@ -82,15 +83,19 @@ worker_spi_sigterm(SIGNAL_ARGS)
8283

8384
/*
8485
* Signal handler for SIGHUP
85-
* Set a flag to let the main loop to reread the config file, and set
86+
* Set a flag to tell the main loop to reread the config file, and set
8687
* our latch to wake it up.
8788
*/
8889
static void
8990
worker_spi_sighup(SIGNAL_ARGS)
9091
{
92+
int save_errno = errno;
93+
9194
got_sighup = true;
9295
if (MyProc)
9396
SetLatch(&MyProc->procLatch);
97+
98+
errno = save_errno;
9499
}
95100

96101
/*

src/backend/replication/walreceiver.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,11 @@ WalRcvSigHupHandler(SIGNAL_ARGS)
740740
static void
741741
WalRcvSigUsr1Handler(SIGNAL_ARGS)
742742
{
743+
int save_errno = errno;
744+
743745
latch_sigusr1_handler();
746+
747+
errno = save_errno;
744748
}
745749

746750
/* SIGTERM: set flag for main loop, or shutdown immediately if safe */

src/backend/replication/walsender.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,17 +1401,23 @@ InitWalSenderSlot(void)
14011401
static void
14021402
WalSndKill(int code, Datum arg)
14031403
{
1404-
Assert(MyWalSnd != NULL);
1404+
WalSnd *walsnd = MyWalSnd;
1405+
1406+
Assert(walsnd != NULL);
1407+
1408+
/*
1409+
* Clear MyWalSnd first; then disown the latch. This is so that signal
1410+
* handlers won't try to touch the latch after it's no longer ours.
1411+
*/
1412+
MyWalSnd = NULL;
1413+
1414+
DisownLatch(&walsnd->latch);
14051415

14061416
/*
14071417
* Mark WalSnd struct no longer in use. Assume that no lock is required
14081418
* for this.
14091419
*/
1410-
MyWalSnd->pid = 0;
1411-
DisownLatch(&MyWalSnd->latch);
1412-
1413-
/* WalSnd struct isn't mine anymore */
1414-
MyWalSnd = NULL;
1420+
walsnd->pid = 0;
14151421
}
14161422

14171423
/*

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