Skip to content

Commit f1b7301

Browse files
authored
Fixed stderr writes in a worker subprocess causing a deadlock (#1207)
1 parent 212be93 commit f1b7301

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎docs/versionhistory.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
1313
identity (``value is math.inf``), so only the exact ``math.inf`` singleton was accepted,
1414
while every backend setter (using ``math.isinf()``) accepts any positive infinity
1515
(`#1189 <https://github.com/agronholm/anyio/pull/1189>`_; PR by @greymoth-jp).
16-
- Fixed ``anyio.open_process()`` (and ``run_process()``) ignoring the ``extra_groups``
17-
argument, as it mistakenly passed the value of the ``group`` argument instead
18-
(`#1209 <https://github.com/agronholm/anyio/pull/1209>`_)
16+
- Fixed ``to_process.run_sync()`` deadlocking when the worker function writes enough data
17+
to ``sys.stderr`` to fill the (undrained) pipe buffer. The worker process now redirects
18+
``sys.stderr`` to ``os.devnull`` as well, matching the documented behavior
1919
- Fixed ``TLSStream.wrap()`` matching an internationalized (unicode) host name against
2020
the peer certificate using IDNA 2003 (via the standard library) instead of IDNA 2008,
2121
which could cause the host name to be matched against the wrong certificate
2222
(`#1208 <https://github.com/agronholm/anyio/pull/1208>`_)
23+
- Fixed ``anyio.open_process()`` (and ``run_process()``) ignoring the ``extra_groups``
24+
argument, as it mistakenly passed the value of the ``group`` argument instead
25+
(`#1209 <https://github.com/agronholm/anyio/pull/1209>`_)
2326

2427
**4.14.1**
2528

‎src/anyio/to_process.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def process_worker() -> None:
211211
stdout = sys.stdout
212212
sys.stdin = open(os.devnull)
213213
sys.stdout = open(os.devnull, "w")
214+
sys.stderr = open(os.devnull, "w")
214215

215216
stdout.buffer.write(b"READY\n")
216217
while True:

‎tests/test_to_process.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ async def test_print() -> None:
5858
assert await to_process.run_sync(os.getpid) == worker_pid
5959

6060

61+
def _flood_stderr() -> str:
62+
"""Helper that writes enough data to stderr to fill an undrained pipe buffer."""
63+
64+
# A typical pipe buffer is 64 KiB; write well beyond that to ensure it would block
65+
# if stderr were still connected to the (undrained) parent pipe.
66+
payload = "x" * (1024 * 1024)
67+
for stream in sys.stdout, sys.stderr:
68+
stream.write(payload)
69+
stream.flush()
70+
71+
return "completed"
72+
73+
74+
async def test_stderr_flood() -> None:
75+
"""
76+
Test that writing large amounts of data to stderr in the worker process won't
77+
deadlock the call. Regression test for the worker's stderr not being redirected to
78+
/dev/null, leaving it connected to an undrained pipe.
79+
"""
80+
81+
with fail_after(10):
82+
assert await to_process.run_sync(_flood_stderr) == "completed"
83+
84+
6185
async def test_cancel_before() -> None:
6286
"""
6387
Test that starting to_process.run_sync() in a cancelled scope does not cause a

0 commit comments

Comments
 (0)