# Buggy code
async def wrapper(*args, **kwargs):
    key = tuple([args, tuple([tuple([k, kwargs[k]]) for k in kwargs])])
    cache[key] = cache.get(key) or asyncio.create_task(async_fn(*args, **kwargs))
    try:
        return await cache[key]
    finally:
        cache.pop(key, None)


# Traceback (concurrent.futures._base.CancelledError)
2019-05-16 15:14:35,679 INFO     lbrynet.dht.blob_announcer:51: announced 5 blobs
2019-05-16 15:15:04,419 INFO     torba.client.baseledger:133: lbc_mainnet: added BlockHeightEvent(height=568534, change=1) header blocks, final height 568534
2019-05-16 15:15:21,629 INFO     lbrynet.blob_exchange.client:137: downloaded 4af012f8 from 18.213.255.15:5567
2019-05-16 15:15:21,652 INFO     lbrynet.stream.managed_stream:358: write blob 8/76
2019-05-16 15:15:28,815 INFO     torba.client.baseledger:133: lbc_mainnet: added BlockHeightEvent(height=568535, change=1) header blocks, final height 568535
2019-05-16 15:16:24,383 ERROR    lbrynet.wallet.resolve:42:
Traceback (most recent call last):
File "lbrynet/wallet/resolve.py", line 38, in resolve
File "lbrynet/wallet/resolve.py", line 67, in _handle_resolutions
File "lbrynet/wallet/resolve.py", line 109, in _handle_resolve_uri_response
File "lbrynet/wallet/resolve.py", line 172, in parse_and_validate_claim_result
concurrent.futures._base.CancelledError
2019-05-16 15:16:24,391 WARNING  lbrynet.extras.daemon.Daemon:914: Error downloading mp3: Failed to resolve "mp3" within the timeout
2019-05-16 15:16:31,259 ERROR    lbrynet.wallet.resolve:42:
Traceback (most recent call last):
File "lbrynet/wallet/resolve.py", line 38, in resolve
File "lbrynet/wallet/resolve.py", line 67, in _handle_resolutions
File "lbrynet/wallet/resolve.py", line 109, in _handle_resolve_uri_response
File "lbrynet/wallet/resolve.py", line 172, in parse_and_validate_claim_result
concurrent.futures._base.CancelledError
2019-05-16 15:16:31,267 WARNING  lbrynet.extras.daemon.Daemon:914: Error downloading mp3: 'str' object has no attribute 'get'
2019-05-16 15:16:39,715 ERROR    lbrynet.wallet.resolve:42:
Traceback (most recent call last):
File "lbrynet/wallet/resolve.py", line 38, in resolve
File "lbrynet/wallet/resolve.py", line 67, in _handle_resolutions
File "lbrynet/wallet/resolve.py", line 109, in _handle_resolve_uri_response
File "lbrynet/wallet/resolve.py", line 172, in parse_and_validate_claim_result
concurrent.futures._base.CancelledError
2019-05-16 15:16:39,728 WARNING  lbrynet.extras.daemon.Daemon:914: Error downloading mp3: 'str' object has no attribute 'get'
2019-05-16 15:18:36,193 INFO     torba.client.baseledger:133: lbc_mainnet: added BlockHeightEvent(height=568536, change=1) header blocks, final height 568536

# Fixed code
def wrapper(async_fn):
    @functools.wraps(async_fn)
    async def _inner(*args, **kwargs):
        key = tuple([args, tuple([tuple([k, kwargs[k]]) for k in kwargs])])
        if key in lru_cache:
            return lru_cache.get(key)

        concurrent_cache[key] = concurrent_cache.get(key) or asyncio.create_task(
            async_fn(*args, **kwargs)
        )

        try:
            result = await concurrent_cache[key]
            lru_cache.set(key, result)
            return result
        finally:
            concurrent_cache.pop(key, None)

    return _inner