Uploaded image for project: 'Motor'
  1. Motor
  2. MOTOR-1320

Runtime Error: Event loop is closed

    • Type: Icon: Question Question
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4
    • Component/s: asyncio
    • Python Drivers

      When I was testing my FastAPI application via pytest, I got an unexcepted RuntimeError.

      Traceback:

      > pytest --cov --cov-report=html -v -s --asyncio-mode=auto
      ====================== test session starts =======================
      platform linux – Python 3.12.3, pytest-8.2.1, pluggy-1.5.0 – /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/bin/python
      cachedir: .pytest_cache
      rootdir: /mnt/e/MicroServices/AuthPI
      configfile: pyproject.toml
      plugins: anyio-4.3.0, cov-5.0.0, asyncio-0.21.2
      asyncio: mode=Mode.AUTO
      collected 3 items                                                tests/test_management/test_userpool.py::test_get_userpools PASSED
      tests/test_management/test_userpool.py::test_create_userpool FAILED
      tests/test_utils.py::test_ping PASSED============================ FAILURES ============================
      ______________________ test_create_userpool ______________________    async def test_create_userpool():
              from main import app
              with TestClient(app) as client:
      >           resp = client.post('/management/userpool/', headers={
                      'su-token': SUPER_USER_TOKEN
                  }, json=TEST_USERPOOL)tests/test_management/test_userpool.py:85: 
      _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/testclient.py:633: in post
          return super().post(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:1145: in post
          return self.request(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/testclient.py:516: in request
          return super().request(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:827: in request
          return self.send(request, auth=auth, follow_redirects=follow_redirects)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:914: in send
          response = self._send_handling_auth(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:942: in _send_handling_auth
          response = self._send_handling_redirects(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:979: in _send_handling_redirects
          response = self._send_single_request(request)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/httpx/_client.py:1015: in _send_single_request
          response = transport.handle_request(request)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/testclient.py:398: in handle_request
          raise exc
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/testclient.py:395: in handle_request
          portal.call(self.app, scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/anyio/from_thread.py:288: in call
          return cast(T_Retval, self.start_task_soon(func, *args).result())
      /usr/lib/python3.12/concurrent/futures/_base.py:456: in result
          return self.__get_result()
      /usr/lib/python3.12/concurrent/futures/_base.py:401: in __get_result
          raise self._exception
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/anyio/from_thread.py:217: in _call_func
          retval = await retval_or_awaitable
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/fastapi/applications.py:1054: in _call_
          await super()._call_(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/applications.py:123: in _call_
          await self.middleware_stack(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/middleware/errors.py:186: in _call_
          raise exc
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/middleware/errors.py:164: in _call_
          await self.app(scope, receive, _send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/middleware/exceptions.py:65: in _call_
          await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py:64: in wrapped_app
          raise exc
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py:53: in wrapped_app
          await app(scope, receive, sender)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/routing.py:756: in _call_
          await self.middleware_stack(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/routing.py:776: in app
          await route.handle(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/routing.py:297: in handle
          await self.app(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/routing.py:77: in app
          await wrap_app_handling_exceptions(app, request)(scope, receive, send)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py:64: in wrapped_app
          raise exc
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py:53: in wrapped_app
          await app(scope, receive, sender)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/starlette/routing.py:72: in app
          response = await func(request)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/fastapi/routing.py:278: in app
          raw_response = await run_endpoint_function(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/fastapi/routing.py:191: in run_endpoint_function
          return await dependant.call(**values)
      routers/management/userpool.py:40: in create_user_pool
          return await engine.save(userpool)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/odmantic/engine.py:577: in save
          async with await self.client.start_session() as local_session:
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/motor/frameworks/asyncio/_init_.py:148: in _wrapper
          result = await f(self, *args, **kwargs)
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/motor/metaprogramming.py:75: in method
          return framework.run_on_executor(
      /home/xycode/.cache/pypoetry/virtualenvs/authpi-P9W-ssYy-py3.12/lib/python3.12/site-packages/motor/frameworks/asyncio/_init_.py:85: in run_on_executor
          return loop.run_in_executor(_EXECUTOR, functools.partial(fn, *args, **kwargs))
      /usr/lib/python3.12/asyncio/base_events.py:850: in run_in_executor
          self._check_closed()
      _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <_UnixSelectorEventLoop running=False closed=True debug=False>    def _check_closed(self):
              if self._closed:
      >           raise RuntimeError('Event loop is closed')
      E           RuntimeError: Event loop is closed/usr/lib/python3.12/asyncio/base_events.py:541: RuntimeError---------- coverage: platform linux, python 3.12.3-final-0 -----------
      Coverage HTML written to dir htmlcov==================== short test summary info =====================
      FAILED tests/test_management/test_userpool.py::test_create_userpool - RuntimeError: Event loop is closed
      ================== 1 failed, 2 passed in 3.11s ===================

       

      All code about this problem hosted on XYCode-Kerman/AuthPI at feature/test (github.com) commit 8b0713

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            xycode-xyc@outlook.com XYCode Kerman
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: