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

websocket: Task was destroyed but it is pending! #2763

Closed
gdbrianlu opened this issue Oct 29, 2019 · 5 comments · Fixed by #3269
Closed

websocket: Task was destroyed but it is pending! #2763

gdbrianlu opened this issue Oct 29, 2019 · 5 comments · Fixed by #3269

Comments

@gdbrianlu
Copy link

[E 191029 10:54:51 base_events:1608] Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketProtocol13._receive_frame_loop() done, defined at D:\AndroidControl\Python37\lib\site-packages\tornado\websocket.py:1115> wait_for=<Future pending cb=[IOLoop.add_future..() at D:\AndroidControl\Python37\lib\site-packages\tornado\ioloop.py:690, <TaskWakeupMethWrapper object at 0x000002AC48174468>()]> cb=[IOLoop.add_future..() at D:\AndroidControl\Python37\lib\site-packages\tornado\ioloop.py:690]>

@bdarnell
Copy link
Member

If this message occurs while the process is shutting down, it's generally harmless - it just means that the process is only partially cleaning up after itself. I recommend ignoring it, but if you want to work towards a cleaner shutdown, you need to close all your websocket connections before stopping the event loop.

This message comes from asyncio rather than Tornado, so there's not much Tornado can do to control it.

@afzoun
Copy link

afzoun commented Nov 24, 2019

If this message occurs while the process is shutting down, it's generally harmless - it just means that the process is only partially cleaning up after itself. I recommend ignoring it, but if you want to work towards a cleaner shutdown, you need to close all your websocket connections before stopping the event loop.

This message comes from asyncio rather than Tornado, so there's not much Tornado can do to control it.

I have the same RuntimeWarning, but it doesn't occur only on shut down. I get this warning constantly while my websocket is open. I know it comes from asyncio, but the Tornado is talking to asyncio.

I am using tornado 6.0.3 with python 3.7

@bdarnell
Copy link
Member

Hmm. There's only one receive_frame_loop call per websocket connection, so it shouldn't be possible to see this more than once while the connection is alive. Are you sure something's not closing and reopening the connection? But even if it is, it's not expected to see this message at any time but shutdown. Can you provide any kind of reproducible test case?

Are you also on windows? There are reports of issues with websockets on windows that are not understood: #2136.

@bdarnell bdarnell reopened this Nov 24, 2019
@bdarnell bdarnell changed the title Task was destroyed but it is pending! websocket: Task was destroyed but it is pending! Nov 24, 2019
@tankienleong
Copy link

Hi @bdarnell , I faced the same problem as @afzoun.

you can reproduce the error by running the cam_server.py.

Please rename the index.txt to index.html (github does not allow me upload html file)
index.txt

Please rename the cam_server.txt to cam_server.py (github does not allow me upload python file)
cam_server.txt

@ploxiln
Copy link
Contributor

ploxiln commented Jan 1, 2020

@tankienleong in your example you call write_message() on CamWSHandler instances from the gstreamer callback pull_frame(), which runs in a different thread than the ioloop of the async http/websocket server. That is a problem, you are not supposed to do that. The only thing you can do to interact with tornado stuff from other threads, is call the appropriate ioloop's add_callback() method, and then do the other tornado related calls in that callback. I think that AnyThreadEventLoopPolicy does not really help you here.

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.

5 participants