v2-0001-Fix-crash-with-RemoveFromWaitQueue-when-detecting.patch

application/x-patch

Filename: v2-0001-Fix-crash-with-RemoveFromWaitQueue-when-detecting.patch
Type: application/x-patch
Part: 0
Message: Re: BUG #18031: Segmentation fault after deadlock within VACUUM's parallel worker

Patch

Format: format-patch
Series: patch v2-0001
Subject: Fix crash with RemoveFromWaitQueue() when detecting a deadlock.
File+
src/backend/storage/lmgr/lock.c 1 1
From 238b4c68759187143c4a9bf4b1c314b19a534301 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Tue, 25 Jul 2023 16:04:41 +0900
Subject: [PATCH v2] Fix crash with RemoveFromWaitQueue() when detecting a
 deadlock.

Commit 5764f611e used dclist_delete_from() to remove the proc from the
wait queue. However, since it doesn't clear dist_node's next/prev to
NULL, it could call RemoveFromWaitQueue() twice: when the process
detects a deadlock and then when cleaning up locks on aborting the
transaction. The waiting lock information is cleared in the first
call, so it led to a crash in the second call.

Backpatch to v16, where the change was introduced.

Bug: #18031
Reported-by: Justin Pryzby, Alexander Lakhin
Reviewed-by: Andres Freund
Discussion: https://postgr.es/m/ZKy4AdrLEfbqrxGJ%40telsasoft.com
Discussion: https://postgr.es/m/18031-ebe2d08cb405f6cc@postgresql.org
Backpatch-through: 16
---
 src/backend/storage/lmgr/lock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index f595bce31b..ec6240fbae 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1881,7 +1881,7 @@ RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
 	Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
 
 	/* Remove proc from lock's wait queue */
-	dclist_delete_from(&waitLock->waitProcs, &proc->links);
+	dclist_delete_from_thoroughly(&waitLock->waitProcs, &proc->links);
 
 	/* Undo increments of request counts by waiting process */
 	Assert(waitLock->nRequested > 0);
-- 
2.31.1