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 double-free bugs on failure to send a write message. #13051

Merged
merged 1 commit into from
Dec 16, 2021
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
18 changes: 12 additions & 6 deletions src/app/WriteClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,20 @@ CHIP_ERROR WriteClient::SendWriteRequest(SessionHandle session, System::Clock::T
ChipLogError(DataManagement, "Write client failed to SendWriteRequest");
ClearExistingExchangeContext();
}

if (session.IsGroupSession())
else
{
// Always shutdown on Group communication
ChipLogDetail(DataManagement, "Closing on group Communication ");
// TODO: Ideally this would happen async, but to make sure that we
// handle this object dying (e.g. due to IM enging shutdown) while the
// async bits are pending we'd need to malloc some state bit that we can
// twiddle if we die. For now just do the OnDone callback sync.
if (session.IsGroupSession())
{
// Always shutdown on Group communication
ChipLogDetail(DataManagement, "Closing on group Communication ");

// onDone is called
ShutdownInternal();
// onDone is called
ShutdownInternal();
}
}

return err;
Expand Down
3 changes: 3 additions & 0 deletions src/app/WriteClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ class WriteClientHandle
/**
* Finalize the message and send it to the desired node. The underlying write object will always be released, and the user
* should not use this object after calling this function.
*
* Note: In failure cases this will _synchronously_ invoke OnDone on the
* WriteClient::Callback before returning.
*/
CHIP_ERROR SendWriteRequest(SessionHandle session, System::Clock::Timeout timeout = kImMessageTimeout);

Expand Down
6 changes: 5 additions & 1 deletion src/controller/WriteInteraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ CHIP_ERROR WriteAttribute(SessionHandle sessionHandle, chip::EndpointId endpoint
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY);

ReturnErrorOnFailure(app::InteractionModelEngine::GetInstance()->NewWriteClient(handle, callback.get(), aTimedWriteTimeoutMs));

// At this point the handle will ensure our callback's OnDone is always
// called.
callback.release();

if (sessionHandle.IsGroupSession())
{
ReturnErrorOnFailure(
Expand All @@ -119,7 +124,6 @@ CHIP_ERROR WriteAttribute(SessionHandle sessionHandle, chip::EndpointId endpoint

ReturnErrorOnFailure(handle.SendWriteRequest(sessionHandle));

callback.release();
return CHIP_NO_ERROR;
}

Expand Down