Skip to content

Commit

Permalink
websocket: Add warning if client connection isn't closed cleanly
Browse files Browse the repository at this point in the history
This gives a warning that is not dependent on GC for the issue
in tornadoweb#3257.
  • Loading branch information
bdarnell committed May 7, 2023
1 parent e0fa53e commit 4fcaa67
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tornado/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import struct
import tornado
from urllib.parse import urlparse
import warnings
import zlib

from tornado.concurrent import Future, future_set_result_unless_cancelled
Expand Down Expand Up @@ -1410,6 +1411,15 @@ def __init__(
104857600,
)

def __del__(self) -> None:
if self.protocol is not None and asyncio.get_event_loop().get_debug():
# Unclosed client connections can sometimes log "task was destroyed but
# was pending" warnings if shutdown strikes at the wrong time (such as
# while a ping is being processed due to ping_interval). Log our own
# warning to make it a little more deterministic (although it's still
# dependent on GC timing).
warnings.warn("Unclosed WebSocketClientConnection", ResourceWarning)

def close(self, code: Optional[int] = None, reason: Optional[str] = None) -> None:
"""Closes the websocket connection.
Expand Down

0 comments on commit 4fcaa67

Please sign in to comment.