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

Fix OT UDP receive and UDP close racing #20536

Merged
merged 2 commits into from
Jul 14, 2022
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
2 changes: 1 addition & 1 deletion src/inet/UDPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ void UDPEndPoint::Close()
{
if (mState != State::kClosed)
{
CloseImpl();
mState = State::kClosed;
CloseImpl();
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/inet/UDPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ void UDPEndPointImplLwIP::CloseImpl()
udp_remove(mUDP);
mUDP = nullptr;
mLwIPEndPointType = LwIPEndPointType::Unknown;

// In case that there is a UDPEndPointImplLwIP::LwIPReceiveUDPMessage
// event pending in the event queue (SystemLayer::ScheduleLambda), we
// schedule a release call to the end of the queue, to ensure that the
// queued pointer to UDPEndPointImplLwIP is not dangling.
Retain();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is relying on being called with the Matter stack locked, right? @kghost should we have corresponding asserts here (and in the openthread version)?

CHIP_ERROR err = GetSystemLayer().ScheduleLambda([this] { Release(); });
if (err != CHIP_NO_ERROR)
{
ChipLogError(Inet, "Unable scedule lambda: %" CHIP_ERROR_FORMAT, err.Format());
// There is nothing we can do here, accept the chance of racing
kghost marked this conversation as resolved.
Show resolved Hide resolved
Release();
}
}

// Unlock LwIP stack
Expand Down Expand Up @@ -342,6 +355,10 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
UDPEndPointImplLwIP * ep = static_cast<UDPEndPointImplLwIP *>(arg);
IPPacketInfo * pktInfo = nullptr;
System::PacketBufferHandle buf = System::PacketBufferHandle::Adopt(p);

if (ep->mState == State::kClosed)
return;

if (buf->HasChainedBuffer())
{
// Try the simple expedient of flattening in-place.
Expand Down
16 changes: 16 additions & 0 deletions src/inet/UDPEndPointImplOpenThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ void UDPEndPointImplOT::handleUdpReceive(void * aContext, otMessage * aMessage,
char destStr[Inet::IPAddress::kMaxStringLength];
#endif

if (ep->mState == State::kClosed)
return;

if (msgLen > System::PacketBuffer::kMaxSizeWithoutReserve)
{
ChipLogError(Inet, "UDP message too long, discarding. Size received %d", msgLen);
Expand Down Expand Up @@ -261,6 +264,19 @@ void UDPEndPointImplOT::CloseImpl()
if (otUdpIsOpen(mOTInstance, &mSocket))
{
otUdpClose(mOTInstance, &mSocket);

// In case that there is a UDPEndPointImplOT::handleUdpReceive event
// pending in the event queue (SystemLayer::ScheduleLambda), we
// schedule a release call to the end of the queue, to ensure that the
// queued pointer to UDPEndPointImplOT is not dangling.
Retain();
CHIP_ERROR err = GetSystemLayer().ScheduleLambda([this] { Release(); });
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
if (err != CHIP_NO_ERROR)
{
ChipLogError(Inet, "Unable scedule lambda: %" CHIP_ERROR_FORMAT, err.Format());
// There is nothing we can do here, accept the chance of racing
Release();
}
}
UnlockOpenThread();
}
Expand Down