This is an automated email from the ASF dual-hosted git repository.

avamingli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git


The following commit(s) were added to refs/heads/main by this push:
     new 644b4e3073 Fix volatile qualifier discard and ensure safe access to 
PGPROC in lock.c
644b4e3073 is described below

commit 644b4e307380954e986f048d0a7910a3063e524e
Author: Jianghua Yang <[email protected]>
AuthorDate: Wed Feb 26 12:29:37 2025 +0800

    Fix volatile qualifier discard and ensure safe access to PGPROC in lock.c
    
    Fix https://github.com/apache/cloudberry/issues/958
---
 src/backend/storage/ipc/procarray.c | 4 ++--
 src/backend/storage/lmgr/lock.c     | 9 +++++++--
 src/include/storage/procarray.h     | 2 +-
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/backend/storage/ipc/procarray.c 
b/src/backend/storage/ipc/procarray.c
index f16b8f4243..3154caba1b 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -5024,7 +5024,7 @@ DisplayXidCache(void)
 }
 #endif                                                 /* XIDCACHE_DEBUG */
 
-PGPROC *
+volatile PGPROC *
 FindProcByGpSessionId(long gp_session_id)
 {
        /* Find the guy who should manage our locks */
@@ -5037,7 +5037,7 @@ FindProcByGpSessionId(long gp_session_id)
 
        for (index = 0; index < arrayP->numProcs; index++)
        {
-               PGPROC     *proc = &allProcs[arrayP->pgprocnos[index]];
+               volatile PGPROC    *proc = &allProcs[arrayP->pgprocnos[index]];
                        
                if (proc->pid == MyProc->pid)
                        continue;
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 26a52b5859..90ea0a666e 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -936,19 +936,24 @@ LockAcquireExtended(const LOCKTAG *locktag,
                        if (lockHolderProcPtr == MyProc)
                        {
                                /* Find the guy who should manage our locks */
-                               PGPROC * proc = 
FindProcByGpSessionId(gp_session_id);
+                               volatile PGPROC * proc = 
FindProcByGpSessionId(gp_session_id);
                                int count = 0;
                                while(proc==NULL && count < 5)
                                {
                                        pg_usleep( /* microseconds */ 2000);
                                        count++;
                                        CHECK_FOR_INTERRUPTS();
+                                       /*
+                                        * The reason for using 
pg_memory_barrier() is to ensure that
+                                        * all CPU cores can see the latest 
shared memory modifications.
+                                        */
+                                       pg_memory_barrier();
                                        proc = 
FindProcByGpSessionId(gp_session_id);
                                }
                                if (proc != NULL)
                                {
                                        elog(DEBUG1,"Found writer proc entry.  
My Pid %d, his pid %d", MyProc-> pid, proc->pid);
-                                       lockHolderProcPtr = proc;
+                                       lockHolderProcPtr = (PGPROC*) proc;
                                }
                                else
                                        ereport(FATAL,
diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h
index cecae9371d..607834e46c 100644
--- a/src/include/storage/procarray.h
+++ b/src/include/storage/procarray.h
@@ -99,7 +99,7 @@ extern void XidCacheRemoveRunningXids(TransactionId xid,
                                                                          int 
nxids, const TransactionId *xids,
                                                                          
TransactionId latestXid);
                                                  
-extern PGPROC *FindProcByGpSessionId(long gp_session_id);
+extern volatile PGPROC *FindProcByGpSessionId(long gp_session_id);
 extern void UpdateCommandIdInSnapshot(CommandId curcid);
 
 extern void updateSharedLocalSnapshot(struct DtxContextInfo *dtxContextInfo,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to