From b6aa7fc7d5113c557a44cb3bdf3570cfdb0d91e5 Mon Sep 17 00:00:00 2001 From: Nathan Wallace Date: Sun, 16 Nov 2025 00:55:33 +0800 Subject: [PATCH] Fix #4698: Add nil checks to ResetPools method (#4732) * Add test for #4698: ResetPools should handle nil pools gracefully * Fix #4698: Add nil checks to ResetPools method --- pkg/db/db_client/db_client.go | 8 ++++++-- pkg/db/db_client/db_client_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pkg/db/db_client/db_client.go b/pkg/db/db_client/db_client.go index ef066eda3..caf2e6221 100644 --- a/pkg/db/db_client/db_client.go +++ b/pkg/db/db_client/db_client.go @@ -246,8 +246,12 @@ func (c *DbClient) ResetPools(ctx context.Context) { log.Println("[TRACE] db_client.ResetPools start") defer log.Println("[TRACE] db_client.ResetPools end") - c.userPool.Reset() - c.managementPool.Reset() + if c.userPool != nil { + c.userPool.Reset() + } + if c.managementPool != nil { + c.managementPool.Reset() + } } func (c *DbClient) buildSchemasQuery(schemas ...string) string { diff --git a/pkg/db/db_client/db_client_test.go b/pkg/db/db_client/db_client_test.go index 7496993a0..af9e3377b 100644 --- a/pkg/db/db_client/db_client_test.go +++ b/pkg/db/db_client/db_client_test.go @@ -288,6 +288,27 @@ func TestDbClient_ClosePools_NilPoolsHandling(t *testing.T) { }, "closePools should handle nil pools gracefully") } +// TestResetPools verifies that ResetPools handles nil pools gracefully without panicking. +// This test addresses bug #4698 where ResetPools panics when called on a DbClient with nil pools. +func TestResetPools(t *testing.T) { + // Create a DbClient with nil pools (simulating a partially initialized or closed client) + client := &DbClient{ + userPool: nil, + managementPool: nil, + } + + // ResetPools should NOT panic even with nil pools + // This is the expected correct behavior + defer func() { + if r := recover(); r != nil { + t.Errorf("ResetPools panicked with nil pools: %v", r) + } + }() + + ctx := context.Background() + client.ResetPools(ctx) +} + // TestDbClient_SessionsMapInitialized verifies sessions map is initialized in NewDbClient func TestDbClient_SessionsMapInitialized(t *testing.T) { // Verify the initialization happens in NewDbClient