From c8e846dcf85d5facec24bf5a33cb65c9551039eb Mon Sep 17 00:00:00 2001 From: Tobias Krais Date: Thu, 29 Oct 2026 12:36:14 +0100 Subject: [PATCH] Media-Tracking: Verbindungsabbruch bei grossen Dateien abfangen (MySQL has gone away) statt Log-Fehler zu erzeugen --- lib/media/MediaRequest.php | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/media/MediaRequest.php b/lib/media/MediaRequest.php index c0a1176..8c5b5cc 100644 --- a/lib/media/MediaRequest.php +++ b/lib/media/MediaRequest.php @@ -133,6 +133,41 @@ public function isMedia(): bool * @throws rex_sql_exception */ public function save(): void + { + // Media tracking runs in MEDIA_MANAGER_AFTER_SEND, i.e. after the file was already + // streamed to the client. For large files (e.g. video downloads / range requests) the + // transfer can outlive MySQL's wait_timeout, so the connection may be gone by the time + // this counter UPDATE runs ("MySQL server has gone away", SQLSTATE[HY000] 2006). + // One reconnect attempt covers that case; any remaining failure is swallowed so a counter + // that cannot be written never turns into log spam on an otherwise successful media request. + try { + $this->persist(); + } catch (rex_sql_exception $e) { + if ($this->isConnectionLost($e)) { + try { + // Force a fresh connection and retry once. + rex_sql::factory()->setQuery('SELECT 1'); + $this->persist(); + return; + } catch (rex_sql_exception $retry) { + // fall through – give up silently, see note above + return; + } + } + // Non-connection errors are not expected here; keep them out of the log as well, + // since a failed media counter must not break or spam an already-delivered response. + return; + } + } + + + /** + * Writes the counter row: increment if it exists for today, otherwise insert. + * + * @return void + * @throws rex_sql_exception + */ + private function persist(): void { $sql = rex_sql::factory(); $result = $sql->setQuery('UPDATE ' . rex::getTable('pagestats_media') . ' SET count = count + 1 WHERE url = :url AND date = :date', ['url' => $this->url, 'date' => $this->datetime_now->format('Y-m-d')]); @@ -146,4 +181,20 @@ public function save(): void $bot->insert(); } } + + + /** + * Detects the "MySQL server has gone away" condition (connection dropped mid-request). + * + * @param rex_sql_exception $e + * @return bool + */ + private function isConnectionLost(rex_sql_exception $e): bool + { + $message = $e->getMessage(); + + return str_contains($message, 'server has gone away') + || str_contains($message, '2006') + || str_contains($message, '2013'); // Lost connection to MySQL server during query + } }