Skip to content

Commit 2a08477

Browse files
committed
Use standard SIGHUP and SIGTERM signal handlers in worker_spi.
Previously worker_spi used its custom signal handlers for SIGHUP and SIGTERM. This commit makes worker_spi use the standard signal handlers, to simplify the code. Note that die() is used as the standard SIGTERM signal handler in worker_spi instead of SignalHandlerForShutdownRequest() or bgworker_die(). Previously the exit handling was only able to exit from within the main loop, and not from within the backend code it calls. This is why die() needs to be used here, so worker_spi can respond to SIGTERM signal while it's executing a query. Maybe we can say that it's a bug that worker_spi could not respond to SIGTERM during query execution. But since worker_spi is a just example of the background worker code, we don't do the back-patch. Thanks to Craig Ringer for the report and investigation of the issue. Author: Bharath Rupireddy Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/CALj2ACXDEZhAFOTDcqO9cFSRvrFLyYOnPKrcA1UG4uZn9hUAVg@mail.gmail.com Discussion: https://postgr.es/m/CAGRY4nxsAe_1k_9g5b47orA0S011iBoHsXHFMH7cg7HV0O1bwQ@mail.gmail.com
1 parent 0926e96 commit 2a08477

File tree

1 file changed

+9
-43
lines changed

1 file changed

+9
-43
lines changed

src/test/modules/worker_spi/worker_spi.c

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/* These are always necessary for a bgworker */
2626
#include "miscadmin.h"
2727
#include "postmaster/bgworker.h"
28+
#include "postmaster/interrupt.h"
2829
#include "storage/ipc.h"
2930
#include "storage/latch.h"
3031
#include "storage/lwlock.h"
@@ -48,10 +49,6 @@ PG_FUNCTION_INFO_V1(worker_spi_launch);
4849
void _PG_init(void);
4950
void worker_spi_main(Datum) pg_attribute_noreturn();
5051

51-
/* flags set by signal handlers */
52-
static volatile sig_atomic_t got_sighup = false;
53-
static volatile sig_atomic_t got_sigterm = false;
54-
5552
/* GUC variables */
5653
static int worker_spi_naptime = 10;
5754
static int worker_spi_total_workers = 2;
@@ -64,38 +61,6 @@ typedef struct worktable
6461
const char *name;
6562
} worktable;
6663

67-
/*
68-
* Signal handler for SIGTERM
69-
* Set a flag to let the main loop to terminate, and set our latch to wake
70-
* it up.
71-
*/
72-
static void
73-
worker_spi_sigterm(SIGNAL_ARGS)
74-
{
75-
int save_errno = errno;
76-
77-
got_sigterm = true;
78-
SetLatch(MyLatch);
79-
80-
errno = save_errno;
81-
}
82-
83-
/*
84-
* Signal handler for SIGHUP
85-
* Set a flag to tell the main loop to reread the config file, and set
86-
* our latch to wake it up.
87-
*/
88-
static void
89-
worker_spi_sighup(SIGNAL_ARGS)
90-
{
91-
int save_errno = errno;
92-
93-
got_sighup = true;
94-
SetLatch(MyLatch);
95-
96-
errno = save_errno;
97-
}
98-
9964
/*
10065
* Initialize workspace for a worker process: create the schema if it doesn't
10166
* already exist.
@@ -179,8 +144,8 @@ worker_spi_main(Datum main_arg)
179144
table->name = pstrdup("counted");
180145

181146
/* Establish signal handlers before unblocking signals. */
182-
pqsignal(SIGHUP, worker_spi_sighup);
183-
pqsignal(SIGTERM, worker_spi_sigterm);
147+
pqsignal(SIGHUP, SignalHandlerForConfigReload);
148+
pqsignal(SIGTERM, die);
184149

185150
/* We're now ready to receive signals */
186151
BackgroundWorkerUnblockSignals();
@@ -219,9 +184,10 @@ worker_spi_main(Datum main_arg)
219184
table->name);
220185

221186
/*
222-
* Main loop: do this until the SIGTERM handler tells us to terminate
187+
* Main loop: do this until SIGTERM is received and processed by
188+
* ProcessInterrupts.
223189
*/
224-
while (!got_sigterm)
190+
for (;;)
225191
{
226192
int ret;
227193

@@ -242,9 +208,9 @@ worker_spi_main(Datum main_arg)
242208
/*
243209
* In case of a SIGHUP, just reload the configuration.
244210
*/
245-
if (got_sighup)
211+
if (ConfigReloadPending)
246212
{
247-
got_sighup = false;
213+
ConfigReloadPending = false;
248214
ProcessConfigFile(PGC_SIGHUP);
249215
}
250216

@@ -303,7 +269,7 @@ worker_spi_main(Datum main_arg)
303269
pgstat_report_activity(STATE_IDLE, NULL);
304270
}
305271

306-
proc_exit(1);
272+
/* Not reachable */
307273
}
308274

309275
/*

0 commit comments

Comments
 (0)