From ea3d8e6b07786b31c6ebdf91eb8fb286d2f60a63 Mon Sep 17 00:00:00 2001 From: Alvar Penning Date: Mon, 15 Dec 2025 11:23:44 +0100 Subject: [PATCH] notifications: Close Prepared Statement The prepared statement used to fetch custom vars from SQL for notification events is not closed. This results in leaking prepared statements, eventually resulting in the rejection of new prepared statements. > notifications: Cannot build event from history entry error="cannot > build event for \"[...]\",\"[...]\": Error 1461 (42000): Can't create > more than max_prepared_stmt_count statements (current value: 16382)" Monitoring MariaDB's prepared_stmt_count over roughly ten minutes before and after this change shows an increasing number of open prepared statements. Before patch 180 +-----------------------------------------------------------------+ | + + + + + + AAAA | 160 |-+ AAA +-| | AA | 140 |-+ AAAAA +-| | AAAA | 120 |-+ AAA +-| | AAAAA | 100 |-+ AAAAAAA +-| 80 |-+ AAAA +-| | AA | 60 |-+ AAA +-| | AAAAAAAA | 40 |-+ AAAA +-| | AAAAA | 20 |-+ AAA +-| | +AAA + + + + + + | 0 +-----------------------------------------------------------------+ 9 1.76579 1.76579x 1.76579 1.76579 1.76579 1.76579x 1.76579 1.765 1.76579x10 Unix timestamp The second graph, a record after the patch, contains measurements at the zero level. After patch 1 +-----------------------------------------------------------------+ | + + + + + + + | | | 0.8 |-+ +-| | | | | | | 0.6 |-+ +-| | | | | 0.4 |-+ +-| | | | | | | 0.2 |-+ +-| | | | + + + + + + + | 0 +-----------------------------------------------------------------+ 9 1.76579 1.76579x 1.76579 1.76579 1.76579 1.76579x 1.76579 1.765 1.76579x10 Unix timestamp --- pkg/notifications/fetch.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/notifications/fetch.go b/pkg/notifications/fetch.go index 58b1a0d7..9174c183 100644 --- a/pkg/notifications/fetch.go +++ b/pkg/notifications/fetch.go @@ -94,6 +94,7 @@ func (client *Client) fetchCustomVarFromSql( if err != nil { return nil, err } + defer func() { _ = stmt.Close() }() var customVars []customVar if err := stmt.SelectContext(ctx, &customVars, id); err != nil {