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

Issue with interactive and default threadpools in Julia 1.9 #1040

Closed
simsurace opened this issue May 2, 2023 · 2 comments
Closed

Issue with interactive and default threadpools in Julia 1.9 #1040

simsurace opened this issue May 2, 2023 · 2 comments

Comments

@simsurace
Copy link

I want to keep low latency tasks on the interactive thread pool and spawn heavier tasks on the default threadpool. However, this still doesn't seem to work with the most recent release candidate:

The following is a MWE server script. Run with julia +beta -t1,1

using Dates, HTTP

const ROUTER = HTTP.Router()

function low_latency(req)
    @info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid())"
    return "Done with low latency task"
end
HTTP.register!(ROUTER, "GET", "/low", low_latency)

function high_latency(req)
    Threads.@spawn :default begin
        s = sum(randn(10^8))
        @info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid()), sum = $s"
        return "Done with high latency task"
    end
end
HTTP.register!(ROUTER, "GET", "/high", high_latency)

function requestHandler(req)
    resp = ROUTER(req)
    return HTTP.Response(200, resp)
end

HTTP.serve(requestHandler, "0.0.0.0", 7777)

Calling the /low route works fine, but on calling the /high route, I get:

[ Info: 2023-05-02T16:25:18.769> default tid: 2, sum = 3245.4070974669858
┌ Error: handle_connection handler error
│   exception =
│    MethodError: no method matching length(::Task)
│    
│    Closest candidates are:
│      length(::Union{Base.KeySet, Base.ValueIterator})
│       @ Base abstractdict.jl:58
│      length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S})
│       @ LinearAlgebra ~/.julia/juliaup/julia-1.9.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:295
│      length(::Union{SparseArrays.FixedSparseVector{Tv, Ti}, SparseArrays.SparseVector{Tv, Ti}} where {Tv, Ti})
│       @ SparseArrays ~/.julia/juliaup/julia-1.9.0-rc3+0.x64.linux.gnu/share/julia/stdlib/v1.9/SparseArrays/src/sparsevector.jl:95
│      ...
│    
│    Stacktrace:
│      [1] mkheaders(h::Task)
│        @ HTTP.Messages ~/.julia/packages/HTTP/qGpdS/src/Messages.jl:209
│      [2] HTTP.Messages.Response(status::Int64, headers::Task, body::Vector{UInt8}; version::HTTP.Strings.HTTPVersion, request::Nothing)
│        @ HTTP.Messages ~/.julia/packages/HTTP/qGpdS/src/Messages.jl:108
│      [3] HTTP.Messages.Response(status::Int64, headers::Task; body::Vector{UInt8}, request::Nothing)
│        @ HTTP.Messages ~/.julia/packages/HTTP/qGpdS/src/Messages.jl:112
│      [4] HTTP.Messages.Response(status::Int64, headers::Task)
│        @ HTTP.Messages ~/.julia/packages/HTTP/qGpdS/src/Messages.jl:112
│      [5] requestHandler(req::HTTP.Messages.Request)
│        @ Main ~/Documents/projects/n/julia-ai-service/test.jl:22
│      [6] (::HTTP.Handlers.var"#1#2"{typeof(requestHandler)})(stream::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.Connections.Connection{Sockets.TCPSocket}})
│        @ HTTP.Handlers ~/.julia/packages/HTTP/qGpdS/src/Handlers.jl:58
│      [7] #invokelatest#2
│        @ ./essentials.jl:816 [inlined]
│      [8] invokelatest
│        @ ./essentials.jl:813 [inlined]
│      [9] handle_connection(f::Function, c::HTTP.Connections.Connection{Sockets.TCPSocket}, listener::HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, readtimeout::Int64, access_log::Nothing)
│        @ HTTP.Servers ~/.julia/packages/HTTP/qGpdS/src/Servers.jl:447
│     [10] macro expansion
│        @ ~/.julia/packages/HTTP/qGpdS/src/Servers.jl:385 [inlined]
│     [11] (::HTTP.Servers.var"#16#17"{HTTP.Handlers.var"#1#2"{typeof(requestHandler)}, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, Set{HTTP.Connections.Connection}, Int64, Nothing, Base.Semaphore, HTTP.Connections.Connection{Sockets.TCPSocket}})()
│        @ HTTP.Servers ./task.jl:514
└ @ HTTP.Servers ~/.julia/packages/HTTP/qGpdS/src/Servers.jl:461

Is this user error or a bug?

Versions

  • Julia 1.9.0-rc3
  • HTTP.jl 1.8.0
  • MbedTLS.jl 1.1.7
@quinnj
Copy link
Member

quinnj commented May 2, 2023

Request handlers must be of the form f(::Request) -> Response), and you're violating that for the high latency; instead you want:

function high_latency(req)
    fetch(Threads.@spawn :default begin
        s = sum(randn(10^8))
        @info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid()), sum = $s"
        return "Done with high latency task"
    end)
end

@simsurace
Copy link
Author

Oh my bad, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants