Skip to content

Don't retry on internal exceptions #1110

New issue

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

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

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 15, 2023
Merged
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
13 changes: 8 additions & 5 deletions src/clientlayers/RetryRequest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RetryRequest

using Sockets, LoggingExtras, MbedTLS, OpenSSL
using Sockets, LoggingExtras, MbedTLS, OpenSSL, ExceptionUnwrapping
using ..IOExtras, ..Messages, ..Strings, ..ExceptionRequest, ..Exceptions

export retrylayer
Expand Down Expand Up @@ -76,9 +76,10 @@ function retrylayer(handler)
end
end

isrecoverable(ex) = true
isrecoverable(ex::CapturedException) = isrecoverable(ex.ex)
isrecoverable(ex::ConnectError) = isrecoverable(ex.error)
isrecoverable(ex) = is_wrapped_exception(ex) ? isrecoverable(unwrap_exception(ex)) : false
isrecoverable(::Union{Base.EOFError, Base.IOError, MbedTLS.MbedException, OpenSSL.OpenSSLError}) = true
isrecoverable(ex::ArgumentError) = ex.msg == "stream is closed or unusable"
isrecoverable(ex::CompositeException) = all(isrecoverable, ex.exceptions)
# Treat all DNS errors except `EAI_AGAIN`` as non-recoverable
# Ref: https://github.com/JuliaLang/julia/blob/ec8df3da3597d0acd503ff85ac84a5f8f73f625b/stdlib/Sockets/src/addrinfo.jl#L108-L112
isrecoverable(ex::Sockets.DNSError) = (ex.code == Base.UV_EAI_AGAIN)
Expand All @@ -92,9 +93,11 @@ end

function no_retry_reason(ex, req)
buf = IOBuffer()
unwrapped_ex = unwrap_exception(ex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this unwrap_exception if we're doing it via dispatch in isrecoverable; so I think we remove it here and keep it above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but this is just for the no_retry_reson, so maybe we just need this for printing, which is fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly yeah, this is for printing. Thanks!

show(IOContext(buf, :compact => true), req)
print(buf, ", ",
ex isa StatusError ? "HTTP $(ex.status): " :
unwrapped_ex isa StatusError ? "HTTP $(ex.status): " :
!isrecoverable(unwrapped_ex) ? "unrecoverable exception: " :
!isbytes(req.body) ? "request streamed, " : "",
!isbytes(req.response.body) ? "response streamed, " : "",
!isidempotent(req) ? "$(req.method) non-idempotent" : "")
Expand Down
88 changes: 82 additions & 6 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,53 @@ end
end
end

@testset "Don't retry on internal exceptions" begin
kws = (retry_delays = [1, 2, 3], retries=3) # ~ 6 secs
max_wait = 3

function test_finish_within(f, secs)
timedout = Ref(false)
t = Timer((t)->(timedout[] = true), secs)
try
f()
finally
close(t)
end
@test !timedout[]
end

expected = ErrorException("request")
test_finish_within(max_wait) do
@test_throws expected ErrorRequest.get("https://$httpbin/ip"; request_exception=expected, kws...)
end
expected = ArgumentError("request")
test_finish_within(max_wait) do
@test_throws expected ErrorRequest.get("https://$httpbin/ip"; request_exception=expected, kws...)
end

test_finish_within(max_wait) do
expected = ErrorException("stream")
e = try
ErrorRequest.get("https://$httpbin/ip"; stream_exception=expected, kws...)
catch e
e
end
@assert e isa HTTP.RequestError
@test e.error == expected
end

test_finish_within(max_wait) do
expected = ArgumentError("stream")
e = try
ErrorRequest.get("https://$httpbin/ip"; stream_exception=expected, kws...)
catch e
e
end
@assert e isa HTTP.RequestError
@test e.error == expected
end
end

@testset "Retry with ConnectError" begin
mktemp() do path, io
redirect_stdout(io) do
Expand All @@ -668,12 +715,41 @@ end
end

# isrecoverable tests
@test HTTP.RetryRequest.isrecoverable(nothing)
@test HTTP.RetryRequest.isrecoverable(ErrorException(""))
@test HTTP.RetryRequest.isrecoverable(Sockets.DNSError("localhost", Base.UV_EAI_AGAIN))
@test !HTTP.RetryRequest.isrecoverable(Sockets.DNSError("localhost", Base.UV_EAI_NONAME))
@test HTTP.RetryRequest.isrecoverable(HTTP.Exceptions.ConnectError("http://localhost", Sockets.DNSError("localhost", Base.UV_EAI_AGAIN)))
@test !HTTP.RetryRequest.isrecoverable(HTTP.Exceptions.ConnectError("http://localhost", Sockets.DNSError("localhost", Base.UV_EAI_NONAME)))
@test !HTTP.RetryRequest.isrecoverable(nothing)

@test !HTTP.RetryRequest.isrecoverable(ErrorException(""))
@test !HTTP.RetryRequest.isrecoverable(ArgumentError("yikes"))
@test HTTP.RetryRequest.isrecoverable(ArgumentError("stream is closed or unusable"))

@test HTTP.RetryRequest.isrecoverable(HTTP.RequestError(nothing, ArgumentError("stream is closed or unusable")))
@test !HTTP.RetryRequest.isrecoverable(HTTP.RequestError(nothing, ArgumentError("yikes")))

@test HTTP.RetryRequest.isrecoverable(CapturedException(ArgumentError("stream is closed or unusable"), Any[]))

recoverable_dns_error = Sockets.DNSError("localhost", Base.UV_EAI_AGAIN)
unrecoverable_dns_error = Sockets.DNSError("localhost", Base.UV_EAI_NONAME)
@test HTTP.RetryRequest.isrecoverable(recoverable_dns_error)
@test !HTTP.RetryRequest.isrecoverable(unrecoverable_dns_error)
@test HTTP.RetryRequest.isrecoverable(HTTP.Exceptions.ConnectError("http://localhost", recoverable_dns_error))
@test !HTTP.RetryRequest.isrecoverable(HTTP.Exceptions.ConnectError("http://localhost", unrecoverable_dns_error))
@test HTTP.RetryRequest.isrecoverable(CompositeException([
recoverable_dns_error,
HTTP.Exceptions.ConnectError("http://localhost", recoverable_dns_error),
]))
@test HTTP.RetryRequest.isrecoverable(CompositeException([
recoverable_dns_error,
HTTP.Exceptions.ConnectError("http://localhost", recoverable_dns_error),
CompositeException([recoverable_dns_error])
]))
@test !HTTP.RetryRequest.isrecoverable(CompositeException([
recoverable_dns_error,
HTTP.Exceptions.ConnectError("http://localhost", unrecoverable_dns_error),
]))
@test !HTTP.RetryRequest.isrecoverable(CompositeException([
recoverable_dns_error,
HTTP.Exceptions.ConnectError("http://localhost", recoverable_dns_error),
CompositeException([unrecoverable_dns_error])
]))
end

findnewline(bytes) = something(findfirst(==(UInt8('\n')), bytes), 0)
Expand Down
22 changes: 22 additions & 0 deletions test/resources/TestRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,25 @@ end
HTTP.@client (first=[testouterrequestlayer], last=[testinnerrequestlayer]) (first=[testouterstreamlayer], last=[testinnerstreamlayer])

end

module ErrorRequest

using HTTP

function throwingrequestlayer(handler)
return function(req; request_exception=nothing, kw...)
!isnothing(request_exception) && throw(request_exception)
return handler(req; kw...)
end
end

function throwingstreamlayer(handler)
return function(stream; stream_exception=nothing, kw...)
!isnothing(stream_exception) && throw(stream_exception)
return handler(stream; kw...)
end
end

HTTP.@client (throwingrequestlayer,) (throwingstreamlayer,)

end