From 9eb53ab99734431ca5382ec801c6609a3b8f7942 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Tue, 1 Dec 2026 22:47:32 +0000
Subject: [PATCH v2 1/3] Don't clean up after a data checksums launcher that
 never started

launcher_exit() cleans up after the data checksums launcher: it
signals the worker, turns data checksums off if enabling did not
finish, and clears the shared launcher_running flag.  It is registered,
and the local launcher_running flag was set, before the launcher
connects to the database, while the shared flag is set only after
that.  If connection setup failed, shmem_exit() released
DataChecksumsWorkerLock before running the exit callbacks, so a
duplicate launcher waiting for the lock could find the shared flag
unset and take over.  launcher_exit() in the failed launcher then acted
as if it owned the operation: it could signal the new launcher's
worker, turn data checksums off underneath it, and clear its shared
flag.

To fix, set the local flag together with the shared one, and add a
launcher_started flag that makes launcher_exit() do nothing in a
launcher that never got that far.  The callback has to stay registered
before connecting: launcher_exit() may wait for a ProcSignalBarrier,
which only works if our own ProcSignal slot is already gone by then, as
interrupts are held during exit.  Add a comment about that.

This needs an error in InitPostgres(), so it's unlikely in practice.

Discussion: https://www.postgresql.org/message-id/3ed1ca68-7409-4170-8a99-32e1704ff4aa@iki.fi
---
 src/backend/postmaster/datachecksum_state.c | 30 ++++++++++++++++-----
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index e2b673a96b5..bac84843d24 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -394,6 +394,12 @@ static uint64 worker_invocation;
  */
 static volatile sig_atomic_t launcher_running = false;
 
+/*
+ * Did this process ever take over as the launcher?  Unlike launcher_running,
+ * this stays set after we have given up the role.
+ */
+static bool launcher_started = false;
+
 /* Are we enabling data checksums, or disabling them? */
 static DataChecksumsWorkerOperation operation;
 
@@ -1124,17 +1130,21 @@ done:
  * launcher_exit
  *
  * Internal routine for cleaning up state when a launcher process which has
- * performed checksum operations exits. A launcher process which is exiting due
- * to a duplicate started launcher does not need to perform any cleanup and
- * this function should not be called. Otherwise, we need to clean up the abort
- * flag to ensure that processing can be started again if it was previously
- * aborted (note: started again, *not* restarted from where it left off).
+ * performed checksum operations exits. A launcher process which never took
+ * over the launcher role, because another launcher was already running or
+ * because it failed during startup, must not perform any cleanup. Otherwise,
+ * we need to clean up the abort flag to ensure that processing can be started
+ * again if it was previously aborted (note: started again, *not* restarted
+ * from where it left off).
  */
 static void
 launcher_exit(int code, Datum arg)
 {
 	abort_requested = false;
 
+	if (!launcher_started)
+		return;
+
 	if (launcher_running)
 	{
 		LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
@@ -1282,13 +1292,21 @@ DataChecksumsWorkerLauncherMain(Datum arg)
 		return;
 	}
 
+	/*
+	 * Register the exit callback before connecting, so that it runs after our
+	 * ProcSignal slot has been released.  launcher_exit() may have to wait
+	 * for a ProcSignalBarrier, which we could not absorb ourselves with
+	 * interrupts held during exit.  The callback does nothing until
+	 * launcher_started is set below.
+	 */
 	on_shmem_exit(launcher_exit, 0);
-	launcher_running = true;
 
 	/* Initialize a connection to shared catalogs only */
 	BackgroundWorkerInitializeConnectionByOid(InvalidOid, InvalidOid, 0);
 
 	operation = DataChecksumState->launch_operation;
+	launcher_started = true;
+	launcher_running = true;
 	DataChecksumState->launcher_running = true;
 	DataChecksumState->operation = operation;
 	DataChecksumState->cost_delay = DataChecksumState->launch_cost_delay;
-- 
2.56.1

