Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport race fix #295

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions source/common/conn_pool/conn_pool_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,24 @@ bool ConnPoolImplBase::isIdleImpl() const {
connecting_clients_.empty() && early_data_clients_.empty();
}

/*
This method may be invoked once or twice.
It is called first time in ConnPoolImplBase::onConnectionEvent for Local/RemoteClose events.
The second time it is called from Envoy::Tcp::ActiveTcpClient::~ActiveTcpClient via
ConnPoolImplBase::checkForIdleAndCloseIdleConnsIfDraining.

The logic must be constructed in such way that the method is called once or twice.
See PR 30807 description for explanation why idle callbacks are deleted after being called.
*/
void ConnPoolImplBase::checkForIdleAndNotify() {
if (isIdleImpl()) {
ENVOY_LOG(debug, "invoking idle callbacks - is_draining_for_deletion_={}",
is_draining_for_deletion_);
ENVOY_LOG(debug, "invoking {} idle callback(s) - is_draining_for_deletion_={}",
idle_callbacks_.size(), is_draining_for_deletion_);
for (const Instance::IdleCb& cb : idle_callbacks_) {
cb();
}
// Clear callbacks, so they are not executed if checkForIdleAndNotify is called again.
idle_callbacks_.clear();
}
}

Expand Down
17 changes: 15 additions & 2 deletions test/common/conn_pool/conn_pool_base_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,19 @@ TEST_F(ConnPoolImplDispatcherBaseTest, NoAvailableStreams) {
pool_.destructAllConnections();
}

// Verify that not fully connected active client calls
// idle callbacks upon destruction.
TEST_F(ConnPoolImplBaseTest, PoolIdleNotConnected) {
auto active_client = std::make_unique<NiceMock<TestActiveClient>>(pool_, stream_limit_,
concurrent_streams_, false);

testing::MockFunction<void()> idle_pool_callback;
EXPECT_CALL(idle_pool_callback, Call());
pool_.addIdleCallbackImpl(idle_pool_callback.AsStdFunction());

pool_.drainConnectionsImpl(Envoy::ConnectionPool::DrainBehavior::DrainAndDelete);
}

// Remote close simulates the peer closing the connection.
TEST_F(ConnPoolImplBaseTest, PoolIdleCallbackTriggeredRemoteClose) {
EXPECT_CALL(dispatcher_, createTimer_(_)).Times(AnyNumber());
Expand All @@ -491,13 +504,13 @@ TEST_F(ConnPoolImplBaseTest, PoolIdleCallbackTriggeredRemoteClose) {
pool_.onStreamClosed(*clients_.back(), false);

// Now that the last connection is closed, while there are no requests, the pool becomes idle.
// idle_pool_callback should be called once.
testing::MockFunction<void()> idle_pool_callback;
EXPECT_CALL(idle_pool_callback, Call());
pool_.addIdleCallbackImpl(idle_pool_callback.AsStdFunction());
dispatcher_.clearDeferredDeleteList();
clients_.back()->onEvent(Network::ConnectionEvent::RemoteClose);

EXPECT_CALL(idle_pool_callback, Call());
pool_.drainConnectionsImpl(Envoy::ConnectionPool::DrainBehavior::DrainAndDelete);
}

Expand All @@ -519,13 +532,13 @@ TEST_F(ConnPoolImplBaseTest, PoolIdleCallbackTriggeredLocalClose) {
pool_.onStreamClosed(*clients_.back(), false);

// Now that the last connection is closed, while there are no requests, the pool becomes idle.
// idle_pool_callback should be called once.
testing::MockFunction<void()> idle_pool_callback;
EXPECT_CALL(idle_pool_callback, Call());
pool_.addIdleCallbackImpl(idle_pool_callback.AsStdFunction());
dispatcher_.clearDeferredDeleteList();
clients_.back()->onEvent(Network::ConnectionEvent::LocalClose);

EXPECT_CALL(idle_pool_callback, Call());
pool_.drainConnectionsImpl(Envoy::ConnectionPool::DrainBehavior::DrainAndDelete);
}

Expand Down
1 change: 0 additions & 1 deletion test/common/tcp/conn_pool_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,6 @@ TEST_F(TcpConnPoolImplTest, TestIdleTimeout) {
c1.releaseConn();
conn_pool_->test_conns_[0].connection_->raiseEvent(Network::ConnectionEvent::RemoteClose);

EXPECT_CALL(idle_callback, Call());
conn_pool_->drainConnections(Envoy::ConnectionPool::DrainBehavior::DrainAndDelete);
EXPECT_CALL(*conn_pool_, onConnDestroyedForTest());
dispatcher_.clearDeferredDeleteList();
Expand Down