Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add CloseActiveConnections() call in TCPBase::Close(), which
is called as part of Server::Shutdown().
Active connections should be closed as part of Server shutdown.
This allows the TCPConnectionState to also close the associated
TCPEndpoint object as part of this shutdown flow.

Previously, the CloseActiveConnections() call was present in the
TCPBase destructor alone.

Add test for Connection Close() and checking for TCPEndPoint.
  • Loading branch information
pidarped authored Dec 19, 2024
1 parent 238e801 commit 93e50c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/transport/raw/ActiveTCPConnectionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ struct ActiveTCPConnectionState

void Free()
{
mEndPoint->Free();
if (mEndPoint)
{
mEndPoint->Free();
}
mPeerAddr = PeerAddress::Uninitialized();
mEndPoint = nullptr;
mReceived = nullptr;
Expand Down
13 changes: 5 additions & 8 deletions src/transport/raw/TCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ constexpr int kListenBacklogSize = 2;

TCPBase::~TCPBase()
{
if (mListenSocket != nullptr)
{
// endpoint is only non null if it is initialized and listening
mListenSocket->Free();
mListenSocket = nullptr;
}

CloseActiveConnections();
// Call Close to free the listening socket and close all active connections.
Close();
}

void TCPBase::CloseActiveConnections()
Expand Down Expand Up @@ -125,6 +119,9 @@ void TCPBase::Close()
mListenSocket->Free();
mListenSocket = nullptr;
}

CloseActiveConnections();

mState = TCPState::kNotReady;
}

Expand Down
23 changes: 23 additions & 0 deletions src/transport/raw/tests/TestTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,29 @@ TEST_F(TestTCP, HandleConnCloseCalledTest6)
HandleConnCloseTest(addr);
}

TEST_F(TestTCP, CheckTCPEndpointAfterCloseTest)
{
TCPImpl tcp;

IPAddress addr;
IPAddress::FromString("::1", addr);

MockTransportMgrDelegate gMockTransportMgrDelegate(mIOContext);
gMockTransportMgrDelegate.InitializeMessageTest(tcp, addr);
gMockTransportMgrDelegate.ConnectTest(tcp, addr);

Transport::PeerAddress lPeerAddress = Transport::PeerAddress::TCP(addr, gChipTCPPort);
void * state = TestAccess::FindActiveConnection(tcp, lPeerAddress);
ASSERT_NE(state, nullptr);
TCPEndPoint * lEndPoint = TestAccess::GetEndpoint(state);
ASSERT_NE(lEndPoint, nullptr);

// Call Close and check the TCPEndpoint
tcp.Close();
lEndPoint = TestAccess::GetEndpoint(state);
ASSERT_EQ(lEndPoint, nullptr);
}

TEST_F(TestTCP, CheckProcessReceivedBuffer)
{
TCPImpl tcp;
Expand Down

0 comments on commit 93e50c7

Please sign in to comment.