Skip to content

Fix shared snapshot slot ID locking - #2256

Open
protectione055 wants to merge 1 commit into
apache:mainfrom
protectione055:fix/shared-snapshot-slotid-lock
Open

protectione055 wants to merge 1 commit into
apache:mainfrom
protectione055:fix/shared-snapshot-slotid-lock

Conversation

@protectione055

Copy link
Copy Markdown

What does this PR do?

GpDropTempTables() updates slotid under the per-slot lock, while
SharedSnapshotAdd() and SharedSnapshotLookup() scan slot IDs under
SharedSnapshotLock. These accesses use different locks and can run
concurrently.

Acquire SharedSnapshotLock in exclusive mode before the existing
per-slot lock when updating slotid during a session reset. This
serializes the update with directory scans while preserving protection
for per-slot readers. Release the locks in reverse order.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:
Adds an exclusive directory-lock acquisition when a session reset
updates its shared snapshot slot ID.

Checklist

@yjhjstz yjhjstz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. GpDropTempTables() was updating slotid under only the per-slot slotLock, while SharedSnapshotAdd() and SharedSnapshotLookup() scan slotid under SharedSnapshotLock. Since the two sides used different locks, a scan could race with the update. Taking SharedSnapshotLock first, then slotLock, fixes that.

I checked sharedsnapshot.c and found no path that holds slotLock and then waits on SharedSnapshotLock (the error path in getSharedSnapshot releases slotLock first), so the lock order is consistent and shouldn't deadlock. The extra exclusive lock only applies on session reset, which is rare.

Two optional suggestions:

  1. Add a comment near the slotLock field noting that the directory lock must be taken first.
  2. Mention how the race was found (reproduction or code reading).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @protectione055 welcome!🎊 Thanks for taking the effort to make our project better! 🙌 Keep making such awesome contributions!

@yjhjstz
yjhjstz enabled auto-merge (rebase) November 5, 2026 21:11
GpDropTempTables() updates slotid under the per-slot lock, while
SharedSnapshotAdd() and SharedSnapshotLookup() scan slot IDs under
SharedSnapshotLock. These locks do not serialize the update with
concurrent directory scans.

Acquire SharedSnapshotLock exclusively before the existing per-slot
lock when updating slotid during a session reset. Retain the per-slot
lock for slot-local readers and release the locks in reverse order.

Assisted-by: ChatGPT
auto-merge was automatically disabled November 6, 2026 01:48

Head branch was pushed to by a user without write access

@protectione055
protectione055 force-pushed the fix/shared-snapshot-slotid-lock branch from 6ae3a23 to 79aca01 Compare November 6, 2026 01:48
@protectione055

protectione055 commented Nov 6, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review and suggestions.

Two optional suggestions:

  1. Add a comment near the slotLock field noting that the directory lock must be taken first.

I’ve added a comment next to slotLock documenting that SharedSnapshotLock should be acquired first when both locks are needed.

  1. Mention how the race was found (reproduction or code reading).

I found the locking mismatch while reading the code and haven’t constructed a test case that reproduces a failure. In theory, if a concurrent scan observed a partial slotid value, it could mistake the slot for another session’s slot or an unused slot. I haven’t verified that such a read can occur on a supported platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants