Skip to content

Commit

Permalink
Fix osrf.py_common.process_utils.get_loop() implementation (#70) (#75)
Browse files Browse the repository at this point in the history
On Windows, avoid loops closing during garbage collection
and reuse existing ones if possible.

Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic authored Jun 29, 2021
1 parent f06364b commit 3c1b5f5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions osrf_pycommon/process_utils/get_loop_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ def get_loop_impl(asyncio):
return asyncio.get_event_loop()
# Setup this thread's loop and return it
if os.name == 'nt':
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
try:
loop = asyncio.get_event_loop()
if not isinstance(loop, asyncio.ProactorEventLoop):
# Before replacing the existing loop, explicitly
# close it to prevent an implicit close during
# garbage collection, which may or may not be a
# problem depending on the loop implementation.
loop.close()
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
except (RuntimeError, AssertionError):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
try:
loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 3c1b5f5

Please sign in to comment.