Skip to content

Commit

Permalink
skip precompile workload if localhost cannot be resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Dec 23, 2024
1 parent d13c17c commit a9d044f
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
using PrecompileTools: @setup_workload, @compile_workload

do_precompile = false
try
@setup_workload begin
# These need to be safe to call here and bake into the pkgimage, i.e. called twice.
Connections.__init__()
MultiPartParsing.__init__()
Parsers.__init__()
Sockets.getalladdrinfo("localhost")
catch ex
@debug "Skipping precompilation workload because localhost cannot be resolved. Check firewall settings" exception=(ex,catch_backtrace())
do_precompile = false
end

# Doesn't seem to be needed here, and might not be safe to call twice (here and during runtime)
# ConnectionRequest.__init__()
if do_precompile
using PrecompileTools: @setup_workload, @compile_workload
try
@setup_workload begin

gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data)))
# These need to be safe to call here and bake into the pkgimage, i.e. called twice.
Connections.__init__()
MultiPartParsing.__init__()
Parsers.__init__()

# random port in the dynamic/private range (49152–65535) which are are
# least likely to be used by well-known services
_port = 57813
# Doesn't seem to be needed here, and might not be safe to call twice (here and during runtime)
# ConnectionRequest.__init__()

cert, key = joinpath.(@__DIR__, "../test", "resources", ("cert.pem", "key.pem"))
sslconfig = MbedTLS.SSLConfig(cert, key)
gzip_data(data::String) = read(GzipCompressorStream(IOBuffer(data)))

server = HTTP.serve!("0.0.0.0", _port; verbose = -1, listenany=true, sslconfig=sslconfig) do req
HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response"))
end
try
# listenany allows changing port if that one is already in use, so check the actual port
_port = HTTP.port(server)
url = "https://localhost:$_port"

env = ["JULIA_NO_VERIFY_HOSTS" => "localhost",
"JULIA_SSL_NO_VERIFY_HOSTS" => nothing,
"JULIA_ALWAYS_VERIFY_HOSTS" => nothing]

withenv(env...) do
@compile_workload begin
HTTP.get(url);
# random port in the dynamic/private range (49152–65535) which are are
# least likely to be used by well-known services
_port = 57813

cert, key = joinpath.(@__DIR__, "../test", "resources", ("cert.pem", "key.pem"))
sslconfig = MbedTLS.SSLConfig(cert, key)

server = HTTP.serve!("0.0.0.0", _port; verbose = -1, listenany=true, sslconfig=sslconfig) do req
HTTP.Response(200, ["Content-Encoding" => "gzip"], gzip_data("dummy response"))
end
try
# listenany allows changing port if that one is already in use, so check the actual port
_port = HTTP.port(server)
url = "https://localhost:$_port"

env = ["JULIA_NO_VERIFY_HOSTS" => "localhost",
"JULIA_SSL_NO_VERIFY_HOSTS" => nothing,
"JULIA_ALWAYS_VERIFY_HOSTS" => nothing]

withenv(env...) do
@compile_workload begin
HTTP.get(url);
end
end
finally
HTTP.forceclose(server)
yield() # needed on 1.9 to avoid some issue where it seems a task doesn't stop before serialization
server = nothing
end
finally
HTTP.forceclose(server)
yield() # needed on 1.9 to avoid some issue where it seems a task doesn't stop before serialization
server = nothing
end
catch e
@info "Ignoring an error that occurred during the precompilation workload" exception=(e, catch_backtrace())
end
catch e
@info "Ignoring an error that occurred during the precompilation workload" exception=(e, catch_backtrace())
end

0 comments on commit a9d044f

Please sign in to comment.