From 4a2a583f5f5f51c547bf92e24190b235d3df10c5 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Thu, 6 Mar 2025 18:53:48 +0200 Subject: [PATCH] Ensures the connection is closed when the object is garbage collected. --- src/websockets/sync/connection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/websockets/sync/connection.py b/src/websockets/sync/connection.py index 8b9e06257..12046d7ea 100644 --- a/src/websockets/sync/connection.py +++ b/src/websockets/sync/connection.py @@ -226,6 +226,9 @@ def __exit__( else: self.close(CloseCode.INTERNAL_ERROR) + def __del__(self) -> None: + self.close() + def __iter__(self) -> Iterator[Data]: """ Iterate on incoming messages.