MagicStack / MagicStack/uvloop

Server.serve_forever() isn't cancelled when Server.close() is called

未关闭
#602 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Cython
星标
11.9k
派生
616
PR 合并指标
30 天内没有已合并 PR

描述

  • 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 PYTHONASYNCIODEBUG in 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!

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,将 uvloop 的服务器行为与 Lib/asyncio/base_events.py 中第 341–344 行附近链接的 CPython 实现进行比较,重点关注 Server.serve_forever() 和 Server.close()。使用提供的 SIGTERM 示例重现该问题,并确认关闭服务器会导致 serve_forever() 引发 asyncio.CancelledError,并在退出前打印“Shutting down”。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend, networking
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。