Server cannot shutdown on python3.12 #2122
Replies: 9 comments 7 replies
-
This behavior appears to be because of this change in Python: python/cpython#98582 It looks to me like @masenf's suggested change is exactly the correct fix, as This is a big problem for applications and frameworks that build on Uvicorn and use WebSockets--it puts Python in a state where it appears hung and hitting Ctrl+C does nothing. |
Beta Was this translation helpful? Give feedback.
-
Or perhaps the |
Beta Was this translation helpful? Give feedback.
-
I cannot reproduce it. |
Beta Was this translation helpful? Give feedback.
-
I tried the following and was not able to repro on linux/docker. Although in my bigger, more complicated project that is using websockets (where i first noticed the issue), the problem still arises, even on linux. docker run -it -p8000:8000 python:3.12 bash
cat - <<EOF > app.py
async def app(scope, receive, send):
assert scope["type"] == "http"
await send({"type": "http.response.start", "status": 204, "headers": []})
await send({"type": "http.response.body", "body": b"", "more_body": False})
EOF
pip install uvicorn==0.23.2
uvicorn app:app & sleep 1 && curl http://localhost:8000; fg (Then CTRL-C to stop uvicorn.) I was able to reproduce on Windows 11 and macOS 13.6 with the steps in my original message. However macOS 14 is not reproducing the issue consistently for me (upgraded from 13.5.4 last week). Not sure if this is relevant, but data points. |
Beta Was this translation helpful? Give feedback.
-
It looks like the pipeline fails on Windows for 3.12: #2129 |
Beta Was this translation helpful? Give feedback.
-
Sorry, I should've clarified. It only reproduces when a connection is left open. So the docker snippet @masenf posted won't do it because Try this instead:
Then Ctrl+C. Our framework is heavily WebSocket based, so there's essentially always a connection open, especially when developing locally. |
Beta Was this translation helpful? Give feedback.
-
This is a bug on CPython. Discussion can be found here: python/cpython#79033 |
Beta Was this translation helpful? Give feedback.
-
@Kludex would you be open to a PR? I'm MORE than happy to create one. |
Beta Was this translation helpful? Give feedback.
-
Resolved with the release of uvicorn-0.24.0; Thanks to @jcheng5 @Kludex and @zanieb for making it happen 🙌 |
Beta Was this translation helpful? Give feedback.
-
Not sure if uvicorn support for python 3.12.0 is expected yet, but my testing did not get far.
Using python.org 3.12 compiled for apple silicon, I have not tested other platforms yet.
app.py
☝️ and it just kind of sits there. It exits as expected on python3.11. (If the server does not receive any requests, then it also exits without hanging)
I ran a debugger in the shutdown function and it appears to be hanging at
uvicorn/uvicorn/server.py
Lines 271 to 272 in 40b99b8
server._active_count
is non-zero and does not seem to be making progress toward shutting down.I try with the following patch and it seems to resolve the issue:
However I'm not sure if this change would have broader implications for uvicorn or libraries that interact with it, but the tests do seem to pass with py3.11 and py3.12 after this change (except
test_sigint_abort_req
, which also failed before the change). I did also have to qualify theuvloop
dep since they do not support 3.12 yet.This issue might be similar to #1803, asked last year, but i think that was more gunicorn-specific.
Beta Was this translation helpful? Give feedback.
All reactions