File tree Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Expand file tree Collapse file tree 4 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -65,7 +65,17 @@ def __repr__(self) -> str:
65
65
return f"<{ self .__class__ .__name__ } (name={ self ._name } ) object at 0x{ id (self ):x} >"
66
66
67
67
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]
69
79
70
80
def open (self ) -> None :
71
81
"""Start. Multiple calls have no effect.
Original file line number Diff line number Diff line change @@ -65,7 +65,17 @@ def __repr__(self) -> str:
65
65
return f"<{ self .__class__ .__name__ } (name={ self ._name } ) object at 0x{ id (self ):x} >"
66
66
67
67
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]
69
79
70
80
def open (self ) -> None :
71
81
"""Start. Multiple calls have no effect.
Original file line number Diff line number Diff line change 79
79
80
80
_IS_SYNC = True
81
81
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
+
82
92
83
93
class ClientContext :
84
94
client : MongoClient
Original file line number Diff line number Diff line change 79
79
80
80
_IS_SYNC = False
81
81
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
+
82
92
83
93
class AsyncClientContext :
84
94
client : AsyncMongoClient
You can’t perform that action at this time.
0 commit comments