You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The top one reports ERROR at teardown of test_mocksocket_mocker. The bottom one does not report any error. The only difference between them is that the bottom one requests the event_loop fixture.
The error message is quite long. It seems to be caused by MockSocket still being active when _provide_clean_event_loop() is called, and the new event loop failing at initialization.
Also, if the test case calls unittest.mock.patch instead of mocker.patch, then there is no error.
Attached is the test file, full console output, and package versions. This is running on Windows 10.
The event_loop fixture has been deprecated in v0.23 and removed in v1.0.0. Therefore, I will close this issue as obsolete.
This bug manifests when not using the event_loop fixture, so it being removed only exacerbates the issue.
From what I can see, the problem here is that the scope of the event loop intersects with mocker: after the mocker patches socket, the existing event loop is torn down and recreated while socket is still being mocked, causing the creation of event loop to fail. The old event_loop fixture somehow "nests" with the mocker fixture correctly, while not using the event_loop fixture made the event loop's life cycle conflict with the mocker fixture.
The workaround I used is to not use the mocker fixture either, and switch to unittest.mock and with mock.patch, which made the lifecycle of the mock fit entirely into the test function.
To properly fix this in pytest-asyncio, it's probably necessary to ensure the lifecycle of the event loop fully encompass everything inside the test function including fixtures.
To properly fix this in pytest-asyncio, it's probably necessary to ensure the lifecycle of the event loop fully encompass everything inside the test function including fixtures.
We'd like to do that, as it would solve other issues like #112, but it's not part of the v1.0 release. I'm reopening the issue :)
Activity
Remove all explicit event_loop and mocker arguments.
seifertm commentedon May 26, 2025
The
event_loop
fixture has been deprecated in v0.23 and removed in v1.0.0.Therefore, I will close this issue as obsolete.
twisteroidambassador commentedon May 26, 2025
This bug manifests when not using the event_loop fixture, so it being removed only exacerbates the issue.
From what I can see, the problem here is that the scope of the event loop intersects with mocker: after the mocker patches
socket
, the existing event loop is torn down and recreated while socket is still being mocked, causing the creation of event loop to fail. The oldevent_loop
fixture somehow "nests" with the mocker fixture correctly, while not using theevent_loop
fixture made the event loop's life cycle conflict with the mocker fixture.The workaround I used is to not use the
mocker
fixture either, and switch tounittest.mock
andwith mock.patch
, which made the lifecycle of the mock fit entirely into the test function.To properly fix this in pytest-asyncio, it's probably necessary to ensure the lifecycle of the event loop fully encompass everything inside the test function including fixtures.
seifertm commentedon May 26, 2025
@twisteroidambassador Thanks for the clarification!
We'd like to do that, as it would solve other issues like #112, but it's not part of the v1.0 release. I'm reopening the issue :)