Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError when using cached_stampede #389

Closed
borisnaydis opened this issue Mar 16, 2018 · 2 comments
Closed

UnboundLocalError when using cached_stampede #389

borisnaydis opened this issue Mar 16, 2018 · 2 comments
Labels

Comments

@borisnaydis
Copy link

borisnaydis commented Mar 16, 2018

The @cached_stampede decorator throws UnboundLocalError when a decorated function is called multiple times when cache is empty.

Error:

Error handling request
Traceback (most recent call last):
  File "C:\Git\test_project\.venv\Lib\site-packages\aiohttp\web_protocol.py", line 410, in start
    resp = yield from self._request_handler(request)
  File "C:\Git\test_project\.venv\Lib\site-packages\aiohttp\web.py", line 325, in _handle
    resp = yield from handler(request)
  File "C:\Git\test_project\.venv\Lib\site-packages\aiohttp\web_middlewares.py", line 93, in impl
    return (yield from handler(request))
  File "<ipython-input-1-bfa0dca86818>", line 15, in test
    await test_func(val)
  File "C:\Git\test_project\.venv\Lib\site-packages\aiocache\decorators.py", line 69, in wrapper
    return await self.decorator(f, *args, **kwargs)
  File "C:\Git\test_project\.venv\Lib\site-packages\aiocache\decorators.py", line 170, in decorator
    return result
UnboundLocalError: local variable 'result' referenced before assignment

Steps to reproduce:

  1. Start application
import asyncio
from aiocache import cached_stampede
from aiohttp import web

@cached_stampede(ttl=5, lease=1)
async def test_func(val):
    await asyncio.sleep(5)

async def test(request):
    val = request.match_info['val']
    await test_func(val)
    return web.Response(text=f"Val is {val}")

app = web.Application()
app.router.add_get('/{val}', test)
web.run_app(app)
  1. Go to http://localhost:8080/anything_you_like

  2. Hit the F5 key fast several times.

@argaen
Copy link
Member

argaen commented Mar 16, 2018

Yup, was able to reproduce. Thanks for reporting!

@argaen argaen added the bug label Mar 16, 2018
@argaen
Copy link
Member

argaen commented Apr 4, 2018

Hey @borisnaydis Ive (well @pfreixes) found the cause. Basically this is happening because when hitting F5 in the same browser tab, previous request is cancelled and leads to an unexpected path which raises this unbound result exception. I take two actions from here:

  • I will make this path play nicer, will raise the Cancelled Error in case this happens so instead an unbound error, we will se a cancelled error (which is expected)
  • I will add a new attribute to shield the real function calls from being cancelled add shield=True to cached_stampede #393

PS: If you this in different browser tabs or background curl queries behavior is expected, the bug is only when the task gets cancelled.

argaen added a commit that referenced this issue Apr 4, 2018
__aexit__ was returning whether asyncio Event was removed or not. In some cases this was avoiding the context manager to propagate exceptions happening inside. Now its not returning anything and will raise always any exception raised from inside
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants