@@ -81,7 +81,15 @@ def safe_watcher():
8181 yield
8282 return
8383
84- loop = policy .get_event_loop ()
84+ try :
85+ loop = policy .get_event_loop ()
86+ if loop .is_closed ():
87+ raise RuntimeError ("open a new one" )
88+ except RuntimeError :
89+ # closed loop, make a new one
90+ loop = policy .new_event_loop ()
91+ policy .set_event_loop (loop )
92+
8593 try :
8694 watcher = asyncio .SafeChildWatcher ()
8795 watcher .attach_loop (loop )
@@ -236,9 +244,19 @@ async def _stream_communicate(process, cell):
236244 await asyncio .wait ([stdout_task , stderr_task ])
237245 await process .wait ()
238246
239- if sys .platform .startswith ("win" ):
240- asyncio .set_event_loop_policy (asyncio .WindowsProactorEventLoopPolicy ())
241- loop = asyncio .get_event_loop_policy ().get_event_loop ()
247+ policy = asyncio .get_event_loop_policy ()
248+ if sys .platform .startswith ("win" ) and not isinstance (
249+ policy , asyncio .WindowsProactorEventLoopPolicy
250+ ):
251+ # _do not_ overwrite the current policy
252+ policy = asyncio .WindowsProactorEventLoopPolicy ()
253+
254+ try :
255+ loop = policy .get_event_loop ()
256+ except RuntimeError :
257+ # closed loop, make a new one
258+ loop = policy .new_event_loop ()
259+ policy .set_event_loop (loop )
242260 argv = arg_split (line , posix = not sys .platform .startswith ("win" ))
243261 args , cmd = self .shebang .parser .parse_known_args (argv )
244262 try :
0 commit comments