Skip to content

Commit

Permalink
Fetcher: avoid response deadlock (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev authored Dec 16, 2024
1 parent e6a30b0 commit e27da23
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Sources/tart/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fileprivate class Delegate: NSObject, URLSessionDataDelegate {
buffer = Data(capacity: Int(capacity))

responseContinuation?.resume(returning: response)
responseContinuation = nil
completionHandler(.allow)
}

Expand All @@ -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
}
}
}

0 comments on commit e27da23

Please sign in to comment.