From e27da23f4c05b98e392c6ca6595433d45d69880a Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Tue, 17 Dec 2024 01:19:06 +0400 Subject: [PATCH] Fetcher: avoid response deadlock (#975) --- Sources/tart/Fetcher.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/tart/Fetcher.swift b/Sources/tart/Fetcher.swift index e56c845..a811104 100644 --- a/Sources/tart/Fetcher.swift +++ b/Sources/tart/Fetcher.swift @@ -55,6 +55,7 @@ fileprivate class Delegate: NSObject, URLSessionDataDelegate { buffer = Data(capacity: Int(capacity)) responseContinuation?.resume(returning: response) + responseContinuation = nil completionHandler(.allow) } @@ -76,15 +77,20 @@ fileprivate class Delegate: NSObject, URLSessionDataDelegate { task: URLSessionTask, didCompleteWithError error: Error? ) { - if !buffer.isEmpty { - streamContinuation?.yield(buffer) - buffer.removeAll(keepingCapacity: true) - } - if let error = error { + responseContinuation?.resume(throwing: error) + responseContinuation = nil + streamContinuation?.finish(throwing: error) + streamContinuation = nil } else { + if !buffer.isEmpty { + streamContinuation?.yield(buffer) + buffer.removeAll(keepingCapacity: true) + } + streamContinuation?.finish() + streamContinuation = nil } } }