Skip to content

Commit 5e1f993

Browse files
committed
ensure diagnostics task is canceled
1 parent aa25970 commit 5e1f993

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

robotcode/language_server/common/parts/diagnostics.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,17 @@ def done(t: asyncio.Task[Any]) -> None:
360360

361361
if task is not None and not task.done():
362362
self._logger.debug(lambda: f"try to cancel diagnostics for {document}")
363-
task.get_loop().call_soon_threadsafe(task.cancel)
363+
364+
async def cancel(t: asyncio.Task[Any]) -> None:
365+
t.cancel()
366+
try:
367+
await t
368+
except asyncio.CancelledError:
369+
pass
370+
371+
asyncio.run_coroutine_threadsafe(cancel(task), loop=task.get_loop()).result(5)
372+
373+
# task.get_loop().call_soon_threadsafe(task.cancel)
364374

365375
data.version = document.version
366376
data.task = create_sub_task(

robotcode/utils/async_tools.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ async def async_lock(lock: threading.RLock) -> AsyncGenerator[None, None]:
413413
# yield
414414

415415

416-
class Event:
416+
class NewEvent:
417417
def __init__(self, value: bool = False) -> None:
418418
self._event = threading.Event()
419419
if value:
@@ -443,7 +443,7 @@ async def wait(self, timeout: Optional[float] = None) -> bool:
443443
return result
444444

445445

446-
class OldEvent:
446+
class Event:
447447
"""Thread safe version of an async Event"""
448448

449449
def __init__(self, value: bool = False) -> None:
@@ -488,8 +488,14 @@ def set_result(w: asyncio.Future[Any], ev: threading.Event) -> None:
488488

489489
fut.get_loop().call_soon_threadsafe(set_result, fut, done)
490490

491-
if not done.wait(120):
492-
raise TimeoutError("Callback timeout.")
491+
start = time.monotonic()
492+
while not done.is_set():
493+
check_canceled_sync()
494+
495+
if time.monotonic() - start > 120:
496+
raise TimeoutError("Can't set future result.")
497+
498+
time.sleep(0.001)
493499

494500
def clear(self) -> None:
495501
with self._lock:
@@ -718,7 +724,7 @@ class Lock:
718724
def __init__(self) -> None:
719725
self._waiters: Optional[Deque[asyncio.Future[Any]]] = None
720726
self._locked = False
721-
self._lock = threading.RLock()
727+
self._lock = threading.Lock()
722728

723729
async def __aenter__(self) -> None:
724730
await self.acquire()
@@ -737,16 +743,13 @@ def __repr__(self) -> str:
737743

738744
@asynccontextmanager
739745
async def __inner_lock(self) -> AsyncGenerator[Any, None]:
740-
b = self._lock.acquire(blocking=False)
741-
while not b:
746+
while not (b := self._lock.acquire(blocking=False)):
742747
await asyncio.sleep(0.001)
743-
b = self._lock.acquire(blocking=False)
744748
try:
745749
yield None
746750
finally:
747-
self._lock.release()
748-
# with self._lock:
749-
# yield None
751+
if b:
752+
self._lock.release()
750753

751754
@property
752755
def locked(self) -> bool:
@@ -765,16 +768,12 @@ async def acquire(self) -> bool:
765768
self._waiters.append(fut)
766769

767770
try:
768-
try:
769-
await fut
770-
finally:
771-
async with self.__inner_lock():
772-
if fut in self._waiters:
773-
self._waiters.remove(fut)
774-
self._locked = True
775-
except asyncio.CancelledError:
776-
await self._wake_up_next()
777-
raise
771+
await fut
772+
finally:
773+
async with self.__inner_lock():
774+
if fut in self._waiters:
775+
self._waiters.remove(fut)
776+
self._locked = True
778777

779778
return True
780779

0 commit comments

Comments
 (0)