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

Task was destroyed but it is pending #3047

Closed
littleningmeng opened this issue Aug 3, 2021 · 4 comments · Fixed by #3269
Closed

Task was destroyed but it is pending #3047

littleningmeng opened this issue Aug 3, 2021 · 4 comments · Fixed by #3269

Comments

@littleningmeng
Copy link

Hi, guys
My code works fine in FastAPI, but got errors below in Tornado !

ERROR:asyncio:Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last):
File "app.py", line 20, in
tornado.ioloop.IOLoop.current().start()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/home/qspace/python3.8.2/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
self._run_once()
File "/home/qspace/python3.8.2/lib/python3.8/asyncio/base_events.py", line 1851, in _run_once
handle._run()
File "/home/qspace/python3.8.2/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/ioloop.py", line 758, in _run_callback
ret = callback()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/http1connection.py", line 735, in _server_request_loop
ret = yield conn.read_response(request_delegate)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/http1connection.py", line 152, in read_response
return self._read_message(delegate)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 346, in wrapper
runner = Runner(result, future, yielded)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1080, in init
self.run()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1147, in run
yielded = self.gen.send(value)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/http1connection.py", line 238, in _read_message
delegate.finish()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/routing.py", line 256, in finish
self.delegate.finish()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/web.py", line 2195, in finish
self.execute()
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/web.py", line 2227, in execute
self.handler._execute(transforms, *self.path_args,
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 346, in wrapper
runner = Runner(result, future, yielded)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1078, in init
if self.handle_yield(first_yielded):
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1221, in handle_yield
self.future = convert_yielded(yielded)
File "/home/qspace/python3.8.2/lib/python3.8/functools.py", line 874, in wrapper
return dispatch(args[0].class)(*args, **kw)
File "/home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py", line 1361, in convert_yielded
return _wrap_awaitable(yielded)
task: <Task pending name='Task-350' coro=<MainHandler.get() running at app.py:8> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fa18ca65910>()] created at /home/qspace/python3.8.2/lib/python3.8/asyncio/base_events.py:422> cb=[IOLoop.add_future..() at /home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/ioloop.py:719] created at /home/qspace/.local/share/virtualenvs/qspace-BOV63HU7/lib/python3.8/site-packages/tornado/gen.py:1361>

@Python-37
Copy link
Contributor

Could you please paste your code

@littleningmeng
Copy link
Author

Could you please paste your code

image

But in FastAPI, the same code works fine
image

@bdarnell
Copy link
Member

Which version of Tornado are you using?

That snippet of code looks fine, but of course there's very little to it. The problem could be elsewhere. Are you trying to shut down your application or is it still running while this message is logged?

@littleningmeng
Copy link
Author

The Tornado version is 6.1
The asyncio's Streams is used in the GetRpcWfsFile function
And I did not do shutting down operations in it
Also the implemention of GetRpcWfsFile are the same in both Tornado and FastAPI, but error occurred only in Tornado

bdarnell added a commit to bdarnell/tornado that referenced this issue May 15, 2023
Per the warning in the asyncio documentation, we need to hold a strong
reference to all asyncio Tasks to prevent premature GC. Following
discussions in cpython (python/cpython#91887),
we hold these references on the IOLoop instance to ensure that they are
strongly held but do not cause leaks if the event loop itself is
discarded.

This is expected to fix all of the various "task was destroyed but
it is pending" warnings that have been reported. The
IOLoop._pending_tasks set is expected to become obsolete if
corresponding changes are made to asyncio in Python 3.13.

Fixes tornadoweb#3209
Fixes tornadoweb#3047
Fixes tornadoweb#2763

Some issues involve this warning as their most visible symptom,
but have an underlying cause that should still be addressed.
Updates tornadoweb#2914
Updates tornadoweb#2356
jack-hegman pushed a commit to jack-hegman/tornado that referenced this issue Aug 16, 2023
Per the warning in the asyncio documentation, we need to hold a strong
reference to all asyncio Tasks to prevent premature GC. Following
discussions in cpython (python/cpython#91887),
we hold these references on the IOLoop instance to ensure that they are
strongly held but do not cause leaks if the event loop itself is
discarded.

This is expected to fix all of the various "task was destroyed but
it is pending" warnings that have been reported. The
IOLoop._pending_tasks set is expected to become obsolete if
corresponding changes are made to asyncio in Python 3.13.

Fixes tornadoweb#3209
Fixes tornadoweb#3047
Fixes tornadoweb#2763

Some issues involve this warning as their most visible symptom,
but have an underlying cause that should still be addressed.
Updates tornadoweb#2914
Updates tornadoweb#2356

(cherry picked from commit bffed1a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants