From 836f65376c26106ca262e9b17ecea6af040601e7 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 6 Nov 2023 16:49:34 +0000 Subject: [PATCH] fix(deltachat-rpc-client): add the Lock around request ID Avoid handing out the same request ID twice. --- deltachat-rpc-client/src/deltachat_rpc_client/rpc.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py b/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py index 4a181c528c..0240902835 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/rpc.py @@ -4,7 +4,7 @@ import subprocess import sys from queue import Queue -from threading import Event, Thread +from threading import Event, Lock, Thread from typing import Any, Dict, Optional @@ -24,6 +24,7 @@ def __init__(self, accounts_dir: Optional[str] = None, **kwargs): self._kwargs = kwargs self.process: subprocess.Popen self.id: int + self.id_lock: Lock self.event_queues: Dict[int, Queue] # Map from request ID to `threading.Event`. self.request_events: Dict[int, Event] @@ -55,6 +56,7 @@ def start(self) -> None: **self._kwargs, ) self.id = 0 + self.id_lock = Lock() self.event_queues = {} self.request_events = {} self.request_results = {} @@ -143,14 +145,16 @@ def wait_for_event(self, account_id: int) -> Optional[dict]: def __getattr__(self, attr: str): def method(*args) -> Any: + self.id_lock.acquire() self.id += 1 request_id = self.id + self.id_lock.release() request = { "jsonrpc": "2.0", "method": attr, "params": args, - "id": self.id, + "id": request_id, } event = Event() self.request_events[request_id] = event