public suspend fun withTimeout()

in ktor-http/ktor-http-cio/jvm/src/io/ktor/http/cio/internals/WeakTimeoutQueue.kt [76:102]


    public suspend fun <T> withTimeout(block: suspend CoroutineScope.() -> T): T {
        return suspendCoroutineUninterceptedOrReturn { rawContinuation ->
            val continuation = rawContinuation.intercepted()

            val wrapped =
                WeakTimeoutCoroutine(continuation.context, continuation)
            val handle = register(wrapped)
            wrapped.invokeOnCompletion(handle)

            val result = try {
                if (wrapped.isCancelled) COROUTINE_SUSPENDED
                else block.startCoroutineUninterceptedOrReturn(receiver = wrapped, completion = wrapped)
            } catch (t: Throwable) {
                if (wrapped.tryComplete()) {
                    handle.dispose()
                    throw t
                } else COROUTINE_SUSPENDED
            }

            if (result !== COROUTINE_SUSPENDED) {
                if (wrapped.tryComplete()) {
                    handle.dispose()
                    result
                } else COROUTINE_SUSPENDED
            } else COROUTINE_SUSPENDED
        }
    }