Skip to content

Commit cb89061

Browse files
authored
PYTHON-4537 - Use selector asyncio loop on windows tests (#1748)
1 parent afd0b6f commit cb89061

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

pymongo/asynchronous/periodic_executor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ def __repr__(self) -> str:
6565
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"
6666

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

7080
def open(self) -> None:
7181
"""Start. Multiple calls have no effect.

pymongo/synchronous/periodic_executor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ def __repr__(self) -> str:
6565
return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>"
6666

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

7080
def open(self) -> None:
7181
"""Start. Multiple calls have no effect.

test/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@
7979

8080
_IS_SYNC = True
8181

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

8393
class ClientContext:
8494
client: MongoClient

test/asynchronous/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@
7979

8080
_IS_SYNC = False
8181

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

8393
class AsyncClientContext:
8494
client: AsyncMongoClient

0 commit comments

Comments
 (0)