Skip to content

Commit c97bad9

Browse files
committed
replace locks in async_cache and text_document with threading.RLocks
1 parent 0324713 commit c97bad9

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

robotcode/language_server/common/text_document.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import weakref
88
from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar, Union, cast
99

10-
from ...utils.async_tools import Lock, async_event, create_sub_task
10+
from ...utils.async_tools import async_event, create_sub_task
1111
from ...utils.logging import LoggingDescriptor
1212
from ...utils.uri import Uri
1313
from .lsp_types import DocumentUri, Range
@@ -24,7 +24,7 @@ class CacheEntry:
2424
def __init__(self) -> None:
2525
self.data: Any = None
2626
self.has_data: bool = False
27-
self.lock: Lock = Lock()
27+
self.lock = threading.RLock()
2828

2929

3030
class TextDocument:
@@ -216,16 +216,14 @@ async def get_cache(
216216

217217
reference = self.__get_cache_reference(entry)
218218

219-
# with self._lock:
220219
e = self._cache[reference]
221220

222-
if not e.has_data:
223-
async with e.lock:
224-
if not e.has_data:
225-
e.data = await entry(self, *args, **kwargs)
226-
e.has_data = True
221+
with e.lock:
222+
if not e.has_data:
223+
e.data = await entry(self, *args, **kwargs)
224+
e.has_data = True
227225

228-
return cast(_T, e.data)
226+
return cast(_T, e.data)
229227

230228
@_logger.call
231229
async def remove_cache_entry(

robotcode/utils/async_cache.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import threading
12
from collections import defaultdict
23
from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple, TypeVar, cast
34

4-
from .async_tools import Lock
5-
65
_T = TypeVar("_T")
76

87

@@ -16,7 +15,7 @@ class CacheEntry:
1615
def __init__(self) -> None:
1716
self.data: Any = None
1817
self.has_data: bool = False
19-
self.lock: Lock = Lock()
18+
self.lock = threading.RLock()
2019

2120

2221
class AsyncSimpleLRUCache:
@@ -27,7 +26,7 @@ def __init__(self, max_items: Optional[int] = 128) -> None:
2726
self._order: Optional[List[Tuple[Any, ...]]] = None
2827
if self.max_items:
2928
self._order = []
30-
self._lock: Lock = Lock()
29+
self._lock = threading.RLock()
3130

3231
async def has(self, *args: Any, **kwargs: Any) -> bool:
3332
key = self._make_key(*args, **kwargs)
@@ -42,27 +41,26 @@ async def get(self, func: Callable[..., Awaitable[_T]], *args: Any, **kwargs: An
4241

4342
entry = self._cache[key]
4443

45-
if not entry.has_data:
46-
async with entry.lock:
47-
if not entry.has_data:
48-
entry.data = await func(*args, **kwargs)
49-
entry.has_data = True
44+
with entry.lock:
45+
if not entry.has_data:
46+
entry.data = await func(*args, **kwargs)
47+
entry.has_data = True
5048

51-
if self._order is not None and self.max_items is not None:
52-
async with self._lock:
53-
self._order.insert(0, key)
49+
if self._order is not None and self.max_items is not None:
50+
with self._lock:
51+
self._order.insert(0, key)
5452

55-
if len(self._order) > self.max_items:
56-
del self._cache[self._order.pop()]
53+
if len(self._order) > self.max_items:
54+
del self._cache[self._order.pop()]
5755

58-
return cast(_T, entry.data)
56+
return cast(_T, entry.data)
5957

6058
@staticmethod
6159
def _make_key(*args: Any, **kwargs: Any) -> Tuple[Any, ...]:
6260
return (tuple(_freeze(v) for v in args), hash(frozenset({k: _freeze(v) for k, v in kwargs.items()})))
6361

6462
async def clear(self) -> None:
65-
async with self._lock:
63+
with self._lock:
6664
self._cache.clear()
6765
if self._order is not None:
6866
self._order.clear()

tests/robotcode/language_server/robotframework/parts/data/.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
// "robotcode.debug.outputMessages": true
3232
//"python.analysis.diagnosticMode": "workspace"
3333
//"robotcode.robot.paths": ["./tests", "./tests1"]
34-
"robotcode.analysis.diagnosticMode": "workspace"
34+
"robotcode.analysis.diagnosticMode": "workspace",
35+
"robotcode.analysis.progressMode": "detailed"
3536
}

0 commit comments

Comments
 (0)