Skip to content

Commit 3c1b5f5

Browse files
authored
Fix osrf.py_common.process_utils.get_loop() implementation (#70) (#75)
On Windows, avoid loops closing during garbage collection and reuse existing ones if possible. Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
1 parent f06364b commit 3c1b5f5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

osrf_pycommon/process_utils/get_loop_impl.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ def get_loop_impl(asyncio):
2424
return asyncio.get_event_loop()
2525
# Setup this thread's loop and return it
2626
if os.name == 'nt':
27-
loop = asyncio.ProactorEventLoop()
28-
asyncio.set_event_loop(loop)
27+
try:
28+
loop = asyncio.get_event_loop()
29+
if not isinstance(loop, asyncio.ProactorEventLoop):
30+
# Before replacing the existing loop, explicitly
31+
# close it to prevent an implicit close during
32+
# garbage collection, which may or may not be a
33+
# problem depending on the loop implementation.
34+
loop.close()
35+
loop = asyncio.ProactorEventLoop()
36+
asyncio.set_event_loop(loop)
37+
except (RuntimeError, AssertionError):
38+
loop = asyncio.ProactorEventLoop()
39+
asyncio.set_event_loop(loop)
2940
else:
3041
try:
3142
loop = asyncio.get_event_loop()

0 commit comments

Comments
 (0)