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

disallow bugs in @try usage #908

Merged
merged 7 commits into from
Aug 18, 2022
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
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
fail-fast: false
matrix:
version:
- '~1.8.0-0' # 1.8 beta
- '1.6'
- '1' # automatically expands to the latest stable 1.x release of Julia
- 'nightly'
os:
Expand All @@ -30,12 +30,10 @@ jobs:
- windows-latest
arch:
- x64
- x86
exclude:
- os: macOS-latest
arch: x86
include:
- os: windows-latest
version: 'nightly'
version: '1'
arch: x86
steps:
- uses: actions/checkout@v2
- name: setup python
Expand Down
11 changes: 8 additions & 3 deletions src/Exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import ..HTTP # for doc references

@eval begin
"""
@try expr
@try Permitted Error Types expr

Convenience macro for wrapping an expression in a try/catch block
where thrown exceptions are ignored.
"""
macro $(:try)(ex)
macro $(:try)(exes...)
errs = Any[exes...]
ex = pop!(errs)
isempty(errs) && error("no permitted errors")
quote
try $(esc(ex))
catch
catch e
e isa InterruptException && rethrow(e)
|($([:(e isa $(esc(err))) for err in errs]...)) || rethrow(e)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ function bodysummary(body::Form)
end
return "[Message Body was streamed]"
end
bodysummary(body::Union{Dict, NamedTuple}) = URIs.escapeuri(body)

function compactstartline(m::Message)
b = IOBuffer()
Expand Down
21 changes: 6 additions & 15 deletions src/Servers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ function getsslcontext(tcp, sslconfig)
MbedTLS.handshake!(ssl)
return ssl
catch e
return nothing
@try Base.IOError close(tcp)
e isa Base.IOError && return nothing
e isa MbedTLS.MbedException && return nothing
rethrow(e)
end
end

Expand Down Expand Up @@ -145,28 +148,16 @@ closedorclosing(st) = st == CLOSING || st == CLOSED

function requestclose!(c::Connection)
if c.state == IDLE
closewriteandwait(c)
close(c)
c.state = CLOSED
close(c)
else
c.state = CLOSING
end
return
end

function closewriteandwait(c::Connection)
io = IOExtras.tcpsocket(ConnectionPool.getrawstream(c))
@try begin
flush(io)
closewrite(io)
sleep(0.5) # give time for client to receive FIN
end
return
end

function closeconnection(c::Connection)
c.state = CLOSED
closewriteandwait(c)
close(c)
return
end
Expand Down Expand Up @@ -474,7 +465,7 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
c.state = CLOSING
finally
if access_log !== nothing
@try(@info sprint(access_log, http) _group=:access)
@try(Any, @info sprint(access_log, http) _group=:access)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Write the final `0` chunk if needed.
function closebody(http::Stream)
if http.writechunked
http.writechunked = false
@try write(http.stream, "0\r\n\r\n")
@try Base.IOError write(http.stream, "0\r\n\r\n")
end
end

Expand Down Expand Up @@ -374,6 +374,7 @@ end

function IOExtras.closeread(http::Stream{<:Request})
if incomplete(http)
@show http.ntoread, http.readchunked, unknown_length
# Error if Message is not complete...
close(http.stream)
throw(EOFError())
Expand Down
3 changes: 2 additions & 1 deletion src/clientlayers/ConnectionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ function connectionlayer(handler)
catch e
@debugv 1 "❗️ ConnectionLayer $e. Closing: $io"
shouldreuse = false
@try Base.IOError close(io)
e isa HTTPError || throw(RequestError(req, e))
rethrow(e)
finally
if !shouldreuse
@try close(io)
@try Base.IOError close(io)
end
releaseconnection(io, shouldreuse)
end
Expand Down
4 changes: 2 additions & 2 deletions src/clientlayers/StreamRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
catch e
# @error "error" exception=(e, catch_backtrace())
write_error = e
isopen(io) && @try close(io)
isopen(io) && @try Base.IOError close(io)
end
@debugv 2 "client startread"
startread(stream)
Expand All @@ -52,7 +52,7 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
if isaborted(stream)
# The server may have closed the connection.
# Don't propagate such errors.
@try close(io)
@try Base.IOError close(io)
end
end
catch e
Expand Down
40 changes: 19 additions & 21 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ end
@test err.error isa EOFError
finally
# Shutdown
@try close(server)
@try Base.IOError close(server)
HTTP.ConnectionPool.closeall()
end
end
Expand Down Expand Up @@ -299,7 +299,7 @@ end
@test req.status == 200
@test String(req.body) == "hello, world"
finally
@try close(server)
@try Base.IOError close(server)
HTTP.ConnectionPool.closeall()
end
end
Expand Down Expand Up @@ -337,7 +337,7 @@ end
@test peer[2] == 8080
end
finally
@try close(server)
@try Base.IOError close(server)
HTTP.ConnectionPool.closeall()
end

Expand Down Expand Up @@ -394,7 +394,7 @@ end

HTTP.setuseragent!(old_user_agent)
finally
@try close(server)
@try Base.IOError close(server)
HTTP.ConnectionPool.closeall()
end
end
Expand Down Expand Up @@ -435,7 +435,7 @@ import NetworkOptions, MbedTLS
@test HTTP.get(url; require_ssl_verification=false).status == 200
end
finally
@try close(server)
@try Base.IOError close(server)
HTTP.ConnectionPool.closeall()
end
end
Expand Down Expand Up @@ -518,23 +518,22 @@ end
end

@testset "Retry with request/response body streams" begin
server = nothing
try
shouldfail = Ref(true)
server = HTTP.listen!(8080) do http
@assert !eof(http)
msg = String(readavailable(http))
if shouldfail[]
shouldfail[] = false
error("500 unexpected error")
end
HTTP.startwrite(http)
HTTP.write(http, msg)
shouldfail = Ref(true)
server = HTTP.listen!(8080) do http
@assert !eof(http)
msg = String(read(http))
if shouldfail[]
shouldfail[] = false
error("500 unexpected error")
end
HTTP.startwrite(http)
HTTP.write(http, msg)
end
try
req_body = IOBuffer("hey there sailor")
seekstart(req_body)
res_body = IOBuffer()
resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body, verbose=2)
resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body)
@test resp.status == 200
@test String(take!(res_body)) == "hey there sailor"
# ensure if retry=false, that we write the response body immediately
Expand All @@ -545,14 +544,13 @@ end
# when retrying, we can still get access to the most recent failed response body in the response's request context
shouldfail[] = true
seekstart(req_body)
println("making 3rd request")
resp = HTTP.get("http://localhost:8080/retry"; body=req_body, response_stream=res_body)
@test resp.status == 200
@test String(take!(res_body)) == "hey there sailor"
@test String(resp.request.context[:response_body]) == "500 unexpected error"
finally
if server !== nothing
close(server)
end
close(server)
HTTP.ConnectionPool.closeall()
end
end
Expand Down