Skip to content

Commit 99a2dc3

Browse files
committed
hack for Lock.aquire
1 parent ba95e7c commit 99a2dc3

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

robotcode/utils/async_tools.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,16 @@ def __repr__(self) -> str:
576576

577577
@asynccontextmanager
578578
async def __inner_lock(self) -> AsyncGenerator[bool, None]:
579-
with self._lock as r:
580-
yield r
579+
b = self._lock.acquire(blocking=False)
580+
while not b:
581+
await asyncio.sleep(0.01)
582+
b = self._lock.acquire(blocking=False)
583+
584+
try:
585+
yield b
586+
finally:
587+
if b:
588+
self._lock.release()
581589

582590
async def acquire(self) -> bool:
583591
async with self.__inner_lock():
@@ -595,13 +603,18 @@ async def acquire(self) -> bool:
595603

596604
try:
597605
try:
598-
await fut
606+
await asyncio.wait_for(fut, 15) # TODO remove this hack
607+
except asyncio.TimeoutError:
608+
pass
599609
finally:
600610
self._waiters.remove(fut)
601611
except asyncio.CancelledError:
612+
wakeup = False
602613
async with self.__inner_lock():
603-
if not self._locked:
604-
self._wake_up_first()
614+
wakeup = not self._locked
615+
616+
if wakeup:
617+
self._wake_up_first()
605618
raise
606619

607620
async with self.__inner_lock():
@@ -610,12 +623,13 @@ async def acquire(self) -> bool:
610623
return True
611624

612625
async def release(self) -> None:
613-
if self._locked:
614-
async with self.__inner_lock():
626+
async with self.__inner_lock():
627+
if self._locked:
615628
self._locked = False
616-
self._wake_up_first()
617-
else:
618-
raise RuntimeError("Lock is not acquired.")
629+
else:
630+
raise RuntimeError("Lock is not acquired.")
631+
632+
self._wake_up_first()
619633

620634
def _wake_up_first(self) -> None:
621635
if not self._waiters:
@@ -625,11 +639,14 @@ def _wake_up_first(self) -> None:
625639
except StopIteration:
626640
return
627641

642+
def s() -> None:
643+
fut.set_result(True)
644+
628645
if not fut.done():
629646
if fut._loop == asyncio.get_running_loop():
630647
fut.set_result(True)
631648
else:
632-
fut._loop.call_soon_threadsafe(fut.set_result, True)
649+
fut._loop.call_soon_threadsafe(s)
633650

634651

635652
class FutureInfo:

0 commit comments

Comments
 (0)