Skip to content

Tests: task-state assertions that assume completion ordering are flaky #3402

Backlog

Description

@mgravell

QueuedResultTests.RetryTransactionFireAndForgetSharesOneCompletedTask (RESP2) failed once on the Ubuntu job of #3399 (run, first attempt), and passed on re-run with no change:

var pending = tran.StringGetAsync(key);
Assert.False(pending.IsCompleted);
Assert.True(await tran.ExecuteAsync());
Assert.True(pending.IsCompleted);   // QueuedResultTests.cs:160 - failed

The retry transaction settles its proxies before ExecuteAsync returns, but they are RunContinuationsAsynchronously, and the task the test holds sits a layer above the proxy, so "complete by the time the other await resumes" is a timing assumption rather than a guarantee. The test is at fault, not the library: it should wait for the task it is asserting on, with a short bound so a genuine hang still fails fast, e.g.

Assert.True(pending.Wait(TimeSpan.FromSeconds(5)));   // or: await pending.WaitAsync(timeout)

TransactionTests already does the right thing (await Task.WhenAll(a, b, ...) before checking IsCompleted).

The sweep

This is one example; the same shape is worth checking across the suite. Roughly 50 assertions inspect task state directly (IsCompleted, IsCompletedSuccessfully, IsFaulted, IsCanceled). They fall into three groups:

  • Synchronous completion is the contract under test: fire-and-forget returning a completed task, empty-input short-circuits (SetPopAsync(count: 0) and friends), cache hits, the blocking context, the fakes in RespSurfaceTests / RespRetryExecutorTests. These must stay as they are; turning them into waits would hide the regression they exist to catch.
  • Already safe: the check follows an await of the same task (PubSubTests, RetryGuardTests, TransactionTests), or a bounded poll (ConfigurationChannelUnitTests).
  • Racy: "it completed because something else did". The check follows an await of a different task, or a fixed delay. QueuedResultTests.cs:160 is the confirmed one; Issues/Issue1101Tests (first.Completion.IsCompleted after unsubscribe + Task.Delay(200)) looks like another candidate. These should wait on the task itself, with a bound.

The mirror image applies too: Assert.False(x.IsCompleted) is only sound when nothing could have completed it yet (no reply has been fed, or the test is asserting it stays incomplete for a bounded wait, as InertClusterNodeUnitTests does).

Activity

  1. mgravell commented on Dec 3, 2026

    @mgravell
    MemberAuthor

    This one reproduces every time locally on net481, against plain v4 (607caaf): RetryTransactionFireAndForgetSharesOneCompletedTask (RESP2), line 160, each run. net10.0 passes. So net481 is a reliable way to check a fix.

  2. mgravell commented on Dec 3, 2026

    @mgravell
    MemberAuthor

    Another one of the same shape, from #3405's Windows job: ClusterTopologyUnitTests.SlotLessNodesAreKnownButNotConnected (also seen once on #3399). The last line is Assert.True(api.IsConnected), straight after await api.PingAsync(). The PING has an answer, but the endpoint's IsConnected is set when the connection is reported established, which can trail the first reply. It should wait on the condition, with a bound, rather than read it once.

    The task-completion example itself is addressed in #3405.

  3. mgravell commented on Dec 3, 2026

    @mgravell
    MemberAuthor

    Another of the same shape, from #3407's Ubuntu job (passed on re-run, and 6/6 locally on both the PR branch and v4): BacklogTests.TotalOutstandingIncludesBacklogQueue, at BacklogTests.cs:437. It asserts Interactive.TotalOutstanding == 0 immediately after await db.PingAsync(). The awaiter sees the reply before the outstanding count is decremented, so it reads 1. It should wait for the counter to settle, with a bound, rather than read it once.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions