Skip to content

Cleaner fix for RPC issue #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions stepup/core/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pickle
import socket
import subprocess
import sys
import traceback
from collections.abc import Awaitable, Callable, Collection
from functools import partial
Expand Down Expand Up @@ -221,31 +222,25 @@ async def _serve_rpc_send_loop(

async def _handle_connection(
handler,
stop_events: list[asyncio.Event],
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
):
stop_event = asyncio.Event()
stop_events.append(stop_event)
await serve_rpc(handler, reader, writer, stop_event)
await serve_rpc(handler, reader, writer)
await writer.drain()
writer.close()
await writer.wait_closed()


async def serve_socket_rpc(handler, path, stop_event):
# Keep a list of stop_events, including one for each handler.
# This works around an apparent (but difficult to isolate) issue in Python 3.11:
# The server context handler exits before all open requests are handled,
# resulting in lost connection errors.
# (This is not needed for Python 3.12)
stop_events = [stop_event]
server = await asyncio.start_unix_server(
partial(_handle_connection, handler, stop_events), path
)
async def serve_socket_rpc(handler, path: str, stop_event: asyncio.Event):
server = await asyncio.start_unix_server(partial(_handle_connection, handler), path)
async with server:
while len(stop_events) > 0:
await stop_events.pop().wait()
await stop_event.wait()
if sys.version_info < (3, 12, 1) and server._waiters is not None:
# Workaround for server.wait_closed() issue fixed in Python 3.12.1
# See https://github.com/python/cpython/issues/120866
waiter = server.get_loop().create_future()
server._waiters.append(waiter)
await waiter


async def serve_stdio_rpc(handler):
Expand Down