[border-agent] track ForwardContext and abort TMF txn on disconnect (#11216)

This commit updates the Border Agent so that the `CoapDtlsSession`
tracks the forwarded CoAP request and the allocated `ForwardContext`
to the leader using `Tmf::Agent` in a list.

If the CoAP session disconnects before the forwarded request finishes,
the pending transaction is explicitly aborted in the session's
`Cleanup()` method using `Tmf::Agent::AbortTransaction()`. This
ensures that the response handler, `HandleCoapResponse()`, is invoked
while the session remains valid.
This commit is contained in:
Abtin Keshavarzian
2025-02-06 08:50:09 -08:00
committed by GitHub
parent 64e0811355
commit 521f95f608
2 changed files with 16 additions and 2 deletions
+10 -1
View File
@@ -623,6 +623,13 @@ BorderAgent::CoapDtlsSession::CoapDtlsSession(Instance &aInstance, Dtls::Transpo
void BorderAgent::CoapDtlsSession::Cleanup(void)
{
while (!mForwardContexts.IsEmpty())
{
ForwardContext *forwardContext = mForwardContexts.Pop();
IgnoreError(Get<Tmf::Agent>().AbortTransaction(HandleCoapResponse, forwardContext));
}
mTimer.Stop();
IgnoreError(Get<Ip6::Udp>().RemoveReceiver(mUdpReceiver));
Get<ThreadNetif>().RemoveUnicastAddress(mCommissionerAloc);
@@ -755,7 +762,7 @@ Error BorderAgent::CoapDtlsSession::ForwardToLeader(const Coap::Message &aMes
// will own it. We take back ownership from `HandleCoapResponse()`
// callback.
forwardContext.Release();
mForwardContexts.Push(*forwardContext.Release());
LogInfo("Forwarded request to leader on %s", PathForUri(aUri));
@@ -790,6 +797,8 @@ void BorderAgent::CoapDtlsSession::HandleCoapResponse(const ForwardContext &aFor
Coap::Message *message = nullptr;
Error error;
IgnoreError(mForwardContexts.Remove(aForwardContext));
SuccessOrExit(error = aResult);
VerifyOrExit((message = NewPriorityMessage()) != nullptr, error = kErrorNoBufs);
+6 -1
View File
@@ -42,6 +42,7 @@
#include "common/as_core_type.hpp"
#include "common/heap_allocatable.hpp"
#include "common/linked_list.hpp"
#include "common/locator.hpp"
#include "common/non_copyable.hpp"
#include "common/notifier.hpp"
@@ -311,7 +312,9 @@ private:
void Cleanup(void);
private:
class ForwardContext : public Heap::Allocatable<ForwardContext>, private ot::NonCopyable
class ForwardContext : public ot::LinkedListEntry<ForwardContext>,
public Heap::Allocatable<ForwardContext>,
private ot::NonCopyable
{
friend class Heap::Allocatable<ForwardContext>;
@@ -319,6 +322,7 @@ private:
Error ToHeader(Coap::Message &aMessage, uint8_t aCode) const;
CoapDtlsSession &mSession;
ForwardContext *mNext;
uint16_t mMessageId;
bool mPetition : 1;
bool mSeparate : 1;
@@ -358,6 +362,7 @@ private:
void HandleTimer(void);
bool mIsActiveCommissioner;
LinkedList<ForwardContext> mForwardContexts;
TimerMilliContext mTimer;
Ip6::Udp::Receiver mUdpReceiver;
Ip6::Netif::UnicastAddress mCommissionerAloc;