memoize icon indicating copy to clipboard operation
memoize copied to clipboard

Missing exception details on concurrent calls

Open omerfarukdogan opened this issue 1 year ago • 0 comments

There is an inconsistency about the exceptions raised from the memoized functions. When a function is called concurrently and it raises an exception, only the first caller receives the exception details. Remaining callers receive CachedMethodFailedExceptions but they don't contain any information about the actual exception. This prevents the callers from handling the exceptions accordingly.

Here is a small code to reproduce the issue:

from datetime import timedelta
from memoize.wrapper import memoize
from memoize.configuration import MutableCacheConfiguration
from memoize.entrybuilder import ProvidedLifeSpanCacheEntryBuilder
from memoize.eviction import NoEvictionStrategy
from memoize.key import EncodedMethodReferenceAndArgsKeyExtractor
from memoize.storage import LocalInMemoryCacheStorage
from asyncio import sleep, gather

@memoize(configuration=MutableCacheConfiguration(
    configured=True,
    storage=LocalInMemoryCacheStorage(),
    key_extractor=EncodedMethodReferenceAndArgsKeyExtractor(),
    method_timeout=timedelta(hours=1),
    entry_builder=ProvidedLifeSpanCacheEntryBuilder(update_after=timedelta(hours=1), expire_after=timedelta(hours=1)),
    eviction_strategy=NoEvictionStrategy(),
))
async def test():
    await sleep(1)
    raise Exception("test")


results = await gather(test(), test(), test(), test(), return_exceptions=True)

print(results)

Output:

[CachedMethodFailedException('Refresh failed to complete', Exception('test')), CachedMethodFailedException('Concurrent refresh failed to complete'), CachedMethodFailedException('Concurrent refresh failed to complete'), CachedMethodFailedException('Concurrent refresh failed to complete')]

omerfarukdogan avatar Mar 21 '24 06:03 omerfarukdogan