-
Notifications
You must be signed in to change notification settings - Fork 818
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4615 from Textualize/fix-notify-error
Fix notifications crash
- Loading branch information
Showing
12 changed files
with
163 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
from asyncio import Lock, Task, current_task | ||
|
||
|
||
class RLock: | ||
"""A re-entrant asyncio lock.""" | ||
|
||
def __init__(self) -> None: | ||
self._owner: Task | None = None | ||
self._count = 0 | ||
self._lock = Lock() | ||
|
||
async def acquire(self) -> None: | ||
"""Wait until the lock can be acquired.""" | ||
task = current_task() | ||
assert task is not None | ||
if self._owner is None or self._owner is not task: | ||
await self._lock.acquire() | ||
self._owner = task | ||
self._count += 1 | ||
|
||
def release(self) -> None: | ||
"""Release a previously acquired lock.""" | ||
task = current_task() | ||
assert task is not None | ||
self._count -= 1 | ||
if self._count < 0: | ||
# Should not occur if every acquire as a release | ||
raise RuntimeError("RLock.release called too many times") | ||
if self._owner is task: | ||
if not self._count: | ||
self._owner = None | ||
self._lock.release() | ||
|
||
@property | ||
def is_locked(self): | ||
"""Return True if lock is acquired.""" | ||
return self._lock.locked() | ||
|
||
async def __aenter__(self) -> None: | ||
"""Asynchronous context manager to acquire and release lock.""" | ||
await self.acquire() | ||
|
||
async def __aexit__(self, _type, _value, _traceback) -> None: | ||
"""Exit the context manager.""" | ||
self.release() | ||
|
||
|
||
if __name__ == "__main__": | ||
from asyncio import Lock | ||
|
||
async def locks(): | ||
lock = RLock() | ||
async with lock: | ||
async with lock: | ||
print("Hello") | ||
|
||
import asyncio | ||
|
||
asyncio.run(locks()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
from textual.rlock import RLock | ||
|
||
|
||
async def test_simple_lock(): | ||
lock = RLock() | ||
# Starts not locked | ||
assert not lock.is_locked | ||
# Acquire the lock | ||
await lock.acquire() | ||
assert lock.is_locked | ||
# Acquire a second time (should not block) | ||
await lock.acquire() | ||
assert lock.is_locked | ||
|
||
# Release the lock | ||
lock.release() | ||
# Should still be locked | ||
assert lock.is_locked | ||
# Release the lock | ||
lock.release() | ||
# Should be released | ||
assert not lock.is_locked | ||
|
||
# Another release is a runtime error | ||
with pytest.raises(RuntimeError): | ||
lock.release() | ||
|
||
|
||
async def test_multiple_tasks() -> None: | ||
"""Check RLock prevents other tasks from acquiring lock.""" | ||
lock = RLock() | ||
|
||
started: list[int] = [] | ||
done: list[int] = [] | ||
|
||
async def test_task(n: int) -> None: | ||
started.append(n) | ||
async with lock: | ||
done.append(n) | ||
|
||
async with lock: | ||
assert done == [] | ||
task1 = asyncio.create_task(test_task(1)) | ||
assert sorted(started) == [] | ||
task2 = asyncio.create_task(test_task(2)) | ||
await asyncio.sleep(0) | ||
assert sorted(started) == [1, 2] | ||
|
||
await task1 | ||
assert 1 in done | ||
await task2 | ||
assert 2 in done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters