New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BF: Save and restore existing asyncio event-loop #5106
Conversation
That brings back the BlockingIOError messages :/
|
I revisited some of the python bug reports about BlockingIOError. I didn't gain much clarity, but somewhere in the followup searching I came across a commit that seems to work around the same issue. It involves cleaning up with (*) I tried each recipe in gh-5064 with python 3.7.3 and ipython 7.19.0 installed in the virtualenv, not system wide, but was unable to trigger the failure. patchdiff --git a/datalad/cmd.py b/datalad/cmd.py
index d861bcd8c..74df00f41 100644
--- a/datalad/cmd.py
+++ b/datalad/cmd.py
@@ -476,21 +476,24 @@ def run(self, cmd, protocol=None, stdin=None, cwd=None, env=None, **kwargs):
else:
event_loop = asyncio.SelectorEventLoop()
asyncio.set_event_loop(event_loop)
- # include the subprocess manager in the asyncio event loop
- results = event_loop.run_until_complete(
- run_async_cmd(
- event_loop,
- cmd,
- protocol,
- stdin,
- protocol_kwargs=kwargs,
- cwd=cwd,
- env=env,
+ try:
+ # include the subprocess manager in the asyncio event loop
+ results = event_loop.run_until_complete(
+ run_async_cmd(
+ event_loop,
+ cmd,
+ protocol,
+ stdin,
+ protocol_kwargs=kwargs,
+ cwd=cwd,
+ env=env,
+ )
)
- )
- # terminate the event loop, cannot be undone, hence we start a fresh
- # one each time (see BlockingIOError notes above)
- event_loop.close()
+ finally:
+ asyncio.set_event_loop(None)
+ # terminate the event loop, cannot be undone, hence we start a fresh
+ # one each time (see BlockingIOError notes above)
+ event_loop.close()
# log before any exception is raised
lgr.log(8, "Finished running %r with status %s", cmd, results['code']) |
This approach is copied from IPython. It prevents the crash reported in dataladgh-5064 by not leaving behind a closed event-loop, but rather any previously existing one, or `None`. Fixes dataladgh-5064
without flooding the terminal with messages. datalad#5106 (comment) @mih cannot trigger the ipython crash with this change anymore (but replicated on plain `maint` before)
Codecov Report
@@ Coverage Diff @@
## maint #5106 +/- ##
==========================================
+ Coverage 89.90% 89.92% +0.02%
==========================================
Files 294 294
Lines 40953 40955 +2
==========================================
+ Hits 36817 36829 +12
+ Misses 4136 4126 -10
Continue to review full report at Codecov.
|
Even though I still don't understand this properly, it looks good now:
I will merge! Thx @kyleam ! |
This approach is copied from IPython. It prevents the crash reported in
gh-5064 by not leaving behind a closed event-loop, but rather any
previously existing one, or
None
.Fixes gh-5064