Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle FDWatcher exception on closed sockets #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Curl/Multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@

function add_handle(multi::Multi, easy::Easy)
connect_semaphore_acquire(easy)
lock(multi.lock) do
added = lock(multi.lock) do
if isempty(multi.easies)
preserve_handle(multi)
end
push!(multi.easies, easy)
init!(multi)
@check curl_multi_add_handle(multi.handle, easy.handle)
end
if added != 0
connect_semaphore_release(easy)

Check warning on line 55 in src/Curl/Multi.jl

View check run for this annotation

Codecov / codecov/patch

src/Curl/Multi.jl#L55

Added line #L55 was not covered by tests
end
return added
end

const MULTIS_LOCK = Base.ReentrantLock()
Expand Down Expand Up @@ -170,7 +174,25 @@
if action in (CURL_POLL_IN, CURL_POLL_OUT, CURL_POLL_INOUT)
readable = action in (CURL_POLL_IN, CURL_POLL_INOUT)
writable = action in (CURL_POLL_OUT, CURL_POLL_INOUT)
watcher = FDWatcher(OS_HANDLE(sock), readable, writable)
watcher = try
FDWatcher(OS_HANDLE(sock), readable, writable)
catch watcher_ex
if watcher_ex isa Base.IOError
task = @async begin

Check warning on line 181 in src/Curl/Multi.jl

View check run for this annotation

Codecov / codecov/patch

src/Curl/Multi.jl#L180-L181

Added lines #L180 - L181 were not covered by tests
lock(multi.lock) do
@check curl_multi_socket_action(multi.handle, sock, CURL_CSELECT_ERR)
check_multi_info(multi)
end
end
@isdefined(errormonitor) && errormonitor(task)
nothing

Check warning on line 188 in src/Curl/Multi.jl

View check run for this annotation

Codecov / codecov/patch

src/Curl/Multi.jl#L187-L188

Added lines #L187 - L188 were not covered by tests
else
rethrow()
end
end
if watcher === nothing
return -1

Check warning on line 194 in src/Curl/Multi.jl

View check run for this annotation

Codecov / codecov/patch

src/Curl/Multi.jl#L194

Added line #L194 was not covered by tests
end
preserve_handle(watcher)
watcher_p = pointer_from_objref(watcher)
@check curl_multi_assign(multi.handle, sock, watcher_p)
Expand Down
11 changes: 10 additions & 1 deletion src/Downloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,16 @@
easy_hook(downloader, easy, info)

# do the request
add_handle(downloader.multi, easy)
add_handle_error = add_handle(downloader.multi, easy)
if add_handle_error != 0
no_response = Response(nothing, "", 0, "", [])
request_error = RequestError(url, add_handle_error, "", no_response)
if throw
Base.throw(request_error)

Check warning on line 403 in src/Downloads.jl

View check run for this annotation

Codecov / codecov/patch

src/Downloads.jl#L400-L403

Added lines #L400 - L403 were not covered by tests
else
return request_error

Check warning on line 405 in src/Downloads.jl

View check run for this annotation

Codecov / codecov/patch

src/Downloads.jl#L405

Added line #L405 was not covered by tests
end
end
interrupted = Threads.Atomic{Bool}(false)
if interrupt !== nothing
interrupt_task = @async begin
Expand Down
Loading