Skip to content

uvloop runs at_fork hook when running subprocess #749

@drtyrsa

Description

@drtyrsa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions