Skip to content

Commit 1320486

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Remove the OnSessionReleased callback from OperationalSessionSetup. (#26395)
Since OperationalSessionSetup is ephemeral, it only holds on to a session while it's notifying its listeners, after which it will delete itself. Right now it was deleting itself from OnSessionReleased, but that means it could end up with a double-delete... and also, it's already notifying listeners if it has a session, so there is no point, or ability, to notify them again on session release. The changes here: 1. Take out the OnSessionReleased that couldn't do anything except lead to use-after-free. 2. Fix a bug on OnSessionEstablished where if we got a session that's not usable we leaked and left our listeners hanging instead of just notifying our listeners with error.
1 parent 081873a commit 1320486

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

src/app/OperationalSessionSetup.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,18 @@ void OperationalSessionSetup::OnSessionEstablished(const SessionHandle & session
396396
ChipLogError(Discovery, "OnSessionEstablished was called while we were not connecting"));
397397

398398
if (!mSecureSession.Grab(session))
399-
return; // Got an invalid session, do not change any state
399+
{
400+
// Got an invalid session, just dispatch an error. We have to do this
401+
// so we don't leak.
402+
DequeueConnectionCallbacks(CHIP_ERROR_INCORRECT_STATE);
403+
404+
// Do not touch `this` instance anymore; it has been destroyed in DequeueConnectionCallbacks.
405+
return;
406+
}
400407

401408
MoveToState(State::SecureConnected);
402409

403410
DequeueConnectionCallbacks(CHIP_NO_ERROR);
404-
// Do not touch `this` instance anymore; it has been destroyed in DequeueConnectionCallbacks.
405411
}
406412

407413
void OperationalSessionSetup::CleanupCASEClient()
@@ -413,14 +419,6 @@ void OperationalSessionSetup::CleanupCASEClient()
413419
}
414420
}
415421

416-
void OperationalSessionSetup::OnSessionReleased()
417-
{
418-
// This is unlikely to be called since within the same call that we get SessionHandle we
419-
// then call DequeueConnectionCallbacks which releases `this`. If this is called, and we
420-
// we have any callbacks we will just send an error.
421-
DequeueConnectionCallbacks(CHIP_ERROR_INCORRECT_STATE);
422-
}
423-
424422
OperationalSessionSetup::~OperationalSessionSetup()
425423
{
426424
if (mAddressLookupHandle.IsActive())

src/app/OperationalSessionSetup.h

+3-11
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,13 @@ typedef void (*OnDeviceConnectionRetry)(void * context, const ScopedNodeId & pee
152152
* It is possible to determine which of the two purposes the OperationalSessionSetup is for by calling
153153
* IsForAddressUpdate().
154154
*/
155-
class DLL_EXPORT OperationalSessionSetup : public SessionDelegate,
156-
public SessionEstablishmentDelegate,
157-
public AddressResolve::NodeListener
155+
class DLL_EXPORT OperationalSessionSetup : public SessionEstablishmentDelegate, public AddressResolve::NodeListener
158156
{
159157
public:
160158
~OperationalSessionSetup() override;
161159

162160
OperationalSessionSetup(const CASEClientInitParams & params, CASEClientPoolDelegate * clientPool, ScopedNodeId peerId,
163-
OperationalSessionReleaseDelegate * releaseDelegate) :
164-
mSecureSession(*this)
161+
OperationalSessionReleaseDelegate * releaseDelegate)
165162
{
166163
mInitParams = params;
167164
if (params.Validate() != CHIP_NO_ERROR || clientPool == nullptr || releaseDelegate == nullptr)
@@ -201,11 +198,6 @@ class DLL_EXPORT OperationalSessionSetup : public SessionDelegate,
201198
void OnSessionEstablished(const SessionHandle & session) override;
202199
void OnSessionEstablishmentError(CHIP_ERROR error) override;
203200

204-
//////////// SessionDelegate Implementation ///////////////
205-
206-
// Called when a connection is closing. The object releases all resources associated with the connection.
207-
void OnSessionReleased() override;
208-
209201
ScopedNodeId GetPeerId() const { return mPeerId; }
210202

211203
static Transport::PeerAddress ToPeerAddress(const Dnssd::ResolvedNodeData & nodeData)
@@ -268,7 +260,7 @@ class DLL_EXPORT OperationalSessionSetup : public SessionDelegate,
268260

269261
Transport::PeerAddress mDeviceAddress = Transport::PeerAddress::UDP(Inet::IPAddress::Any);
270262

271-
SessionHolderWithDelegate mSecureSession;
263+
SessionHolder mSecureSession;
272264

273265
Callback::CallbackDeque mConnectionSuccess;
274266
Callback::CallbackDeque mConnectionFailure;

0 commit comments

Comments
 (0)