From 1a1c8594203ee1667282762490c8ec7739dd7103 Mon Sep 17 00:00:00 2001
From: Lena Biliaieva <lena.biliaieva@qt.io>
Date: Thu, 17 Sep 2026 13:46:12 +0200
Subject: [PATCH] QWebSocket: Check for an existing close timer before creating one

close() is called for every protocol error, so a peer sending malformed
frames could make it allocate an unbounded number of timers. Because
they all connect to the same disconnected signal, destroying them was
quadratic: ~470 KB of malformed frames on a single connection left the
event loop blocked for 37 minutes.

Reuse the existing timer instead. The bug where parsing continues after
a protocol error is addressed separately.

Task-number: QTBUG-150271
Change-Id: I4511d3bb3fc09ac3cd78f8d72677fff93da805ea
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 438981ce0e72f29a01b7daddd9f52fb73d0eb7d2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 0f9a4e27a0389301641f7bfe0308c876a246eade)
---

diff --git a/src/websockets/qwebsocket_p.cpp b/src/websockets/qwebsocket_p.cpp
index 56d32db..6f6d0cd 100644
--- a/src/websockets/qwebsocket_p.cpp
+++ b/src/websockets/qwebsocket_p.cpp
@@ -421,6 +421,8 @@
     }
     static constexpr auto closeTimerName = "_closeTimer"_L1;
     if (!m_isClosingHandshakeReceived) {
+        if (q->findChild<QTimer *>(closeTimerName))
+            return;
         QTimer *closeTimer = new QTimer(q);
         closeTimer->setObjectName(closeTimerName);
         closeTimer->setInterval(3000);
