From d5625c7f0a63b7a7db052e7ec24c9e05213f8aff Mon Sep 17 00:00:00 2001 From: Thibault Wittemberg Date: Wed, 20 Apr 2022 11:15:39 +0200 Subject: [PATCH] asyncThrowingChannel: fixes potential crashes when no more awaiting This commit fixes crashes where the state is "awaiting" with no more awaiting clients. The state is now set to .idle to reflect the reality. --- Sources/AsyncAlgorithms/AsyncThrowingChannel.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift b/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift index 575d88e7..205729e3 100644 --- a/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift +++ b/Sources/AsyncAlgorithms/AsyncThrowingChannel.swift @@ -88,7 +88,11 @@ public final class AsyncThrowingChannel: Asyn switch self { case .awaiting(var awaiting): let continuation = awaiting.remove(Awaiting(placeholder: generation))?.continuation - self = .awaiting(awaiting) + if awaiting.isEmpty { + self = .idle + } else { + self = .awaiting(awaiting) + } return continuation case .idle: self = .awaiting([Awaiting(cancelled: generation)]) @@ -143,7 +147,11 @@ public final class AsyncThrowingChannel: Asyn nexts.remove(Awaiting(placeholder: generation)) cancelled = true } - state.emission = .awaiting(nexts) + if nexts.isEmpty { + state.emission = .idle + } else { + state.emission = .awaiting(nexts) + } return nil } }?.resume()