From 1e5a820be9810afe069c3acec9aad68da01597cd Mon Sep 17 00:00:00 2001 From: Alan Yu Date: Wed, 11 Mar 2026 16:15:50 +0800 Subject: [PATCH 1/3] fix potential NPE --- .gitignore | 6 ++++++ timeplus/client_pool.cpp | 8 +++++--- timeplus/client_pool.h | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 8def154..e7105eb 100644 --- a/.gitignore +++ b/.gitignore @@ -278,3 +278,9 @@ BUCKAROO_DEPS # Vim *.swp *.swo + +# clangd +.cache/ + +# Claude Code +.claude/settings.local.json diff --git a/timeplus/client_pool.cpp b/timeplus/client_pool.cpp index ec0649e..874c0fb 100644 --- a/timeplus/client_pool.cpp +++ b/timeplus/client_pool.cpp @@ -30,15 +30,17 @@ ClientPool::ClientPtr ClientPool::Acquire(int64_t timeout_ms) { } void ClientPool::GuardedClient::TestConnection() noexcept { + if (!client) { + valid = false; + return; + } + try { client->Ping(); valid = true; } catch (...) { valid = false; } - - TRACE("test connection: host=%s port=%d valid=%s", client->GetCurrentEndpoint()->host.c_str(), client->GetCurrentEndpoint()->port, - valid ? "true" : "false"); } } // namespace timeplus diff --git a/timeplus/client_pool.h b/timeplus/client_pool.h index d687dbe..ddaa03b 100644 --- a/timeplus/client_pool.h +++ b/timeplus/client_pool.h @@ -36,7 +36,7 @@ class ClientPool { GuardedClient(ClientPool* pool, ClientPtr client, bool valid) : client(std::move(client)), valid(valid), pool_(pool) {} ~GuardedClient() { - if (pool_) { + if (pool_ && client) { pool_->Release(std::move(client), valid); } } From 6cf8b15cbbdbc0bfa936e06a1f0af3db229ab0ff Mon Sep 17 00:00:00 2001 From: Alan Yu Date: Wed, 11 Mar 2026 17:43:31 +0800 Subject: [PATCH 2/3] better --- timeplus/client_pool.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/timeplus/client_pool.h b/timeplus/client_pool.h index ddaa03b..5c982e1 100644 --- a/timeplus/client_pool.h +++ b/timeplus/client_pool.h @@ -36,7 +36,7 @@ class ClientPool { GuardedClient(ClientPool* pool, ClientPtr client, bool valid) : client(std::move(client)), valid(valid), pool_(pool) {} ~GuardedClient() { - if (pool_ && client) { + if (pool_) { pool_->Release(std::move(client), valid); } } @@ -47,7 +47,7 @@ class ClientPool { GuardedClient& operator=(GuardedClient&& other) noexcept { if (this != &other) { - if (pool_ && client) { + if (pool_) { pool_->Release(std::move(client), valid); } pool_ = other.pool_; From 5269d1fe35b6f9c9a1e753efa89f8c17f81e7514 Mon Sep 17 00:00:00 2001 From: yun <113408135+yokofly@users.noreply.github.com> Date: Wed, 11 Mar 2026 02:51:07 -0700 Subject: [PATCH 3/3] Add ClientPool slot regression test --- ut/client_pool_ut.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ut/client_pool_ut.cpp b/ut/client_pool_ut.cpp index 6823a6e..839ce40 100644 --- a/ut/client_pool_ut.cpp +++ b/ut/client_pool_ut.cpp @@ -44,3 +44,18 @@ TEST(ClientPool, BadHost) { ASSERT_THROW(pool.Acquire(1000), std::system_error); } + +TEST(ClientPool, GuardedClientResetStillReturnsSlot) { + ClientOptions client_options; + client_options.host = "localhost"; + ClientPool pool{client_options, /*pool_size=*/1}; + + { + auto guarded = pool.GetGuardedClient(1000); + ASSERT_NE(guarded.client, nullptr); + guarded.client.reset(); + } + + auto client = pool.Acquire(100); + ASSERT_NE(client, nullptr); +}