Skip to content

Commit de3a116

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

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

pynvim/msgpack_rpc/event_loop/asyncio.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,33 @@ async def connect_stdout():
188188

189189
@override
190190
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)
191+
def can_use_pidfd():
192+
try:
193+
pid = os.getpid()
194+
fd = os.pidfd_open(pid, 0)
195+
os.close(fd)
196+
except OSError:
197+
# blocked by security policy like SECCOMP
198+
return False
199+
return True
200+
201+
def get_child_watcher():
202+
# Unix system without pidfd_open
203+
if hasattr(os, "pidfd_open"):
204+
try:
205+
from asyncio.unix_events import PidfdChildWatcher
206+
if can_use_pidfd():
207+
return PidfdChildWatcher()
208+
except ImportError:
209+
pass
210+
211+
try:
212+
from asyncio.unix_events import ThreadedChildWatcher
213+
return ThreadedChildWatcher()
214+
except ImportError:
215+
pass
216+
217+
return asyncio.get_child_watcher()
195218

196219
async def create_subprocess():
197220
transport: asyncio.SubprocessTransport # type: ignore
@@ -200,6 +223,12 @@ async def create_subprocess():
200223
pid = transport.get_pid()
201224
debug("child subprocess_exec successful, PID = %s", pid)
202225

226+
if os.name != 'nt':
227+
watcher = get_child_watcher()
228+
229+
watcher.attach_loop(self._loop)
230+
self._child_watcher = watcher
231+
203232
self._transport = cast(asyncio.WriteTransport,
204233
transport.get_pipe_transport(0)) # stdin
205234
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)