Skip to content

Commit e41e2f3

Browse files
authored
Handle task cancellation (#44)
AsyncStream cancellation should also cancel the streaming byte buffer of `URLSession`
1 parent 791c4c7 commit e41e2f3

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Sources/OllamaKit/Utils/OKHTTPClient.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ internal extension OKHTTPClient {
3333
do {
3434
let (bytes, response) = try await URLSession.shared.bytes(for: request)
3535
try validate(response: response)
36-
36+
37+
continuation.onTermination = { terminationState in
38+
// Cancellation of our task should cancel the URLSessionDataTask
39+
if case .cancelled = terminationState {
40+
bytes.task.cancel()
41+
}
42+
}
43+
3744
var buffer = Data()
3845

3946
for try await byte in bytes {

0 commit comments

Comments
 (0)