Skip to content
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

ServiceThread complains Task was destroyed but it is pending! #38

Closed
2 tasks done
kevin-ip opened this issue Dec 6, 2022 · 2 comments · Fixed by #39
Closed
2 tasks done

ServiceThread complains Task was destroyed but it is pending! #38

kevin-ip opened this issue Dec 6, 2022 · 2 comments · Fixed by #39
Labels
bug Something isn't working

Comments

@kevin-ip
Copy link

kevin-ip commented Dec 6, 2022

Checklist

  • I have included information about relevant versions
  • I have verified that the issue persists when using the master branch of Mode.

Steps to reproduce

I have a scheduled job that start and stop a ServiceThread but it complains "Task was destroyed but it is pending!"
The following script simulates the scheduled job.

import asyncio

from mode import Service
from mode.threads import ServiceThread


class T(ServiceThread):
    @Service.task
    async def poll_thread(self):
        while not self.should_stop:
            await asyncio.sleep(0)
            print("hello")


async def run():
    t = T()
    await t.start()
    await t.stop()

if __name__ == '__main__':
    for _ in range(3):
        asyncio.run(run())

Expected behavior

The script should only print "hello"

Actual behavior

It complains "Task was destroyed but it is pending!"

Full traceback

hello
hello
Task was destroyed but it is pending!
task: <Task pending name='Task-11' coro=<ServiceThread._wakeup_timer_in_thread() done, defined at /Users/kevinip/code/github/faust-streaming/mode/mode/threads.py:190> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[_chain_future.<locals>._call_set_state() at /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/futures.py:394]>
hello
hello
hello
hello
Task was destroyed but it is pending!
task: <Task pending name='Task-27' coro=<ServiceThread._wakeup_timer_in_thread() done, defined at /Users/kevinip/code/github/faust-streaming/mode/mode/threads.py:190> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[_chain_future.<locals>._call_set_state() at /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/futures.py:394]>
Task was destroyed but it is pending!
task: <Task pending name='Task-41' coro=<ServiceThread._wakeup_timer_in_thread() done, defined at /Users/kevinip/code/github/faust-streaming/mode/mode/threads.py:190> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[_chain_future.<locals>._call_set_state() at /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/futures.py:394]>

Versions

  • Python version = Python 3.11.0
  • Mode version = Master, commit f96657f
  • Operating system = Mac OS Monterey v12.6 Intel Processor
@wbarnha wbarnha added the bug Something isn't working label Dec 7, 2022
@jalaziz
Copy link

jalaziz commented Dec 7, 2022

I believe the issue here is that the thread loop is not properly shutdown. If you look at asyncio.run there are several steps to properly closing a loop. Unfortunately, the shutdown steps are not available in a convenient standalone method. It's a bit more re-usable in 3.11 though.

Another option would be to ensure _wakeup_timer_in_thread is awaited on shutdown to give it a chance to get scheduled.

@wbarnha
Copy link
Member

wbarnha commented Dec 13, 2022

Give #39 a look, should fix the issue you see.

lqhuang pushed a commit that referenced this issue Dec 16, 2022
…_thread` (#39)


* fix import logic again to address #38

* fix thread test import Event

* fix thread test import Event

* commit potential fix?

* replace asyncio.sleep with self.sleep

* readd get_event_loop_policy()

* readd loop kwargs until we figure out what to do

* fix server test import

* fix erroneous loops because im illiterate

* Add changes by @lqhuang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants