Skip to content

Commit 907637a

Browse files
committed
Replace deprecated asyncio.get_child_watcher()
Use PidfdChildWatcher if os.pidfd_open is available and works, and otherwise use ThreadedChildWatcher which should work in any case. Fixes #583
1 parent 6fa9045 commit 907637a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

pynvim/msgpack_rpc/event_loop/asyncio.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from collections import deque
1010
from signal import Signals
1111
from typing import Any, Callable, Deque, List, Optional, cast
12+
from asyncio.unix_events import ThreadedChildWatcher
13+
try:
14+
from asyncio.unix_events import PidfdChildWatcher
15+
except ImportError:
16+
PidfdChildWatcher = None
1217

1318
if sys.version_info >= (3, 12):
1419
from typing import Final, override
@@ -188,18 +193,33 @@ async def connect_stdout():
188193

189194
@override
190195
def _connect_child(self, argv: List[str]) -> None:
191-
if os.name != 'nt':
192-
# see #238, #241
193-
self._child_watcher = asyncio.get_child_watcher()
194-
self._child_watcher.attach_loop(self._loop)
195-
196196
async def create_subprocess():
197197
transport: asyncio.SubprocessTransport # type: ignore
198198
transport, protocol = await self._loop.subprocess_exec(
199199
self._protocol_factory, *argv)
200200
pid = transport.get_pid()
201201
debug("child subprocess_exec successful, PID = %s", pid)
202202

203+
if os.name != 'nt':
204+
# see #238, #241
205+
pidfd_works = False
206+
if PidfdChildWatcher is not None and hasattr(os, "pidfd_open"):
207+
try:
208+
fd = os.pidfd_open(pid)
209+
except Exception:
210+
pass
211+
else:
212+
os.close(fd)
213+
pidfd_works = True
214+
215+
if pidfd_works:
216+
watcher = PidfdChildWatcher()
217+
else:
218+
watcher = ThreadedChildWatcher()
219+
220+
watcher.attach_loop(self._loop)
221+
self._child_watcher = watcher
222+
203223
self._transport = cast(asyncio.WriteTransport,
204224
transport.get_pipe_transport(0)) # stdin
205225
self._protocol = protocol

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[tox]
55
min_version = 4.0
66
envlist =
7-
py{37,38,39,310,311,312}-asyncio
7+
py{37,38,39,310,311,312,313}-asyncio
88
checkqa
99
skip_missing_interpreters =
1010
true
@@ -18,6 +18,7 @@ python =
1818
3.10: py310
1919
3.11: py311
2020
3.12: py312
21+
3.13: py313
2122
pypy3: pypy3
2223

2324
[testenv]

0 commit comments

Comments
 (0)