Skip to content

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

Description

@dfuhry

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions