From e89b68502d4fa7c84ce637c27a81eb502b51eaff Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddy@enterprisedb.com>
Date: Tue, 17 Nov 2020 17:33:25 +0530
Subject: [PATCH v1] Use die() instead of custom signal handler in test_shm_mq
 worker

DoingCommandRead can never be true in bg workers as it's being set
to true only in normal backend PostgresMain(). If we use die()
(instead of custom SIGTERM handlers such as handle_sigterm()) in a
bg worker/non-backend process, there are no chances that the
DoinCommandREad part of the code gets hit and the interrupts are
processed immediately. And also here are the bg worker process
that use die() instead of their own custom handlers:
parallel workers, autoprewarm worker, autovacuum worker,
logical replication launcher and apply worker, wal sender process
---
 src/test/modules/test_shm_mq/worker.c | 28 +++------------------------
 1 file changed, 3 insertions(+), 25 deletions(-)

diff --git a/src/test/modules/test_shm_mq/worker.c b/src/test/modules/test_shm_mq/worker.c
index cb63adebee..3f2e9bf812 100644
--- a/src/test/modules/test_shm_mq/worker.c
+++ b/src/test/modules/test_shm_mq/worker.c
@@ -24,10 +24,10 @@
 #include "storage/procarray.h"
 #include "storage/shm_mq.h"
 #include "storage/shm_toc.h"
+#include "tcop/tcopprot.h"
 
 #include "test_shm_mq.h"
 
-static void handle_sigterm(SIGNAL_ARGS);
 static void attach_to_queues(dsm_segment *seg, shm_toc *toc,
 							 int myworkernumber, shm_mq_handle **inqhp,
 							 shm_mq_handle **outqhp);
@@ -58,10 +58,9 @@ test_shm_mq_main(Datum main_arg)
 	 * Establish signal handlers.
 	 *
 	 * We want CHECK_FOR_INTERRUPTS() to kill off this worker process just as
-	 * it would a normal user backend.  To make that happen, we establish a
-	 * signal handler that is a stripped-down version of die().
+	 * it would a normal user backend.  To make that happen, we use die().
 	 */
-	pqsignal(SIGTERM, handle_sigterm);
+	pqsignal(SIGTERM, die);
 	BackgroundWorkerUnblockSignals();
 
 	/*
@@ -196,24 +195,3 @@ copy_messages(shm_mq_handle *inqh, shm_mq_handle *outqh)
 			break;
 	}
 }
-
-/*
- * When we receive a SIGTERM, we set InterruptPending and ProcDiePending just
- * like a normal backend.  The next CHECK_FOR_INTERRUPTS() will do the right
- * thing.
- */
-static void
-handle_sigterm(SIGNAL_ARGS)
-{
-	int			save_errno = errno;
-
-	SetLatch(MyLatch);
-
-	if (!proc_exit_inprogress)
-	{
-		InterruptPending = true;
-		ProcDiePending = true;
-	}
-
-	errno = save_errno;
-}
-- 
2.25.1

