From 681a800da5a30645209c36aa7a8181a0a5dabd0a Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Tue, 1 Dec 2026 22:23:36 +0000
Subject: [PATCH v2 3/3] 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 exits if the launcher exits at that
point, even while the worker is waiting for a lock.

Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Author: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://www.postgresql.org/message-id/3ed1ca68-7409-4170-8a99-32e1704ff4aa@iki.fi
---
 src/backend/postmaster/datachecksum_state.c   |  2 +
 .../modules/test_checksums/t/005_injection.pl | 52 +++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index b6d2cf30208..e73070be3a6 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1020,6 +1020,8 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 	}
 	worker_handle = bgw_handle;
 
+	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..40a7da2ef22 100644
--- a/src/test/modules/test_checksums/t/005_injection.pl
+++ b/src/test/modules/test_checksums/t/005_injection.pl
@@ -205,6 +205,58 @@ like(
 	qr/create database strategy "file_copy" not allowed/m,
 	'file_copy error message in log');
 
+# ---------------------------------------------------------------------------
+# Test that the worker is terminated if the launcher exits before it has
+# learned the worker's PID, even when the worker is waiting for a lock and
+# does not get to check for an abort request.
+#
+
+disable_data_checksums($node, wait => 1);
+
+# The first worker processes the shared catalogs, block it on one of them.
+# With wal_level=minimal the lock doesn't assign an XID, so it doesn't hold up
+# the launcher's wait for running transactions.
+my $locker = $node->background_psql('postgres');
+$locker->query_safe(
+	'BEGIN; LOCK TABLE pg_shdescription IN ACCESS EXCLUSIVE MODE;');
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('datachecksumsworker-launcher-worker-registered','wait');"
+);
+enable_data_checksums($node);
+$node->wait_for_event('datachecksums launcher',
+	'datachecksumsworker-launcher-worker-registered');
+$node->poll_query_until('postgres',
+		"SELECT count(*) = 1 FROM pg_catalog.pg_stat_activity "
+	  . "WHERE backend_type = 'datachecksums worker' "
+	  . "AND wait_event_type = 'Lock';")
+  or die "timed out waiting for datachecksums worker to block";
+
+$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');"
+);
+
+# The worker must go away while the lock is still held.
+ok( $node->poll_query_until(
+			'postgres',
+			"SELECT count(*) = 0 FROM pg_catalog.pg_stat_activity "
+			  . "WHERE backend_type = 'datachecksums worker';"),
+	'blocked worker exits after the launcher exited');
+
+$locker->query_safe('COMMIT;');
+$locker->quit;
+
+# The launcher disappears from pg_stat_activity before its exit callback has
+# turned data checksums off, so wait for that.
+wait_for_checksum_state($node, 'off');
+
 # ---------------------------------------------------------------------------
 # Test teardown
 #
-- 
2.56.1

