Skip to content

Commit 0dff967

Browse files
authored
Properly handle asyncio.gather() call in SemanticCache.aheck (#263)
An `asyncio.gather()` call should be awaited. Failing to await it causes the tasks to run after the method exits, which can cause unexpected behavior, including failure if the event loop no longer exists. Fixes RAE-564
1 parent 18d1cfd commit 0dff967

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

redisvl/extensions/llmcache/semantic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ async def acheck(
462462
cache_search_results, return_fields # type: ignore
463463
)
464464
# Extend TTL on keys
465-
asyncio.gather(*[self._async_refresh_ttl(key) for key in redis_keys])
465+
await asyncio.gather(*[self._async_refresh_ttl(key) for key in redis_keys])
466466

467467
return cache_hits
468468

tests/integration/test_llmcache.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ async def test_async_ttl_expiration(cache_with_ttl, vectorizer):
272272
vector = vectorizer.embed(prompt)
273273

274274
await cache_with_ttl.astore(prompt, response, vector=vector)
275-
sleep(3)
275+
await asyncio.sleep(3)
276276

277277
check_result = await cache_with_ttl.acheck(vector=vector)
278278
assert len(check_result) == 0
@@ -442,7 +442,7 @@ async def test_async_updating_document(cache):
442442
check_result = await cache.acheck(prompt=prompt, return_fields=["updated_at"])
443443
key = check_result[0]["key"]
444444

445-
sleep(1)
445+
await asyncio.sleep(1)
446446

447447
metadata = {"foo": "bar"}
448448
await cache.aupdate(key=key, metadata=metadata)
@@ -479,7 +479,7 @@ async def test_async_ttl_expiration_after_update(cache_with_ttl, vectorizer):
479479
assert cache_with_ttl.ttl == 4
480480

481481
await cache_with_ttl.astore(prompt, response, vector=vector)
482-
sleep(5)
482+
await asyncio.sleep(5)
483483

484484
check_result = await cache_with_ttl.acheck(vector=vector)
485485
assert len(check_result) == 0

0 commit comments

Comments
 (0)