Skip to content

Commit

Permalink
Properly handle asyncio.gather() call in SemanticCache.aheck (#263)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
abrookins authored Jan 23, 2025
1 parent 18d1cfd commit 0dff967
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion redisvl/extensions/llmcache/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ async def acheck(
cache_search_results, return_fields # type: ignore
)
# Extend TTL on keys
asyncio.gather(*[self._async_refresh_ttl(key) for key in redis_keys])
await asyncio.gather(*[self._async_refresh_ttl(key) for key in redis_keys])

return cache_hits

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_llmcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ async def test_async_ttl_expiration(cache_with_ttl, vectorizer):
vector = vectorizer.embed(prompt)

await cache_with_ttl.astore(prompt, response, vector=vector)
sleep(3)
await asyncio.sleep(3)

check_result = await cache_with_ttl.acheck(vector=vector)
assert len(check_result) == 0
Expand Down Expand Up @@ -442,7 +442,7 @@ async def test_async_updating_document(cache):
check_result = await cache.acheck(prompt=prompt, return_fields=["updated_at"])
key = check_result[0]["key"]

sleep(1)
await asyncio.sleep(1)

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

await cache_with_ttl.astore(prompt, response, vector=vector)
sleep(5)
await asyncio.sleep(5)

check_result = await cache_with_ttl.acheck(vector=vector)
assert len(check_result) == 0
Expand Down

0 comments on commit 0dff967

Please sign in to comment.