Skip to content

Commit 0822193

Browse files
committed
Add pre allocated connection releasing when closing listen endpoint
1 parent d5069a3 commit 0822193

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/inet/TCPEndPointImplLwIP.cpp

+21-19
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ CHIP_ERROR TCPEndPointImplLwIP::ListenImpl(uint16_t backlog)
9090
CHIP_ERROR err = CHIP_NO_ERROR;
9191
if (!mPreAllocatedConnectEP)
9292
{
93+
// Pre allocate a TCP EndPoint for TCP connection, it will be released under either of the two conditions:
94+
// - The Listen EndPoint receives a connection and the connection will use this endpoint. The endpoint will be release when
95+
// the connection is released.
96+
// - The Listen Endpoint is closed.
9397
err = GetEndPointManager().NewEndPoint(&mPreAllocatedConnectEP);
9498
}
9599
if (err == CHIP_NO_ERROR)
@@ -446,6 +450,11 @@ void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState)
446450
}
447451
}
448452

453+
if (mPreAllocatedConnectEP)
454+
{
455+
// If the Listen EndPoint has a pre-allocated connect EndPoint, release it for the Retain() in the constructor
456+
mPreAllocatedConnectEP->Free();
457+
}
449458
if (mState == State::kClosed)
450459
{
451460
mUnackedLength = 0;
@@ -816,7 +825,8 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
816825
conEP = static_cast<TCPEndPointImplLwIP *>(listenEP->mPreAllocatedConnectEP);
817826
if (conEP == nullptr)
818827
{
819-
// The listen endpoint receives a new incomming connection before it pre-allocates a new connection endpoint.
828+
// The listen endpoint received a new incoming connection before it had a chance to pre-allocates a new connection
829+
// endpoint.
820830
err = CHIP_ERROR_BUSY;
821831
}
822832
}
@@ -849,8 +859,18 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
849859
// Post a callback to the HandleConnectionReceived() function, passing it the new end point.
850860
listenEP->Retain();
851861
conEP->Retain();
862+
// Hand over the implied ref from constructing mPreAllocatedConnectEP to
863+
// ongoing connection.
864+
listenEP->mPreAllocatedConnectEP = nullptr;
865+
852866
err = lSystemLayer.ScheduleLambda([listenEP, conEP] {
853867
listenEP->HandleIncomingConnection(conEP);
868+
// Pre-allocate another endpoint for next connection if current connection is established
869+
CHIP_ERROR error = listenEP->GetEndPointManager().NewEndPoint(&listenEP->mPreAllocatedConnectEP);
870+
if (error != CHIP_NO_ERROR)
871+
{
872+
listenEP->HandleError(error);
873+
}
854874
conEP->Release();
855875
listenEP->Release();
856876
});
@@ -861,24 +881,6 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
861881
err = CHIP_ERROR_CONNECTION_ABORTED;
862882
conEP->Release(); // for the Retain() above
863883
}
864-
else
865-
{
866-
// Pre-allocate another endpoint for next connection
867-
listenEP->mPreAllocatedConnectEP = nullptr;
868-
listenEP->Retain();
869-
err = lSystemLayer.ScheduleLambda([listenEP]() {
870-
CHIP_ERROR error = listenEP->GetEndPointManager().NewEndPoint(&listenEP->mPreAllocatedConnectEP);
871-
if (error != CHIP_NO_ERROR)
872-
{
873-
listenEP->HandleError(error);
874-
}
875-
listenEP->Release();
876-
});
877-
if (err != CHIP_NO_ERROR)
878-
{
879-
listenEP->Release();
880-
}
881-
}
882884
}
883885

884886
// Otherwise, there was an error accepting the connection, so post a callback to the HandleError function.

0 commit comments

Comments
 (0)