Skip to content

Commit 93d9b87

Browse files
changes as per new analysis
1 parent 4a0e34d commit 93d9b87

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

src/inet/UDPEndPoint.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ CHIP_ERROR UDPEndPoint::SendMsg(const IPPacketInfo * pktInfo, System::PacketBuff
124124
void UDPEndPoint::Free()
125125
{
126126
Close();
127-
Delete();
127+
128+
// CloseImpl() may have called Ref() to keep endpoint alive for pending operations
129+
// (when mDelayReleaseCount != 0). Only delete if ref count is still 0.
130+
// If ref count > 0, the deferred Unref() scheduled by CloseImpl() will eventually
131+
// call Free() again when all pending operations complete.
132+
if (GetReferenceCount() == 0)
133+
{
134+
Delete();
135+
}
128136
}
129137

130138
void UDPEndPoint::Close()

src/inet/UDPEndPointImplLwIP.cpp

100644100755
Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,7 @@ void UDPEndPointImplLwIP::CloseImpl()
221221
if (mDelayReleaseCount != 0)
222222
{
223223
Ref();
224-
// Capture the instance ID to validate this is still the same endpoint instance
225-
uint32_t expectedInstanceId = mInstanceId.load();
226-
CHIP_ERROR err = GetSystemLayer().ScheduleLambda([this, expectedInstanceId] {
227-
// Verify this is still the same endpoint instance by checking the instance ID
228-
if (mInstanceId.load() != expectedInstanceId)
229-
{
230-
// This endpoint memory was reused for a new endpoint with different instance ID.
231-
// The original endpoint this Unref was intended for is already gone.
232-
return;
233-
}
234-
235-
Unref();
236-
});
224+
CHIP_ERROR err = GetSystemLayer().ScheduleLambda([this] { Unref(); });
237225
if (err != CHIP_NO_ERROR)
238226
{
239227
ChipLogError(Inet, "Unable to schedule lambda: %" CHIP_ERROR_FORMAT, err.Format());
@@ -390,24 +378,8 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
390378
// pending on it.
391379
ep->mDelayReleaseCount++;
392380

393-
// Capture the instance ID to validate this is still the same endpoint instance
394-
uint32_t expectedInstanceId = ep->mInstanceId.load();
395-
CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda(
396-
[ep, expectedInstanceId, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(buf), pktInfo = pktInfo.get()] {
397-
// Critical check: Verify this lambda is for the correct endpoint instance
398-
// by comparing the instance ID. If they don't match, the endpoint was
399-
// deleted and new endpoint wascreated at the same memory address.
400-
if (ep->mInstanceId.load() != expectedInstanceId)
401-
{
402-
// This is a stale lambda for a previously deleted endpoint.
403-
// The memory was reused for a new endpoint with different instance ID.
404-
// Note: We don't decrement mDelayReleaseCount here because:
405-
// - The original endpoint's count is gone with the deleted endpoint
406-
// - The new endpoint's count should not be decremented (it never had this packet)
407-
pbuf_free(p);
408-
return;
409-
}
410-
381+
CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda(
382+
[ep, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(buf), pktInfo = pktInfo.get()] {
411383
ep->mDelayReleaseCount--;
412384

413385
auto handle = System::PacketBufferHandle::Adopt(p);
@@ -430,6 +402,7 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
430402
(void) sQueueFilter->FilterAfterDequeue(ep, *(pktInfo.get()), buf);
431403
ChipLogError(Inet, "Dequeue ERROR err = %" CHIP_ERROR_FORMAT, err.Format());
432404
}
405+
433406
ep->mDelayReleaseCount--;
434407
}
435408
}

src/inet/UDPEndPointImplLwIP.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ namespace Inet {
3333
class UDPEndPointImplLwIP : public UDPEndPoint, public EndPointStateLwIP
3434
{
3535
public:
36-
UDPEndPointImplLwIP(EndPointManager<UDPEndPoint> & endPointManager) : UDPEndPoint(endPointManager), mUDP(nullptr)
37-
{
38-
// Generate a unique instance ID to detect endpoint reuse
39-
static std::atomic<uint32_t> sNextInstanceId{ 1 };
40-
mInstanceId.store(sNextInstanceId.fetch_add(1));
41-
}
36+
UDPEndPointImplLwIP(EndPointManager<UDPEndPoint> & endPointManager) : UDPEndPoint(endPointManager), mUDP(nullptr) {}
4237

4338
// UDPEndPoint overrides.
4439
CHIP_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback) override;
@@ -81,7 +76,6 @@ class UDPEndPointImplLwIP : public UDPEndPoint, public EndPointStateLwIP
8176

8277
udp_pcb * mUDP; // LwIP User datagram protocol (UDP) control block.
8378
std::atomic_int mDelayReleaseCount{ 0 };
84-
std::atomic<uint32_t> mInstanceId{ 0 }; // Unique endpoint ID
8579

8680
static EndpointQueueFilter * sQueueFilter;
8781
};

0 commit comments

Comments
 (0)