From 53b6fb29d2afa674a9dd896bf68aac95de11f057 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 30 Nov 2026 23:50:47 +0200
Subject: [PATCH 2/2] Add test for data checksums worker outliving the launcher

Add an injection point in the data checksums launcher right after it
has registered a worker, before it has learned the worker's PID, and
use it to test that the worker aborts if the launcher exits at that
point.
---
 src/backend/postmaster/datachecksum_state.c   |  2 +
 .../modules/test_checksums/t/005_injection.pl | 55 +++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index 10f76fd09e4..413e1d46578 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1012,6 +1012,8 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 		return DATACHECKSUMSWORKER_FAILED;
 	}
 
+	INJECTION_POINT("datachecksumsworker-launcher-worker-registered", NULL);
+
 	/*
 	 * While this expects to wait for BGWORKER_STARTUP it may return _STOPPED
 	 * if the worker was terminated in the meantime so we must check status.
diff --git a/src/test/modules/test_checksums/t/005_injection.pl b/src/test/modules/test_checksums/t/005_injection.pl
index 8dcbe971466..1154f7194d2 100644
--- a/src/test/modules/test_checksums/t/005_injection.pl
+++ b/src/test/modules/test_checksums/t/005_injection.pl
@@ -205,6 +205,61 @@ like(
 	qr/create database strategy "file_copy" not allowed/m,
 	'file_copy error message in log');
 
+# ---------------------------------------------------------------------------
+# Test that the worker stops if the launcher exits before it has learned the
+# worker's PID, and so cannot signal it.
+#
+
+disable_data_checksums($node, wait => 1);
+$node_loglocation = -s $node->logfile;
+
+# Hold the launcher after it has registered the worker, and the worker before
+# it finishes processing its database.
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('datachecksumsworker-launcher-worker-registered','wait');"
+);
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('datachecksumsworker-fake-temptable-wait','wait');"
+);
+
+enable_data_checksums($node);
+$node->wait_for_event('datachecksums launcher',
+	'datachecksumsworker-launcher-worker-registered');
+$node->wait_for_event('datachecksums worker',
+	'datachecksumsworker-fake-temptable-wait');
+
+# Terminate the launcher while it doesn't know the worker's PID yet.
+$node->safe_psql('postgres',
+		"SELECT pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity "
+	  . "WHERE backend_type = 'datachecksums launcher';");
+$node->poll_query_until('postgres',
+		"SELECT count(*) = 0 FROM pg_catalog.pg_stat_activity "
+	  . "WHERE backend_type = 'datachecksums launcher';")
+  or die "timed out waiting for datachecksums launcher to exit";
+$node->safe_psql('postgres',
+	"SELECT injection_points_detach('datachecksumsworker-launcher-worker-registered');"
+);
+
+# Release the worker.  It must notice that the launcher is gone and abort,
+# rather than carry on processing.
+$node->safe_psql('postgres',
+	"SELECT injection_points_wakeup('datachecksumsworker-fake-temptable-wait');"
+);
+$node->safe_psql('postgres',
+	"SELECT injection_points_detach('datachecksumsworker-fake-temptable-wait');"
+);
+$node->poll_query_until('postgres',
+		"SELECT count(*) = 0 FROM pg_catalog.pg_stat_activity "
+	  . "WHERE backend_type = 'datachecksums worker';")
+  or die "timed out waiting for datachecksums worker to exit";
+
+$log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $node_loglocation);
+like(
+	$log,
+	qr/data checksum processing aborted in database OID/,
+	'worker aborted after the launcher exited');
+test_checksum_state($node, 'off');
+
 # ---------------------------------------------------------------------------
 # Test teardown
 #
-- 
2.47.3

