-
-
Notifications
You must be signed in to change notification settings - Fork 411
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cancel pending tasks #729
Cancel pending tasks #729
Conversation
Codecov Report
@@ Coverage Diff @@
## master #729 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 28 28
Lines 1851 1857 +6
=====================================
+ Hits 1851 1857 +6
Continue to review full report at Codecov.
|
Yeah I also struggled with this. We need to cancel all tasks after a certain timeout (maybe 30 seconds), however this function is being called from within a task so we need to ignore that one. Sleeping for 30 seconds isn't great either as it would keep the thread open. I think we should be counting the number of running tasks (not including the current task) every 0.1 seconds. If we get to 300 checks then they should be cancelled, if they all complete on their own then we should break out of the loop. As you said counting the number of running tasks is tricky though. Might be worth a stack overflow question? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I was giving some thought about this issue and started looking into how other projects seem to close/cancel tasks, do you think is it possible to do something like in the It seems they are adding all tasks( fut = ensure_future(coro, loop=self.loop)
self.futs.add(fut) Then when we try to close the slacker the method if self.futs:
yield from asyncio.gather(*self.futs, loop=self.loop)
yield from self.session.close() Perhaps we could add the Another thing we could do is change the |
Yeah that all sounds good. The only other thing to consider is that if someone's skill enters an infinite (or at least long running) loop that task will need to be cancelled too. Perhaps we should track called skill tasks as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just nicely cancel the tasks that are not yet done like this:
# The extensions have been given a chance to clean up
# gracefully in disconnect(). We can now cancel what we started.
pending = asyncio.Task.all_tasks()
for p in list(pending):
if not p.done() and p is not asyncio.current_task():
p.cancel()
That was my original plan @petri, however the function is called within a task and it cancels itself. I haven't managed to find a way of not cancelling itself. |
@jacobtomlinson recheck the code above. It's not canceling itself due to checking for |
Totally missed that! Nice! |
@FabioRosado could you please update this? |
cface4a
to
76eeac5
Compare
Just updated the PR, we will probably have an issue with testing as I need to test for |
2ad43af
to
b9ca23a
Compare
So in the end testing this was way easier than I was thinking. Should I merge this now @jacobtomlinson ? |
Description
This is not the right way that the issue 723 suggests we should approach this issue. But I was unsure how to get running tasks because
asyncio.Task.all_tasks
seems to return all tasks not only pending but also the ones that have been set as done or finished.I tried to search how to get only the pending tasks but couldn't figure out the best way to do this, since I couldn't figure out I couldn't create a condition to make opsdroid sleep for
x
amount of seconds before cancelling the task.This solution does cancel the task but I'm not sure if this is the best way to do things like I said before. Would like to get some more feedback on how to approach the issue in a better way 馃憤
Fixes #723
Status
READY|UNDER DEVELOPMENT| ON HOLDType of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.
Checklist: