You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose adding a new function WaitIdle to runtime.
The function will have the following function signature:
funcWaitIdle() <-chanstruct{}
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.
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.
This also demonstrates that it is currently possible to implement this function as a third-party library, albeit a somewhat fragile one — it depends on the exact format of runtime.Stack goroutine dumps.
@bcmills I'm currently using a similar implementation, but created this proposal to see if there is interest in adding first-class support for it to the runtime, seeing that it could be a common use case.
@seankhliao This is different from a deadlock detector, which detects the case where all goroutines are in the waiting state, there are no active timers or netpoll sockets, and really only works when cgo is disabled. This only considers the case where all goroutines are in the waiting state, which can be useful for multiple real-world uses:
For tests, advancing mock time once all timers have fired
For tests, ensuring that there are no spinning / busy-looping background goroutines after ending the test case
Running idle work when the runtime has quiesced and there are spare CPU cycles
This proposal was mostly inspired by the this function in the Rust Tokio library, which implements a feature that I found immensely helpful for writing deterministic unit tests: it waits until the runtime has no work to do, then auto-advances the simulated runtime timer to the the next wakeup time.
Adding WaitIdle as proposed would allow for reliably implementing this feature for mock time implementations. Test code that purely utilizes the mock time implementation without touching time.* functions would be able to run deterministically.
Proposal Details
I propose adding a new function
WaitIdle
toruntime
.The function will have the following function signature:
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.
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.
Implementation of proposal here.
The text was updated successfully, but these errors were encountered: