server: python rpc should use create_task instead of run_coroutine_threadsafe (#1953)

run_coroutine_threadsafe is designed for scheduling coroutines from a
different thread onto the event loop. Since readLoop is already running
as an async function on the event loop, using create_task is the correct
and more efficient approach.

This removes unnecessary thread-safe queue overhead for every RPC message.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Raman Gupta
2026-01-15 12:34:44 -05:00
committed by GitHub
parent c1895df062
commit a4d28791ed

View File

@@ -206,7 +206,7 @@ class RpcConnectionTransport(RpcTransport):
return self.writeMessage(bytes(buffer), reject) return self.writeMessage(bytes(buffer), reject)
async def readLoop(loop, peer: rpc.RpcPeer, rpcTransport: RpcTransport): async def readLoop(peer: rpc.RpcPeer, rpcTransport: RpcTransport):
deserializationContext = {"buffers": []} deserializationContext = {"buffers": []}
while True: while True:
@@ -216,9 +216,7 @@ async def readLoop(loop, peer: rpc.RpcPeer, rpcTransport: RpcTransport):
deserializationContext["buffers"].append(message) deserializationContext["buffers"].append(message)
continue continue
asyncio.run_coroutine_threadsafe( asyncio.create_task(peer.handleMessage(message, deserializationContext))
peer.handleMessage(message, deserializationContext), loop
)
deserializationContext = {"buffers": []} deserializationContext = {"buffers": []}
@@ -246,7 +244,7 @@ async def prepare_peer_readloop(loop: AbstractEventLoop, rpcTransport: RpcTransp
async def peerReadLoop(): async def peerReadLoop():
try: try:
await readLoop(loop, peer, rpcTransport) await readLoop(peer, rpcTransport)
except: except:
peer.kill() peer.kill()
raise raise