-
-
Notifications
You must be signed in to change notification settings - Fork 178
Graceful loop shutdown with running executor tasks #258
Comments
I think the first option is fine. But I'd like |
See http://stackoverflow.com/questions/32598231/asyncio-runtimeerror-event-loop-is-closed/32615276 for yet another example for the problem |
Thank you @asvetlov for your AsyncIO support on StackOverflow, a coworker told me he has found a solution from one of your answer ;-) |
I think throwing away the callback when the loop is already closed is fine (i.e., option 1). When closing a loop we throw away all callbacks that haven't run yet, so this would just extend that behavior. Now, arguably it is actually inconsistent that calling call_soon() on a closed loop raises an error at all -- why not just ignore the call? However, given that no further callbacks are run, we don't expect any further callbacks to be scheduled either, since only callbacks -- or coroutines, which is the same in this context -- should schedule callbacks. So it arguably doesn't matter much, and it does occasionally catch errors (I presume). But for call_soon_threadsafe() calls made from the executor it's a different story -- we explicitly don't wait for the workers to finish (since who knows when that might be) so we should expect such calls -- but we should treat them the same regardless of whether the thread finishe just before or just after the loop is closed. So option 1 is the right one. |
Pull Request would be welcome! |
Can I be helpful with that? :) I'd probably be able to implement the first option if I get some initial key points. So, for As for test case for this, I think it's clear. |
When I use my implementation of ThreadPool (uses threading.pool under the hood) an exception not raises. loop = asyncio.get_event_loop()
pool = ThreadPool(loop=loop)
loop.set_default_executor(pool)
# do something
pool.shutdown(wait=True)
loop.close() Also when I call
For uvloop the same.
This looks like special behaviour of |
Some tests from our stack fail because of this bug. We could mock the executor but since they are end to end tests ideally we shouldn't mock anything. What's the current status of this? Is anyone working on it? Do we have a reference in https://bugs.python.org/? |
I'm on my phone but a pr is open on python search for my login
Le mar. 24 oct. 2017 à 10:07, Manuel Miranda <notifications@github.com> a
écrit :
… Some tests from our stack fail because of this bug. We could mock the
executor but since they are end to end tests ideally we shouldn't mock
anything.
What's the current status of this? Is anyone working on it? Do we have a
reference in https://bugs.python.org/?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#258 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAVFXdbgPzjwuLVAhwRp3ILT1At4y7Bbks5svZrRgaJpZM4Ff4WE>
.
|
That PR is python/cpython#431. |
Thanks for the references! |
When I send a long task to tread-pool executor and immediately close the loop without waiting future returned by
.run_in_executor()
call I get exception report in logs:That's because task executed in thread has finished after loop closing. The task calls
call_soon_threadsafe()
(seefutures.wrap_future()
for details) and it fails on check for loop health.Due threaded nature it's hard to avoid situations like this: threads cannot be cancelled, we need cope with these even after loop closing. That's pretty common situation, especially for client-side jobs like web page fetching: DNS resolver is executed in thread pool also.
I see two options:
wrap_future
call, skipcall_soon_threadsafe()
for closed loopsrun_in_executor()
(while I don't see situations when I don't like the behavior, maybe flag should beTrue
by default).The text was updated successfully, but these errors were encountered: