From dd892f0d8bea26e8b4b7b7ff224d7a16e021232e Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-46-230.ec2.internal>
Date: Mon, 3 Nov 2025 17:36:19 +0000
Subject: [PATCH v1 1/1] Prevent tranche_id leak in test_dsa_resowners

tranche_id was previously allocated on every call and intentionally
leaked, which was acceptable for this test. However, 38b602b02 no
longer tolerates such leaks, and this can cause test_dsa_resowners to
fail to release resources properly.

tranche_id is now static and allocated only once by checking that its
value is below LWTRANCHE_FIRST_USER_DEFINED. This is sufficient for
this test. In general, tranche IDs should be tracked in shared memory.

Discussion: https://www.postgresql.org/message-id/dd36d384-55df-4fc2-825c-5bc56c950fa9%40gmail.com
---
 src/test/modules/test_dsa/test_dsa.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/test/modules/test_dsa/test_dsa.c b/src/test/modules/test_dsa/test_dsa.c
index 01d5c6fa67f..09670964ee9 100644
--- a/src/test/modules/test_dsa/test_dsa.c
+++ b/src/test/modules/test_dsa/test_dsa.c
@@ -62,14 +62,18 @@ PG_FUNCTION_INFO_V1(test_dsa_resowners);
 Datum
 test_dsa_resowners(PG_FUNCTION_ARGS)
 {
-	int			tranche_id;
+	static int	tranche_id;
 	dsa_area   *a;
 	dsa_pointer p[10000];
 	ResourceOwner oldowner;
 	ResourceOwner childowner;
 
-	/* XXX: this tranche is leaked */
-	tranche_id = LWLockNewTrancheId("test_dsa");
+	/*
+	 * Allocate a tranche ID once.  A static variable is fine for this test;
+	 * it otherwise should be tracked in shared memory.
+	 */
+	if (tranche_id < LWTRANCHE_FIRST_USER_DEFINED)
+		tranche_id = LWLockNewTrancheId("test_dsa");
 
 	/* Create DSA in parent resource owner */
 	a = dsa_create(tranche_id);
-- 
2.43.0

