Skip to content

TestKit: Replace Thread.Sleep with SpinWait to reduce startup failures under load #7744

@Aaronontheweb

Description

@Aaronontheweb

Problem

TestKit's WaitUntilTestActorIsReady uses Thread.Sleep(10) in a tight loop, which:

  • Always waits minimum 10ms even if TestActor is ready in microseconds
  • Blocks thread pool threads unnecessarily
  • Exacerbates thread starvation under high CPU load (e.g., CI with 2 CPUs)
  • Causes "Timeout waiting for test actor to be ready" failures

Root Cause

The TestActor (a simple system actor) typically starts in microseconds, but Thread.Sleep(10) forces a minimum 10ms wait. Under resource pressure, this blocking wait makes thread pool starvation worse, leading to timeouts.

Proposed Solution

Replace Thread.Sleep(10) with SpinWait which:

  • Spins for ~10-20 microseconds (detects ready TestActor almost instantly)
  • Then yields to other threads progressively
  • Eventually sleeps if needed
  • Adapts to system load automatically

Benefits

  • Faster test startup (microseconds vs 10ms minimum)
  • Better behavior under load (proper yielding)
  • No breaking changes
  • Reduces CI timeout failures

Related to #7259, #7423

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions