Skip to content

Commit

Permalink
Turn command id into global
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Jan200101 committed Dec 4, 2023
1 parent 91186da commit 11f4ead
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions backend/src/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

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
url: str
webSocketDebuggerUrl: str

class Tab:
cmd_id = 0

def __init__(self, res: _TabResponse) -> None:
self.title: str = res["title"]
self.id: str = res["id"]
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 11f4ead

Please sign in to comment.