Skip to content

Commit 56f508a

Browse files
Handle disconnects in AsyncioClient
1 parent a0de059 commit 56f508a

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

sipyco/pc_rpc.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,14 @@ def __send(self, obj):
153153
self.__socket.sendall(line.encode())
154154

155155
def __recv(self):
156-
if self.__closed:
157-
raise CONNECTION_CLOSED_ERR
158-
buf = self.__socket.recv(4096).decode()
159-
while "\n" not in buf:
160-
more = self.__socket.recv(4096)
161-
if not more:
162-
break
163-
buf += more.decode()
164-
if not buf:
156+
if not self.__closed:
157+
buf = self.__socket.recv(4096).decode()
158+
while "\n" not in buf:
159+
more = self.__socket.recv(4096)
160+
if not more:
161+
break
162+
buf += more.decode()
163+
if self.__closed or not buf:
165164
self.__closed = True
166165
raise CONNECTION_CLOSED_ERR
167166
return pyon.decode(buf)
@@ -198,7 +197,10 @@ class AsyncioClient:
198197
"""This class is similar to :class:`sipyco.pc_rpc.Client`, but
199198
uses ``asyncio`` instead of blocking calls.
200199
201-
All RPC methods are coroutines.
200+
All RPC methods are coroutines. As with :class:`sipyco.pc_rpc.Client`,
201+
methods will raise ConnectionAbortedError if the server closes the
202+
connection. The user should call :meth:`~sipyco.pc_rpc.AsyncioClient.close_rpc`
203+
and then discard this object.
202204
203205
Concurrent access from different asyncio tasks is supported; all calls
204206
use a single lock.
@@ -210,6 +212,7 @@ def __init__(self):
210212
self.__writer = None
211213
self.__target_names = None
212214
self.__description = None
215+
self.__closed = False
213216

214217
async def connect_rpc(self, host, port, target_name):
215218
"""Connects to the server. This cannot be done in __init__ because
@@ -269,8 +272,12 @@ def __send(self, obj):
269272
line = pyon.encode(obj) + "\n"
270273
self.__writer.write(line.encode())
271274

272-
async def __recv(self):
273-
line = await self.__reader.readline()
275+
async def __recv(self):
276+
if not self.__closed:
277+
line = await self.__reader.readline()
278+
if self.__closed or not line:
279+
self.__closed = True
280+
raise CONNECTION_CLOSED_ERR
274281
return pyon.decode(line.decode())
275282

276283
async def __do_rpc(self, name, args, kwargs):

0 commit comments

Comments
 (0)