Skip to content

Commit cd8df19

Browse files
committed
test passes
1 parent 2996617 commit cd8df19

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

packages/service-library/src/servicelib/redis/_decorators.py

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88

99
import redis.exceptions
1010
from redis.asyncio.lock import Lock
11-
from tenacity import retry
12-
13-
from servicelib.async_utils import cancel_wait_task, with_delay
14-
from servicelib.logging_utils import log_context
1511

1612
from ..background_task import periodic
1713
from ._client import RedisClientSDK
18-
from ._constants import DEFAULT_LOCK_TTL, SHUTDOWN_TIMEOUT_S
14+
from ._constants import DEFAULT_LOCK_TTL
1915
from ._errors import CouldNotAcquireLockError, LockLostError
2016
from ._utils import auto_extend_lock
2117

@@ -25,30 +21,15 @@
2521
R = TypeVar("R")
2622

2723
_EXCLUSIVE_TASK_NAME: Final[str] = "exclusive/{func_name}"
28-
_EXCLUSIVE_AUTO_EXTEND_TASK_NAME: Final[str] = (
29-
"exclusive/autoextend_lock_{redis_lock_key}"
30-
)
24+
_EXCLUSIVE_AUTO_EXTEND_TASK_NAME: Final[
25+
str
26+
] = "exclusive/autoextend_lock_{redis_lock_key}"
3127

3228

3329
@periodic(interval=DEFAULT_LOCK_TTL / 2, raise_on_error=True)
3430
async def _periodic_auto_extender(lock: Lock, started_event: asyncio.Event) -> None:
35-
started_event.set()
3631
await auto_extend_lock(lock)
37-
current_task = asyncio.tasks.current_task()
38-
assert current_task is not None # nosec
39-
print(current_task.cancelling())
40-
41-
42-
def _cancel_auto_extender_task(
43-
_: asyncio.Task, *, auto_extend_task: asyncio.Task
44-
) -> None:
45-
with log_context(
46-
_logger,
47-
logging.DEBUG,
48-
f"Cancelling auto-extend task {auto_extend_task.get_name()}",
49-
):
50-
auto_extend_task.cancel()
51-
assert auto_extend_task.cancelling()
32+
started_event.set()
5233

5334

5435
def exclusive(
@@ -101,9 +82,9 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
10182
if not await lock.acquire(
10283
token=lock_value,
10384
blocking=blocking,
104-
blocking_timeout=blocking_timeout.total_seconds()
105-
if blocking_timeout
106-
else None,
85+
blocking_timeout=(
86+
blocking_timeout.total_seconds() if blocking_timeout else None
87+
),
10788
):
10889
raise CouldNotAcquireLockError(lock=lock)
10990

@@ -117,7 +98,8 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
11798
redis_lock_key=redis_lock_key
11899
),
119100
)
120-
# NOTE: in case the work task is super short lived, then we might fail in cancelling it
101+
# NOTE: In case the work thread is raising right away,
102+
# this ensures the extend task ran once and ensure cancellation works
121103
await started_event.wait()
122104

123105
# then the task that runs the user code
@@ -127,20 +109,11 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
127109
name=_EXCLUSIVE_TASK_NAME.format(func_name=func.__name__),
128110
)
129111

130-
# work_task.add_done_callback(
131-
# functools.partial(
132-
# _cancel_auto_extender_task,
133-
# auto_extend_task=auto_extend_lock_task,
134-
# )
135-
# )
136-
137112
res = await work_task
138113
auto_extend_lock_task.cancel()
139-
140114
return res
141115

142116
except BaseExceptionGroup as eg:
143-
breakpoint()
144117
# Separate exceptions into LockLostError and others
145118
lock_lost_errors, other_errors = eg.split(LockLostError)
146119

@@ -158,8 +131,6 @@ async def _wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
158131
lock.name,
159132
)
160133
raise lock_lost_errors.exceptions[0] from eg
161-
except Exception as exc:
162-
breakpoint()
163134
finally:
164135
with contextlib.suppress(redis.exceptions.LockNotOwnedError):
165136
# in the case where the lock would have been lost,

packages/service-library/tests/redis/test_decorators.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import pytest
1313
from faker import Faker
1414
from servicelib.redis import CouldNotAcquireLockError, RedisClientSDK, exclusive
15-
from servicelib.redis._errors import LockLostError
1615
from servicelib.redis._decorators import _EXCLUSIVE_AUTO_EXTEND_TASK_NAME
16+
from servicelib.redis._errors import LockLostError
1717
from servicelib.utils import limited_gather, logged_gather
1818

1919
pytest_simcore_core_services_selection = [
@@ -214,19 +214,13 @@ async def test_exclusive_task_erroring_releases_lock(
214214
):
215215
@exclusive(redis_client_sdk, lock_key=lock_name)
216216
async def _() -> None:
217-
# await asyncio.sleep(0)
218-
219-
print("raising error")
220217
msg = "Expected error"
221218
raise RuntimeError(msg)
222219

223220
# initial state
224221
assert await _is_locked(redis_client_sdk, lock_name) is False
225222
assert await redis_client_sdk.lock_value(lock_name) is None
226223

227-
# run the exclusive task
228-
# exclusive_task = asyncio.create_task(_())
229-
230224
with pytest.raises(RuntimeError):
231225
await _()
232226

0 commit comments

Comments
 (0)