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

"RuntimeError: Session is closed" exception raised on request after expiration reached #26

Open
dfuhry opened this issue Feb 23, 2023 · 2 comments
Assignees

Comments

@dfuhry
Copy link

dfuhry commented Feb 23, 2023

When I use the same AsyncCacheControl() object to request a cacheable URL a few times before & then after it expires, I receive a "RuntimeError: Session is closed" error. A minimal script reproducing the error is below. Every 5 seconds it makes a request to a URL which expires after 20 seconds:

import acachecontrol
import asyncio

async def main():
    cached_session = acachecontrol.AsyncCacheControl()

    URL = 'http://httpbin.org/cache/20'

    while True:
        print(f"Making request to {URL}")
        async with cached_session as cached_session_instance:
            async with cached_session_instance.get(URL) as resp:
                await resp.json()
        await asyncio.sleep(5)

asyncio.run(main())

Script output is that it works for the first 4 requests, then fails on the 5th:

Making request to http://httpbin.org/cache/20
Making request to http://httpbin.org/cache/20
Making request to http://httpbin.org/cache/20
Making request to http://httpbin.org/cache/20
Making request to http://httpbin.org/cache/20
Traceback (most recent call last):
  File ".../test_acachecontrol2.py", line 16, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File ".../test_acachecontrol2.py", line 12, in main
    async with cached_session_instance.get(URL) as resp:
  File "/usr/local/lib/python3.9/dist-packages/acachecontrol/request_context_manager.py", line 40, in __aenter__
    async with self.client_session.request(
  File "/usr/lib/python3/dist-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/usr/lib/python3/dist-packages/aiohttp/client.py", line 381, in _request
    raise RuntimeError("Session is closed")
RuntimeError: Session is closed
@MasterSergius
Copy link
Owner

@dfuhry Thank you for posting this issue. Sorry for such long delay (because of awful war), we'll take a look. Btw, have you had a chance to fix it on your own?

@MasterSergius
Copy link
Owner

To unblock you I can suggest using one session for all requests:

import acachecontrol
import asyncio

async def main():
    cached_session = acachecontrol.AsyncCacheControl()

    URL = 'http://httpbin.org/cache/20'

    async with cached_session as cached_session_instance:
        while True:
            print(f"Making request to {URL}")
            async with cached_session_instance.get(URL) as resp:
                data = await resp.json()
            await asyncio.sleep(5)

asyncio.run(main())

Meanwhile, we'll investigate the issue and find a solution

@MasterSergius MasterSergius self-assigned this Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants