MagicStack / MagicStack/uvloop

uvloop runs at_fork hook when running subprocess

Open
#749 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Cython
Stars
11.9k
Forks
616
PR merge metrics
No merged PRs in 30d

Description

Speaking about `os.register_at_fork` python docs [explicitly state](https://docs.python.org/dev/library/os.html#os.register_at_fork):
> These calls are only made if control is expected to return to the Python interpreter. A typical [subprocess](https://docs.python.org/dev/library/subprocess.html#module-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:
```python
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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing uvloop's handling of asyncio.create_subprocess_exec and the fork path used after uvloop.install(), comparing it with the standard asyncio loop and os.register_at_fork behavior. Re-run the provided reproduction with the uptime subprocess; done means the after_in_child hook is not triggered for this subprocess launch.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.