Skip to content

Commit 89d3c7d

Browse files
authored
fix cancelling upload requests (#259)
Cancelling an upload (PUT) request using the mechanism introduced in #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](#256 (comment)).
1 parent df33406 commit 89d3c7d

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/Curl/Easy.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,11 @@ function upload_data(easy::Easy, input::IO)
375375
curl_easy_pause(easy.handle, Curl.CURLPAUSE_CONT)
376376
wait(easy.ready)
377377
easy.input === nothing && break
378-
easy.ready = Threads.Event()
378+
if hasmethod(reset, (Base.Event,))
379+
reset(easy.ready)
380+
else
381+
easy.ready = Threads.Event()
382+
end
379383
end
380384
end
381385

src/Downloads.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function request(
396396

397397
# do the request
398398
add_handle(downloader.multi, easy)
399-
interrupted = false
399+
interrupted = Threads.Atomic{Bool}(false)
400400
if interrupt !== nothing
401401
interrupt_task = @async begin
402402
# wait for the interrupt event
@@ -405,7 +405,9 @@ function request(
405405
remove_handle(downloader.multi, easy)
406406
close(easy.output)
407407
close(easy.progress)
408-
interrupted = true
408+
interrupted[] = true
409+
close(input)
410+
notify(easy.ready)
409411
end
410412
else
411413
interrupt_task = nothing
@@ -425,7 +427,7 @@ function request(
425427
end
426428
end
427429
finally
428-
if !interrupted
430+
if !(interrupted[])
429431
if interrupt_task !== nothing
430432
# trigger interrupt
431433
notify(interrupt)

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,17 @@ include("setup.jl")
478478
timedwait(()->istaskdone(download_task), 5.0)
479479
@test istaskdone(download_task)
480480
@test download_task.result isa RequestError
481+
482+
interrupt = Base.Event()
483+
url = "$server/put"
484+
input=`sh -c 'sleep 15; echo "hello"'`
485+
download_task = @async request(url; interrupt=interrupt, input=input)
486+
sleep(0.1)
487+
@test !istaskdone(download_task)
488+
notify(interrupt)
489+
timedwait(()->istaskdone(download_task), 5.0)
490+
@test istaskdone(download_task)
491+
@test download_task.result isa RequestError
481492
end
482493

483494
@testset "progress" begin

0 commit comments

Comments
 (0)