Skip to content

proposal: runtime: add WaitIdle function #65336

@gh2o

Description

@gh2o

Proposal Details

I propose adding a new function WaitIdle to runtime.

The function will have the following function signature:

func WaitIdle() <-chan struct{}

WaitIdle will return a channel that will be closed at the next moment that no goroutines are runnable, i.e. all P's are idle. This channel would be woken up when pidleput() is called for the last active P.

The main use case of the WaitIdle function is for tests, where a user or library may need to wait for all downstream effects of a given wakeup to complete before proceeding with the test case.

For example, a hypothetical mock time implementation may be used by hypothetical user code in a test harness as follows:

ticker := mocktime.Ticker(time.Second)
for {
  <-ticker
  expensiveComputationHere()
  t := mocktime.Now()
  fmt.Println(t)
}

In order for the call to mocktime.Now() to be deterministic, the implementation must wait until the effects of waking up the ticker channel have completed, e.g. waiting until the goroutine with the user code is once again waiting on ticker or some other channel. Some mock time implementations use a stand-in method such as time.Sleep(1 * time.Millisecond) to wait for user goroutines to settle; however, this is unreliable for CPU-heavy goroutines, and slow for goroutines that execute minimal code upon wakeup. As long as the user code does not call the real time.* functions or other I/O functions, using WaitIdle() here would provide both determinism and a performance benefit.

Implementation of proposal here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions