mirror of
https://github.com/chatmail/core.git
synced 2026-05-09 01:46:30 +03:00
fix(deltachat-rpc-client): add the Lock around request ID
Avoid handing out the same request ID twice.
This commit is contained in:
@@ -4,7 +4,7 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Event, Thread
|
from threading import Event, Lock, Thread
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
|
|
||||||
@@ -24,6 +24,7 @@ class Rpc:
|
|||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
self.process: subprocess.Popen
|
self.process: subprocess.Popen
|
||||||
self.id: int
|
self.id: int
|
||||||
|
self.id_lock: Lock
|
||||||
self.event_queues: Dict[int, Queue]
|
self.event_queues: Dict[int, Queue]
|
||||||
# Map from request ID to `threading.Event`.
|
# Map from request ID to `threading.Event`.
|
||||||
self.request_events: Dict[int, Event]
|
self.request_events: Dict[int, Event]
|
||||||
@@ -55,6 +56,7 @@ class Rpc:
|
|||||||
**self._kwargs,
|
**self._kwargs,
|
||||||
)
|
)
|
||||||
self.id = 0
|
self.id = 0
|
||||||
|
self.id_lock = Lock()
|
||||||
self.event_queues = {}
|
self.event_queues = {}
|
||||||
self.request_events = {}
|
self.request_events = {}
|
||||||
self.request_results = {}
|
self.request_results = {}
|
||||||
@@ -143,14 +145,16 @@ class Rpc:
|
|||||||
|
|
||||||
def __getattr__(self, attr: str):
|
def __getattr__(self, attr: str):
|
||||||
def method(*args) -> Any:
|
def method(*args) -> Any:
|
||||||
|
self.id_lock.acquire()
|
||||||
self.id += 1
|
self.id += 1
|
||||||
request_id = self.id
|
request_id = self.id
|
||||||
|
self.id_lock.release()
|
||||||
|
|
||||||
request = {
|
request = {
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
"method": attr,
|
"method": attr,
|
||||||
"params": args,
|
"params": args,
|
||||||
"id": self.id,
|
"id": request_id,
|
||||||
}
|
}
|
||||||
event = Event()
|
event = Event()
|
||||||
self.request_events[request_id] = event
|
self.request_events[request_id] = event
|
||||||
|
|||||||
Reference in New Issue
Block a user