closeAndAwait

abstract suspend fun closeAndAwait(timeoutMillis: Long): CloseResult

Stops accepting work, waits up to timeoutMillis for work already accepted to be handed to the transport, and reports what happened. See CloseResult for exactly what each part of the result does and does not promise.

Unlike Tracker.close, every call waits on the same shutdown: the first call starts it, concurrent calls wait on it, and a call made after it finished returns its final result at once. Tracker.close and this function share that one shutdown too, so a close() already in progress on another thread is waited for, not repeated.

timeoutMillis bounds this caller's wait, not the shutdown: a caller that times out (or is cancelled) leaves the shutdown running, and a later call reports its outcome. The drain is bounded by AutographConfig.closeDrainTimeoutMillis, but the Transport.flush that follows it is not, and a synchronous flush cannot be interrupted (#305). So an explicit timeoutMillis at or above the drain bound still does not guarantee the final result: the flush takes time after the drain, and a timer that fires meanwhile returns a snapshot with CloseResult.waitCompleted false. A caller that wants the final result calls the no-argument closeAndAwait, which is closeAndAwait(Long.MAX_VALUE): that value waits for the shutdown itself and has no timer to race it. The timer runs on real time even when called from a virtual-time test dispatcher.

A timeoutMillis of zero or less does not wait: it starts (or joins) the shutdown and reports its state at once. It is not an error, so that a bad value can never become a crash on a path that runs while an app is shutting down.