Skip to content

Commit 462f722

Browse files
committed
make some more event handlers threaded
1 parent 42f05a5 commit 462f722

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

robotcode/language_server/common/text_document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ async def cache_invalidated(sender) -> None: # NOSONAR
162162
...
163163

164164
async def _invalidate_cache(self) -> None:
165-
await self.cache_invalidate(self)
165+
create_sub_task(self.cache_invalidate(self))
166166
self._cache.clear()
167-
await self.cache_invalidated(self)
167+
create_sub_task(self.cache_invalidated(self))
168168

169169
@_logger.call
170170
async def invalidate_cache(self) -> None:

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727

2828
from ....utils.async_cache import AsyncSimpleLRUCache
29-
from ....utils.async_tools import Lock, async_tasking_event, create_sub_task
29+
from ....utils.async_tools import Lock, async_tasking_event, create_sub_task, threaded
3030
from ....utils.logging import LoggingDescriptor
3131
from ....utils.path import path_is_relative_to
3232
from ....utils.uri import Uri
@@ -556,10 +556,12 @@ async def variables_changed(sender, variables: List[LibraryDoc]) -> None: # NOS
556556
async def imports_changed(sender, uri: DocumentUri) -> None: # NOSONAR
557557
...
558558

559+
@threaded()
559560
async def _do_imports_changed(self, sender: Any, uri: DocumentUri) -> None: # NOSONAR
560561
await self.imports_changed(self, uri)
561562

562563
@language_id("robotframework")
564+
@threaded()
563565
async def resource_document_changed(self, sender: Any, document: TextDocument) -> None:
564566
resource_changed: List[LibraryDoc] = []
565567

robotcode/language_server/robotframework/parts/diagnostics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
5353
parent.documents_cache.namespace_invalidated.add(self.namespace_invalidated)
5454

5555
@language_id("robotframework")
56+
@threaded()
5657
@_logger.call
5758
async def namespace_invalidated(self, sender: Any, namespace: Namespace) -> None:
5859
if namespace.document is not None:

robotcode/language_server/robotframework/parts/references.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616

1717
from ....utils.async_cache import AsyncSimpleLRUCache
18-
from ....utils.async_tools import async_event
18+
from ....utils.async_tools import async_event, threaded
1919
from ....utils.logging import LoggingDescriptor
2020
from ....utils.uri import Uri
2121
from ...common.decorators import language_id
@@ -56,6 +56,7 @@ def __init__(self, parent: RobotLanguageServerProtocol) -> None:
5656
async def cache_cleared(sender) -> None: # NOSONAR
5757
...
5858

59+
@threaded()
5960
async def document_did_change(self, sender: Any, document: TextDocument) -> None:
6061
await self._keyword_reference_cache.clear()
6162
await self._variable_reference_cache.clear()

robotcode/utils/async_tools.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,11 @@ async def release(self) -> None:
618618
await super().release()
619619

620620

621-
class Lock:
621+
class NewNewLock:
622622
def __init__(self) -> None:
623623
self._lock = threading.Lock()
624+
self._owner_thread: Optional[threading.Thread] = None
625+
self._owner_task: Optional[asyncio.Task[Any]] = None
624626

625627
def locked(self) -> bool:
626628
return self._lock.locked()
@@ -630,12 +632,11 @@ def acquire(self, blocking: bool = True, timeout: float = -1) -> bool:
630632

631633
def release(self) -> None:
632634
self._lock.release()
633-
self._owner_thread: Optional[threading.Thread] = None
634-
self._owner_task: Optional[asyncio.Task[Any]] = None
635+
self._owner_task = None
636+
self._owner_thread = None
635637

636638
async def acquire_async(self, blocking: bool = True, timeout: float = -1) -> bool:
637639
start = time.monotonic()
638-
639640
while not (aquired := self.acquire(blocking=False)):
640641
if not blocking:
641642
return False
@@ -644,23 +645,29 @@ async def acquire_async(self, blocking: bool = True, timeout: float = -1) -> boo
644645
if timeout > 0 and current > timeout:
645646
break
646647

647-
if current > 30:
648+
if current > 30 and self._owner_task is not None:
648649
tb = traceback.format_stack(self._owner_task.get_stack()[0]) if self._owner_task is not None else ""
649650
warnings.warn(
650651
f"locking takes to long {self._owner_thread} {self._owner_task} {tb}",
651652
)
652653

653654
await asyncio.sleep(0)
654655

656+
try:
657+
await asyncio.sleep(0)
658+
except asyncio.CancelledError:
659+
if aquired:
660+
self._lock.release()
661+
aquired = False
662+
raise
663+
655664
self._owner_task = asyncio.current_task()
656665
self._owner_thread = threading.current_thread()
657666

658667
return aquired
659668

660669
async def release_async(self) -> None:
661-
self._lock.release()
662-
self._owner_task = None
663-
self._owner_thread = None
670+
self.release()
664671

665672
def __enter__(self) -> None:
666673
self.acquire()
@@ -705,7 +712,7 @@ async def __aexit__(
705712
await self.release()
706713

707714

708-
class OldLock:
715+
class Lock:
709716
"""Threadsafe version of an async Lock."""
710717

711718
def __init__(self) -> None:
@@ -732,7 +739,7 @@ def __repr__(self) -> str:
732739
async def __inner_lock(self) -> AsyncGenerator[Any, None]:
733740
b = self._lock.acquire(blocking=False)
734741
while not b:
735-
await asyncio.sleep(0)
742+
await asyncio.sleep(0.001)
736743
b = self._lock.acquire(blocking=False)
737744
try:
738745
yield None
@@ -817,9 +824,6 @@ def set_result(w: asyncio.Future[Any], ev: threading.Event) -> None:
817824

818825
await asyncio.sleep(0.001)
819826

820-
# if not done.wait(120):
821-
# raise TimeoutError("Callback timeout.")
822-
823827

824828
class FutureInfo:
825829
def __init__(self, future: asyncio.Future[Any]) -> None:

0 commit comments

Comments
 (0)