Server.serve_forever() isn't cancelled when Server.close() is called
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- backend, networking
Research direction
Start by comparing uvloop's server behavior with the CPython implementation linked in Lib/asyncio/base_events.py around lines 341–344, focusing on Server.serve_forever() and Server.close(). Reproduce the issue with the provided SIGTERM example and confirm that closing the server causes serve_forever() to raise asyncio.CancelledError and print “Shutting down” before exiting.
Written by the indexing model from the issue text.
Description
- uvloop version: 0.19.0
- Python version: 3.10.12 (3.10.12-1~22.04.3)
- Platform: Ubuntu 22.04.4
- Can you reproduce the bug with
PYTHONASYNCIODEBUGin env?: Yes - Does uvloop behave differently from vanilla asyncio? How?: Yes, described below.
With stock asyncio, after awaiting on Server.serve_forever(), calling Server.close() causes the await statement to throw CancelledError. With uvloop's implementation, the listening socket is correctly closed, however the await statement blocks indefinitely.
Example:
async def run_server():
async with await asyncio.start_server(lambda r,w: w.close(), '127.0.0.1', 8080) as server:
server.get_loop().add_signal_handler(15, lambda: server.close())
try:
await server.serve_forever()
except asyncio.CancelledError:
print('Shutting down')
uvloop.run(run_server())
The uvloop version closes the listening socket in response to SIGTERM, but never exits. The stock version prints Shutting down and exits.
Here's the CPython source where this is implemented:
https://github.com/python/cpython/blob/v3.8.18/Lib/asyncio/base_events.py#L341-L344
Thanks for taking a look!
- Dominant language
- Cython
- Stars
- 11.9k
- Forks
- 615
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MagicStack/uvloop
-
License not clear Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#759 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#741 · 2 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
MagicStack/uvloop#702 · 8 comments · 9 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
MagicStack/uvloop#766 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
MagicStack/uvloop#765 ·
All issues in MagicStack/uvloop
Similar issues
-
clawsweeper:linked-pr-open clawsweeper:no-new-fix-pr clawsweeper:source-repro impact:message-loss issue-rating: 🦞 diamond lobster maturity:stable P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
growthbook/growthbook#7100 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
punkpeye/mcp-remote#369 ·
-
Client customer-reported needs-team-attention question Service Attention WebPubSub
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Azure/azure-sdk-for-net#63292 · 3 comments · 1 reaction ·
-
bug p:3-mid pydanty:bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pydantic/pydantic-ai#8643 · 1 comment ·