Fix LockSublogs mutual exclusion and GC hole in AofAddress.Span#1774
Merged
Conversation
The previous implementation unconditionally ORed bits into the lockMap without checking if any requested bits were already held. This allowed concurrent transactions to interleave their TxnCommit markers across physical sublogs in different orders, potentially causing barrier-level deadlocks during multi-log replay. The fix spins until none of the requested sublog bits are held before atomically setting them via CAS, serializing overlapping multi-sublog enqueues. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace fixed/pointer-based Span construction with managed-ref approach using MemoryMarshal.CreateSpan + Unsafe.As. The previous implementation returned a Span whose underlying pointer escaped the fixed block scope, allowing the GC to relocate the struct and leave the Span dangling. The new approach returns a Span backed by a tracked managed reference, eliminating the pin requirement and ensuring GC safety. Added [UnscopedRef] to allow the ref to escape the property. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR targets correctness and safety in the AOF multi-log (sharded sublog) infrastructure by tightening sublog mutual exclusion and fixing unsafe span creation over the AOF address vector.
Changes:
- Enforces mutual exclusion in
ShardedLog.LockSublogs()by waiting for overlapping sublog-lock bits to clear before atomically acquiring them. - Reworks
AofAddress.Spanto avoid returning aSpan<byte>backed by a pointer that escapes afixedscope, switching toMemoryMarshal.CreateSpan+Unsafe.As.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| libs/server/AOF/ShardedLog.cs | Fixes lock-bit acquisition logic to prevent overlapping sublog acquisitions from interleaving. |
| libs/server/AOF/AofAddress.cs | Changes Span implementation to avoid a dangling span from an escaped fixed pointer. |
…f] from AofAddress.Span - Replace Thread.Yield() with SpinWait for progressive backoff under contention - Add SpinOnce() on CAS failure path (previously only yielded on overlap check) - Remove [UnscopedRef] from Span property: all callers use it in safe scopes, so the compiler's ref-escape analysis can enforce lifetime safety at compile time - Remove unused System.Diagnostics.CodeAnalysis import Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
TedHartMS
reviewed
May 6, 2026
Restructure the loop so CAS is only attempted when no requested bits are held, and both failure paths (bits already held or CAS race) fall through to a single SpinOnce() call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
TedHartMS
approved these changes
May 7, 2026
chenhao-ye
pushed a commit
to chenhao-ye/garnet
that referenced
this pull request
May 13, 2026
…osoft#1774) * Fix LockSublogs to enforce mutual exclusion for multi-sublog enqueue The previous implementation unconditionally ORed bits into the lockMap without checking if any requested bits were already held. This allowed concurrent transactions to interleave their TxnCommit markers across physical sublogs in different orders, potentially causing barrier-level deadlocks during multi-log replay. The fix spins until none of the requested sublog bits are held before atomically setting them via CAS, serializing overlapping multi-sublog enqueues. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix GC hole in AofAddress.Span property Replace fixed/pointer-based Span construction with managed-ref approach using MemoryMarshal.CreateSpan + Unsafe.As. The previous implementation returned a Span whose underlying pointer escaped the fixed block scope, allowing the GC to relocate the struct and leave the Span dangling. The new approach returns a Span backed by a tracked managed reference, eliminating the pin requirement and ensuring GC safety. Added [UnscopedRef] to allow the ref to escape the property. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address PR review: use SpinWait in LockSublogs and remove [UnscopedRef] from AofAddress.Span - Replace Thread.Yield() with SpinWait for progressive backoff under contention - Add SpinOnce() on CAS failure path (previously only yielded on overlap check) - Remove [UnscopedRef] from Span property: all callers use it in safe scopes, so the compiler's ref-escape analysis can enforce lifetime safety at compile time - Remove unused System.Diagnostics.CodeAnalysis import Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Simplify LockSublogs: single SpinOnce for both failure paths Restructure the loop so CAS is only attempted when no requested bits are held, and both failure paths (bits already held or CAS race) fall through to a single SpinOnce() call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
chenhao-ye
pushed a commit
to chenhao-ye/garnet
that referenced
this pull request
May 13, 2026
…osoft#1774) * Fix LockSublogs to enforce mutual exclusion for multi-sublog enqueue The previous implementation unconditionally ORed bits into the lockMap without checking if any requested bits were already held. This allowed concurrent transactions to interleave their TxnCommit markers across physical sublogs in different orders, potentially causing barrier-level deadlocks during multi-log replay. The fix spins until none of the requested sublog bits are held before atomically setting them via CAS, serializing overlapping multi-sublog enqueues. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix GC hole in AofAddress.Span property Replace fixed/pointer-based Span construction with managed-ref approach using MemoryMarshal.CreateSpan + Unsafe.As. The previous implementation returned a Span whose underlying pointer escaped the fixed block scope, allowing the GC to relocate the struct and leave the Span dangling. The new approach returns a Span backed by a tracked managed reference, eliminating the pin requirement and ensuring GC safety. Added [UnscopedRef] to allow the ref to escape the property. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address PR review: use SpinWait in LockSublogs and remove [UnscopedRef] from AofAddress.Span - Replace Thread.Yield() with SpinWait for progressive backoff under contention - Add SpinOnce() on CAS failure path (previously only yielded on overlap check) - Remove [UnscopedRef] from Span property: all callers use it in safe scopes, so the compiler's ref-escape analysis can enforce lifetime safety at compile time - Remove unused System.Diagnostics.CodeAnalysis import Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Simplify LockSublogs: single SpinOnce for both failure paths Restructure the loop so CAS is only attempted when no requested bits are held, and both failure paths (bits already held or CAS race) fall through to a single SpinOnce() call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two targeted fixes for the AOF multi-log replay infrastructure:
1. Fix LockSublogs mutual exclusion
ShardedLog.LockSublogs()was not enforcing mutual exclusion - it ORed bits without checking if they were already held. This could allow concurrent multi-sublog enqueues sharing a physical sublog to reorder TxnCommit markers, causing barrier-level deadlocks during replay.Fix: Spin-wait with CAS until none of the requested bits are set, then atomically set them. This serializes multi-sublog enqueues that share any physical sublog.
2. Fix GC hole in AofAddress.Span property
The previous implementation used a
fixedpointer that escaped scope, creating a danglingSpan. The new implementation usesMemoryMarshal.CreateSpanwithUnsafe.Asand[UnscopedRef]to return a GC-tracked managed reference that remains valid as long as the struct is alive.Testing