Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/RESPite/Buffers/CycleBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,14 @@ public void Recycle()
Memory = default;
RunningIndex = 0;
_flags = Flags.None;

// This object becomes available - via the SHARED, STATIC _spare slot below - to any CycleBuffer in the
// process, handed out by Segment.Create as a pristine segment, so StartTrimCount must not survive it.
// Some callers recycle without Untrim first (Release; AppendOrRecycle's search-exhausted path), and Init
// never touches it, so a stale trim reached a "new" segment: Debug.Assert(leasedStart == 0) in Debug,
// and in Release a later Untrim expanding backwards by a trim that never happened, corrupting the
// chain's running indexes. Found under PR #3251's read/parse stress test; absorbed into v4 with the split.
StartTrimCount = 0;
Interlocked.Exchange(ref _spare, this);
DebugAssertValidChain();
}
Expand Down
71 changes: 71 additions & 0 deletions tests/RESPite.Tests/CycleBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,75 @@ public void CanDiscardSafely(Timing timing)

Assert.Equal(0, buffer.GetCommittedLength());
}

/// <summary>
/// A segment recycled while start-trimmed must not carry its trim into the next buffer that is handed it.
/// </summary>
/// <remarks>
/// Recycled segments go to a process-wide spare slot and come back out of <c>Segment.Create</c> as pristine.
/// <c>Recycle</c> did not reset <c>StartTrimCount</c>, and some paths - <see cref="CycleBuffer.Release"/>, and
/// <c>AppendOrRecycle</c>'s search-exhausted path - recycle without <c>Untrim</c> first, so the next buffer's
/// "new" segment wrote at a stale offset and read its committed bytes from the wrong place. Found under #3251's
/// read/parse stress test as <c>Debug.Assert(leasedStart == 0)</c>; absorbed into v4 with the split.
/// </remarks>
[Fact]
public void ARecycledTrimmedSegmentStartsCleanInTheNextBuffer()
{
// the spare slot is process-wide and the suite is parallel, so another buffer can take the segment first;
// repeat the scenario rather than assert on a race nobody controls
for (var attempt = 0; attempt < 50; attempt++)
{
var first = CycleBuffer.Create();
first.Write("0123456789"u8);
first.DiscardCommitted(4); // start-trimmed: StartTrimCount is now 4
first.Release(); // recycled WITHOUT Untrim

var second = CycleBuffer.Create();

second.Write("hello"u8);

// read directly, because in Release - where the Debug.Assert is compiled out - nothing visible fails
// at once: the stale trim only corrupts the chain arithmetic of a later Untrim
Assert.Equal(0, StartTrimCountOfFirstSegment(second));
Assert.True(second.TryGetCommitted(out var committed));
Assert.Equal("hello", System.Text.Encoding.ASCII.GetString(committed.ToArray()));
second.Release();
}
}

/// <summary>
/// The observable consequence of a stale start trim: a discard that empties the buffer while a write lease is
/// outstanding (the outbound copy-out loop draining while the writer holds <c>GetMemory</c>) must still keep the
/// bytes that were written into the lease.
/// </summary>
[Fact]
public void DiscardDuringWriteKeepsLeasedBytesAfterARecycledTrimmedSegment()
{
for (var attempt = 0; attempt < 50; attempt++)
{
var first = CycleBuffer.Create();
first.Write("0123456789"u8);
first.DiscardCommitted(4); // start-trimmed
first.Release(); // recycled WITHOUT Untrim

var second = CycleBuffer.Create();
second.Write("abc"u8);

var lease = second.GetUncommittedMemory();
"XYZ"u8.CopyTo(lease.Span);
second.DiscardCommitted(3); // drains everything committed while the lease is open
second.Commit(3);

Assert.True(second.TryGetCommitted(out var committed));
Assert.Equal("XYZ", System.Text.Encoding.ASCII.GetString(committed.ToArray()));
second.Release();
}
}

private static int StartTrimCountOfFirstSegment(CycleBuffer buffer)
{
const System.Reflection.BindingFlags Any = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
var segment = typeof(CycleBuffer).GetField("startSegment", Any)!.GetValue(buffer)!;
return (int)segment.GetType().GetProperty("StartTrimCount", Any)!.GetValue(segment)!;
}
}
Loading