From 2ba535b65047b3ab21452aabc3e0e04b5bdef1c0 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 26 Nov 2022 22:15:16 -0800 Subject: [PATCH] Call Close() on sql.Rows to prevent connection leak --- backend/server/server.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/server/server.go b/backend/server/server.go index f490daa..d0139e6 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -106,6 +106,7 @@ func usageStatsHandler(ctx context.Context, w http.ResponseWriter, r *http.Reque if err != nil { panic(err) } + defer rows.Close() tbl := table.New("Registration Date", "Num Devices", "Num Entries", "Num Queries", "Last Active", "Last Query", "Versions", "IPs") tbl.WithWriter(w) for rows.Next() { @@ -145,7 +146,8 @@ func statsHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { var weeklyQueryUsers int64 = 0 checkGormResult(GLOBAL_DB.WithContext(ctx).Model(&UsageData{}).Where("last_queried > ?", lastWeek).Count(&weeklyQueryUsers)) var lastRegistration string = "" - err := GLOBAL_DB.WithContext(ctx).Raw("select to_char(max(registration_date), 'DD Month YYYY HH24:MI') from devices").Row().Scan(&lastRegistration) + row := GLOBAL_DB.WithContext(ctx).Raw("select to_char(max(registration_date), 'DD Month YYYY HH24:MI') from devices").Row() + err := row.Scan(&lastRegistration) if err != nil { panic(err) } @@ -396,6 +398,7 @@ func healthCheckHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ if err != nil { panic(fmt.Sprintf("failed to count entries in DB: %v", err)) } + defer rows.Close() if !rows.Next() { panic("Suspiciously few enc history entries!") }