Skip to content

Error: Event loop is closed, when trying t. #1056

@Aakarshit-Sharma19

Description

@Aakarshit-Sharma19

I am getting error when trying to get a value from a RedisCache. This error specifically comes in code when I am running the unit tests with starlette client in a starlette project. I'll mock the caching in the unit tests but still concerned if this error will crash the system.

FAILED [ 64%][2025-09-24 11:19:43,106] [ERROR] [exceptions:108]: RuntimeError('Event loop is closed')
Traceback (most recent call last):
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/connection.py", line 599, in read_response
response = await self._parser.read_response(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/_parsers/resp2.py", line 82, in read_response
response = await self._read_response(disable_decoding=disable_decoding)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/_parsers/resp2.py", line 90, in _read_response
raw = await self._readline()
^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/_parsers/base.py", line 283, in _readline
data = await self._stream.readline()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/python/lib/python3.12/asyncio/streams.py", line 565, in readline
line = await self.readuntil(sep)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/python/lib/python3.12/asyncio/streams.py", line 657, in readuntil
await self._wait_for_data('readuntil')
File "/path/to/python/lib/python3.12/asyncio/streams.py", line 542, in _wait_for_data
await self._waiter
RuntimeError: Task <Task pending name='anyio.from_thread.BlockingPortal._call_func' coro=<BlockingPortal._call_func() running at /path/to/venv/lib/python3.12/site-packages/anyio/from_thread.py:222> cb=[TaskGroup._spawn..task_done() at /path/to/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py:794]> got Future attached to a different loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/path/to/venv/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/swagger_ui.py", line 222, in call
await self.router(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 716, in call
await self.middleware_stack(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 736, in app
await route.handle(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 462, in handle
await self.app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 716, in call
await self.middleware_stack(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 766, in app
await self.default(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/swagger_ui.py", line 235, in default_fn
await self.app(original_scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/routing.py", line 154, in call
await self.router(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 716, in call
await self.middleware_stack(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 736, in app
await route.handle(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 462, in handle
await self.app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 716, in call
await self.middleware_stack(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 736, in app
await route.handle(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/starlette/routing.py", line 290, in handle
await self.app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/routing.py", line 48, in call
await self.next_app(original_scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/abstract.py", line 264, in call
return await operation(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/security.py", line 111, in call
await self.next_app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/abstract.py", line 264, in call
return await operation(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/request_validation.py", line 142, in call
await self.next_app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/abstract.py", line 264, in call
return await operation(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/lifespan.py", line 26, in call
await self.next_app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/abstract.py", line 264, in call
return await operation(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/context.py", line 25, in call
await self.next_app(scope, receive, send)
File "/path/to/venv/lib/python3.12/site-packages/connexion/middleware/abstract.py", line 264, in call
return await operation(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/apps/asynchronous.py", line 69, in call
response = await self.fn()
^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/decorators/main.py", line 188, in wrapper
return await decorated_function(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/v/lib/python3.12/site-packages/connexion/decorators/response.py", line 191, in wrapper
handler_response = await function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/decorators/parameter.py", line 115, in wrapper
return await function(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/connexion/decorators/main.py", line 173, in wrapper
return await function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/ccs_auth/access/access_filter.py", line 73, in wrapper
return await target_fun
^^^^^^^^^^^^^^^^
File "/path/to/project/src/controllers/_controller.py", line 93, in counts
cached_value = await api_cache.get(cache_key)
File "/path/to/venv/lib/python3.12/site-packages/aiocache/base.py", line 64, in _enabled
return await func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/aiocache/base.py", line 48, in _timeout
return await asyncio.wait_for(func(self, args, *kwargs), timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/python/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
return await fut
^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/aiocache/base.py", line 78, in _plugins
ret = await func(self, args, *kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/aiocache/base.py", line 199, in get
value = loads(await self._get(ns_key, encoding=self.serializer.encoding, _conn=_conn))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/aiocache/backends/redis.py", line 90, in _get
value = await self.client.get(key)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/client.py", line 677, in execute_command
return await conn.retry.call_with_retry(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/retry.py", line 50, in call_with_retry
return await do()
^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/client.py", line 652, in _send_command_parse_response
return await self.parse_response(conn, command_name, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/client.py", line 698, in parse_response
response = await connection.read_response()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/connection.py", line 619, in read_response
await self.disconnect(nowait=True)
File "/path/to/venv/lib/python3.12/site-packages/redis/asyncio/connection.py", line 479, in disconnect
self._writer.close() # type: ignore[union-attr]
^^^^^^^^^^^^^^^^^^^^
File "/path/to/python/lib/python3.12/asyncio/streams.py", line 355, in close
return self._transport.close()
^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/python/lib/python3.12/asyncio/selector_events.py", line 1206, in close
super().close()
File "/path/to/python/lib/python3.12/asyncio/selector_events.py", line 871, in close
self._loop.call_soon(self._call_connection_lost, None)
File "/path/to/python/lib/python3.12/asyncio/base_events.py", line 792, in call_soon
self._check_closed()
File "/path/to/python/lib/python3.12/asyncio/base_events.py", line 539, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions