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

serve() fails when run from an :interactive thread (Julia >=1.9) #970

Closed
robsmith11 opened this issue Nov 25, 2022 · 2 comments · Fixed by #972
Closed

serve() fails when run from an :interactive thread (Julia >=1.9) #970

robsmith11 opened this issue Nov 25, 2022 · 2 comments · Fixed by #972

Comments

@robsmith11
Copy link

robsmith11 commented Nov 25, 2022

The minimal example fails to return a valid response (but doesn't throw any errors) when run from an :interactive thread, but works fine if :interactive is removed. Julia should be started with -t2,1.

import HTTP

function run()
  HTTP.serve() do request::HTTP.Request
    return HTTP.Response(200, "hello")
  end
end

t = Threads.@spawn :interactive run()
wait(t)
$ curl -v 127.0.0.1:8081
*   Trying 127.0.0.1:8081...
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.85.0
> Accept: */*
>
* Empty reply from server
* Closing connection 0
curl: (52) Empty reply from server
(foo) pkg> st
Status `/tmp/foo/Project.toml`
  [cd3eb016] HTTP v1.5.5

julia> versioninfo()
Julia Version 1.10.0-DEV.46
Commit d0a211a9209 (2022-11-24 15:42 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 7 4700U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, znver2)
  Threads: 3 on 8 virtual cores
Environment:
  JULIA_DEPOT_PATH = /me/.julia

I also tested and got the same results with 1.9.0-alpha1.

@robsmith11 robsmith11 changed the title serve() fails when run from an :interactive thread (Julia >1.9) serve() fails when run from an :interactive thread (Julia >=1.9) Nov 25, 2022
@fonsp
Copy link
Member

fonsp commented Nov 29, 2022

(This issue is about the new :interactive thread feature in Julia. See the PR and new docs.)

quinnj added a commit that referenced this issue Nov 30, 2022
Fixes #970.

The issue here is that our `access_threaded` function, and thread-specific
arrays, all relied on `Threads.nthreads()`, which by default only returns
the number of threads in the `:default` threadpool. In the new world where
the `:interactive` threadpool exists, there can also be threads there.

There is a new function `Threads.maxthreadid()` which helpfully tells us
the max id of any thread, regardless of threadpool. So the proposed fix
here is that if that function is defined, use that, otherwise fallback
to `Threads.nthreads()`.
@quinnj
Copy link
Member

quinnj commented Nov 30, 2022

Thanks for the report; this is somewhat due to my own naivety with how the new threadpools work in Julia, so it was a good learning experience for me. It's actually not really related to the server code really, but due to our utilities we have around global objects that we try to make threadsafe via resize!ing them during __init__ and then using the current Threads.threadid() to access the appropriate element. The problem is we're sizing those arrays with only Threads.nthreads(), which isn't accurate in the the threadpool world. Luckily, there's a new Threads.maxthreadid() function that is more appropriate for how we're using these global arrays. Fix is up: #972

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

Successfully merging a pull request may close this issue.

3 participants