WIPACrepo / wipac-telemetry

WIPAC Telemetry: Monitoring/Tracing Applications, Supporting Infrastructures, and Services
MIT License
0 stars 0 forks source link

Event Loop(s) Issue, Possibly Nested #29

Closed ric-evans closed 3 years ago

ric-evans commented 3 years ago

In a branch of https://github.com/WIPACrepo/mou-dashboard, on a call to asyncio.get_event_loop().run_until_complete, OTEL seems to be the cause of an error (RuntimeError: This event loop is already running).

@dsschult: "another call to asyncio.run or loop.run_until_complete somewhere. and you can't have two of them"

There doesn't seem to be any of these calls. Further investigation is needed. It could be a consequence of spanning over the RestHandler's async _execute() method.


Example: https://app.circleci.com/pipelines/github/WIPACrepo/mou-dashboard/252/workflows/27d162fe-ce0b-48a0-a262-5d68c4f5cab7/jobs/554, the second-to-last command, line 1904

File "/home/circleci/project/rest_server/databases/mou_db.py", line 33, in __init__
    asyncio.get_event_loop().run_until_complete(self._ensure_all_db_indexes())
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 592, in run_until_complete
    self._check_running()
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 552, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
dsschult commented 3 years ago

Found it. https://github.com/WIPACrepo/mou-dashboard/blob/0900f12ff66512a2c9903cfb88d02d0364c8bafd/tests/unit/test_rest_server.py#L243

Inside an async test (so it already started a loop and is an async function) you called db_utils.MoUMotorClient(mock_mongo), which is a sync function that calls run_until_complete. So that's async -> sync -> async, which is a no-no.

ric-evans commented 3 years ago

Found it. WIPACrepo/mou-dashboard@0900f12/tests/unit/test_rest_server.py#L243

Inside an async test (so it already started a loop and is an async function) you called db_utils.MoUMotorClient(mock_mongo), which is a sync function that calls run_until_complete. So that's async -> sync -> async, which is a no-no.

Good catch. I'm surprised this issue didn't come up for previous releases. Perhaps it was skipping the test, instead of failing.