Skip to content

Commit 451d09a

Browse files
Handle disconnects in AsyncioClient
1 parent a0de059 commit 451d09a

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

sipyco/pc_rpc.py

Lines changed: 18 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.
@@ -269,8 +271,12 @@ def __send(self, obj):
269271
line = pyon.encode(obj) + "\n"
270272
self.__writer.write(line.encode())
271273

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

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

0 commit comments

Comments
 (0)