Skip to content

PYTHON-4537 - Use selector asyncio loop on windows tests #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pymongo/asynchronous/periodic_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ def __repr__(self) -> str:
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"

def _run_async(self) -> None:
asyncio.run(self._run()) # type: ignore[func-returns-value]
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self._run()) # type: ignore[func-returns-value]
finally:
loop.close()
else:
asyncio.run(self._run()) # type: ignore[func-returns-value]

def open(self) -> None:
"""Start. Multiple calls have no effect.
Expand Down
12 changes: 11 additions & 1 deletion pymongo/synchronous/periodic_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ def __repr__(self) -> str:
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"

def _run_async(self) -> None:
asyncio.run(self._run()) # type: ignore[func-returns-value]
# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if sys.platform == "win32":
loop = asyncio.SelectorEventLoop()
try:
loop.run_until_complete(self._run()) # type: ignore[func-returns-value]
finally:
loop.close()
else:
asyncio.run(self._run()) # type: ignore[func-returns-value]

def open(self) -> None:
"""Start. Multiple calls have no effect.
Expand Down
10 changes: 10 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@

_IS_SYNC = True

# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]


class ClientContext:
client: MongoClient
Expand Down
10 changes: 10 additions & 0 deletions test/asynchronous/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@

_IS_SYNC = False

# The default asyncio loop implementation on Windows
# has issues with sharing sockets across loops (https://github.com/python/cpython/issues/122240)
# We explicitly use a different loop implementation here to prevent that issue
if (
not _IS_SYNC
and sys.platform == "win32"
and asyncio.get_event_loop_policy() == asyncio.WindowsProactorEventLoopPolicy
):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]


class AsyncClientContext:
client: AsyncMongoClient
Expand Down
Loading