Skip to content

TurbulenceTest: seq recording race causes 1-off assertion failure after cancellation #554

Description

@Kiryuumaru

Summary

ContinuousStream_SurvivesRapidTopologyChurn in TurbulenceTests.cs can fail with a 1-off assertion error when the test is cancelled while the writer is in its inter-message delay. This is a test-only ordering bug — no product code changes needed.

Root cause

Volatile.Write(ref lastSeqWritten, seq) was placed after Task.Delay(50):

// Bug: delay then record — cancellation during delay loses the last seq
await Task.Delay(50, streamCts.Token);
Volatile.Write(ref lastSeqWritten, seq);

When streamCts.Cancel() fires during the 50ms delay, the WriteAsync for seq already completed successfully but lastSeqWritten was never updated. The assertion:

long totalWritten = Volatile.Read(ref lastSeqWritten) + 1;
Assert.Equal(totalWritten, sorted.Count);

fails by 1 — sorted.Count (which includes the already-written-and-delivered message) exceeds lastSeqWritten + 1.

This bug was masked by #553 (buffer exhaustion at 256 messages) because the test always exited early before cancellation could race. After fixing #553 the 1-off became reproducible.

Fix

Move Volatile.Write before the delay so lastSeqWritten is always ≥ the last written seq:

// Fixed: record immediately after write, then delay
Volatile.Write(ref lastSeqWritten, seq);
await Task.Delay(50, streamCts.Token);

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions