Skip to content

Commit

Permalink
fix cancelling upload requests
Browse files Browse the repository at this point in the history
Cancelling an upload (PUT) request using the mechanism introduced in JuliaLang#256 was not effective. The upload task was not interrupted, which still blocked and the call to `request` did not return.
With this change, cancelling also closes the `input` stream of the request to unblock the upload task.

Also changed the `interrupted` variable to be an `Atomic{Bool}`. Ref discussion [here](JuliaLang#256 (comment)).
  • Loading branch information
tanmaykm committed Sep 4, 2024
1 parent df33406 commit 133db61
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Downloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function request(

# do the request
add_handle(downloader.multi, easy)
interrupted = false
interrupted = Threads.Atomic{Bool}(false)
if interrupt !== nothing
interrupt_task = @async begin
# wait for the interrupt event
Expand All @@ -405,7 +405,8 @@ function request(
remove_handle(downloader.multi, easy)
close(easy.output)
close(easy.progress)
interrupted = true
interrupted[] = true
close(input)
end
else
interrupt_task = nothing
Expand All @@ -425,7 +426,7 @@ function request(
end
end
finally
if !interrupted
if !(interrupted[])
if interrupt_task !== nothing
# trigger interrupt
notify(interrupt)
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ include("setup.jl")
timedwait(()->istaskdone(download_task), 5.0)
@test istaskdone(download_task)
@test download_task.result isa RequestError

url = "$server/put"
input=`sh -c 'sleep 15; echo "hello"'`
download_task = @async request(url; interrupt=interrupt, input=input)
sleep(0.1)
@test !istaskdone(download_task)
notify(interrupt)
timedwait(()->istaskdone(download_task), 5.0)
@test istaskdone(download_task)
@test download_task.result isa RequestError
end

@testset "progress" begin
Expand Down

0 comments on commit 133db61

Please sign in to comment.