While working on the "Interrupts vs signals" patch, my AI review also flagged this one pre-existing issue:

On 09/09/2026 00:38, Daniel Gustafsson wrote:
* A registered worker can outlive a terminated launcher: Relation I/O and
full-page-image WAL can begin after `SHOW data_checksums` reports `off` and the
launcher has exited
- This requires termination within a very tight window, and cannot lead to
wrong results or corruption.  Will look at a fix.

That's pretty harmless, but there's also a simple fix: increment the "invocation number" when the launcher exits. That way, the worker will quickly see that the invocation number doesn't match and exit, even if the launcher fails to signal it.

Attached patch 0001 implements that. Patch 0002 is an AI generated test case to demonstrate the issue. It's pretty artificial, and the consequence is very minor so the test doesn't seem worth committing. But any thoughts on patch 0001?

- Heikki
From 1b407eb13c26ed9bb281027e12072fdfbedc525f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
Date: Mon, 30 Nov 2026 23:48:40 +0200
Subject: [PATCH 1/2] Bump data checksums invocation counter when launcher
 exits

When the data checksums launcher exits, launcher_exit() sends SIGTERM
to the worker, but it can only do that if it knows the worker's PID.
The launcher knows it only after WaitForDataChecksumsWorkerState()
reports that the worker has started.  If the launcher exited after
registering the worker but before recording its PID, e.g. because it
received SIGTERM while waiting for it to start up, the worker was not
signalled and would proceed to start up and run.

To fix, increment the current invocation number in launcher_exit().
That way, if a worker has been launched but hasn't fully started up
yet, it will see that the invocation number has changed when it starts
up, and exit.
---
 src/backend/postmaster/datachecksum_state.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index e2b673a96b5..10f76fd09e4 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1138,6 +1138,17 @@ launcher_exit(int code, Datum arg)
 	if (launcher_running)
 	{
 		LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
+
+		/*
+		 * Increment the invocation number so that if a worker is running, it
+		 * will see that the current invocation aborted and bail out quickly.
+		 * We also send SIGTERM to the worker, but bumping the invocation
+		 * number covers the case that the worker was launched, but we hadn't
+		 * set DataChecksumState->worker_pid yet.
+		 */
+		DataChecksumState->worker_invocation =
+			++DataChecksumState->worker_invocation_counter;
+
 		if (DataChecksumState->worker_pid != InvalidPid)
 		{
 			ereport(LOG,
-- 
2.47.3

From 53b6fb29d2afa674a9dd896bf68aac95de11f057 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <[email protected]>
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

Reply via email to