Closed
Description
Update
Reproducible test case:
https://github.com/gregcotten/ahc-download-cancel-bug
Just run swift test
It turns out that a redirect causes a new task to be created, so you can't count on execute(...)'s Task to be the only Task you'll need to cancel. I'll put a workaround on the above repo at some point.
Original
Stubbing this for now with hope to provide a reproducible test case when I have time...
I'm hoping to use structured concurrency Task cancellation to actually cancel a download, so I did something like this:
let download = Task {
let delegate = try FileDownloadDelegate(path: tempDownloadPath)
let downloadTask = HTTPClient.shared.execute(request: request, delegate: delegate, reportProgress: {
print("progress: \($0.receivedBytes)")
})
_ = try await withTaskCancellationHandler {
try await downloadTask.get()
} onCancel: {
print("cancelling download...")
downloadTask.cancel()
print("cancelled download")
}
}
await Task.sleep(for: .seconds(5))
download.cancel()
try await download.value
The actual download progresses until it's done downloading the file, no matter what, and then downloadTask.get()
returns without throwing an error.
What am I doing wrong here?
Metadata
Metadata
Assignees
Labels
No labels