MagicStack / MagicStack/uvloop

uvloop runs at_fork hook when running subprocess

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

还没有人认领这个 Issue。

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

描述

Speaking about os.register_at_fork python docs explicitly state:

These calls are only made if control is expected to return to the Python interpreter. A typical subprocess launch will not trigger them as the child is not going to re-enter the interpreter.

The standard asyncio loop behaves this way. But uvloop doesn't. Repro:

import asyncio
import os
   
import uvloop


def hook():
    os.write(2, b"TRIGGERED\n")
    
    
async def run():        
    process = await asyncio.create_subprocess_exec(
        "uptime",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        start_new_session=True
    )

    stdout, stderr = await process.communicate()
    print(f"STDOUT: {stdout.decode('utf-8').strip()}")
    

if __name__ == "__main__":
    os.register_at_fork(after_in_child=hook)

    print("asyncio")
    asyncio.run(run())
    
    print("uvloop")
    uvloop.install()
    asyncio.run(run())

Output:

asyncio
STDOUT: 16:56:42 up 35 days,  6:20,  1 user,  load average: 5.21, 4.34, 4.27
uvloop
TRIGGERED
STDOUT: 16:56:42 up 35 days,  6:20,  1 user,  load average: 5.21, 4.34, 4.27

贡献指南

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

从这里开始

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

调研方向

首先跟踪 uvloop 对 asyncio.create_subprocess_exec 的处理,以及在 uvloop.install() 之后使用的 fork 路径,并将其与标准 asyncio 循环和 os.register_at_fork 的行为进行比较。使用 uptime 子进程重新运行提供的复现;当此次子进程启动未触发 after_in_child hook 时,即表示完成。

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

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 发到你的邮箱

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