diff --git a/roc/channel_manager.py b/roc/channel_manager.py index 0beb667..c729dba 100644 --- a/roc/channel_manager.py +++ b/roc/channel_manager.py @@ -23,6 +23,7 @@ def close(self, key: int) -> bool: if chan is None: return True + del self.channels[key] return chan.close() def flush(self): diff --git a/roc/socket.py b/roc/socket.py index e54a3b8..956c6ae 100644 --- a/roc/socket.py +++ b/roc/socket.py @@ -98,13 +98,16 @@ async def request(self, request: Request) -> Response: await self.send(packet) - res = await chan.pop() - if res is False: - raise RequestException("request failed") + try: + res = await chan.pop() + if res is False: + raise RequestException("request failed") - data = json.loads(res) + data = json.loads(res) - return make_response(data) + return make_response(data) + finally: + self.channelManager.close(key) async def start(self): reader, writer = await asyncio.open_connection(self.host, self.port)