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

Ensure we deallocate CASEClient if EstablishSession fails synchronously. #17466

Merged
merged 1 commit into from
Apr 19, 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
27 changes: 12 additions & 15 deletions src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ void OperationalDeviceProxy::MoveToState(State aTargetState)
ChipLogValueX64(mPeerId.GetCompressedFabricId()), ChipLogValueX64(mPeerId.GetNodeId()), to_underlying(mState),
to_underlying(aTargetState));
mState = aTargetState;

if (aTargetState != State::Connecting)
{
CleanupCASEClient();
}
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -211,7 +216,11 @@ CHIP_ERROR OperationalDeviceProxy::EstablishConnection()
ReturnErrorCodeIf(mCASEClient == nullptr, CHIP_ERROR_NO_MEMORY);
CHIP_ERROR err =
mCASEClient->EstablishSession(mPeerId, mDeviceAddress, mMRPConfig, HandleCASEConnected, HandleCASEConnectionFailure, this);
ReturnErrorOnFailure(err);
if (err != CHIP_NO_ERROR)
{
CleanupCASEClient();
return err;
}

MoveToState(State::Connecting);

Expand Down Expand Up @@ -287,7 +296,6 @@ void OperationalDeviceProxy::HandleCASEConnectionFailure(void * context, CASECli
//
device->MoveToState(State::Initialized);

device->CloseCASESession();
device->DequeueConnectionCallbacks(error);

//
Expand All @@ -311,7 +319,6 @@ void OperationalDeviceProxy::HandleCASEConnected(void * context, CASEClient * cl
else
{
device->MoveToState(State::SecureConnected);
device->CloseCASESession();
device->DequeueConnectionCallbacks(CHIP_NO_ERROR);
}

Expand All @@ -329,27 +336,17 @@ CHIP_ERROR OperationalDeviceProxy::Disconnect()
mInitParams.sessionManager->ExpirePairing(mSecureSession.Get());
}
MoveToState(State::Initialized);
if (mCASEClient)
{
mInitParams.clientPool->Release(mCASEClient);
mCASEClient = nullptr;
}

return CHIP_NO_ERROR;
}

void OperationalDeviceProxy::Clear()
{
if (mCASEClient)
{
mInitParams.clientPool->Release(mCASEClient);
mCASEClient = nullptr;
}

MoveToState(State::Uninitialized);
mInitParams = DeviceProxyInitParams();
}

void OperationalDeviceProxy::CloseCASESession()
void OperationalDeviceProxy::CleanupCASEClient()
{
if (mCASEClient)
{
Expand Down
6 changes: 3 additions & 3 deletions src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy,
FabricInfo * mFabricInfo;
System::Layer * mSystemLayer;

// mCASEClient is only non-null if we are in State::Connecting or just
// allocated it as part of an attempt to enter State::Connecting.
CASEClient * mCASEClient = nullptr;

PeerId mPeerId;
Expand Down Expand Up @@ -277,9 +279,7 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy,
static void HandleCASEConnected(void * context, CASEClient * client);
static void HandleCASEConnectionFailure(void * context, CASEClient * client, CHIP_ERROR error);

static void CloseCASESessionTask(System::Layer * layer, void * context);

void CloseCASESession();
void CleanupCASEClient();

void EnqueueConnectionCallbacks(Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure);
Expand Down