From 11f4eadcd323908ad25a1f08e85da51845572974 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Mon, 4 Dec 2023 13:12:34 +0100 Subject: [PATCH] Turn command id into global the response id is tied to the request id thus multiple instances of the Tab class can cause problems when prior messages are not consumed properly --- backend/src/injector.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/src/injector.py b/backend/src/injector.py index a217f6891..64cb99393 100644 --- a/backend/src/injector.py +++ b/backend/src/injector.py @@ -13,6 +13,9 @@ logger = getLogger("Injector") +# Request IDs have to be globally unique to prevent accidentally reading the wrong response +cmd_id = 0 + class _TabResponse(TypedDict): title: str id: str @@ -20,8 +23,6 @@ class _TabResponse(TypedDict): webSocketDebuggerUrl: str class Tab: - cmd_id = 0 - def __init__(self, res: _TabResponse) -> None: self.title: str = res["title"] self.id: str = res["id"] @@ -50,9 +51,11 @@ async def listen_for_message(self): await self.close_websocket() async def _send_devtools_cmd(self, dc: Dict[str, Any], receive: bool = True): + global cmd_id + if self.websocket: - self.cmd_id += 1 - dc["id"] = self.cmd_id + cmd_id += 1 + dc["id"] = cmd_id await self.websocket.send_json(dc) if receive: async for msg in self.listen_for_message():