Skip to content

Commit

Permalink
Fix Cancel (swap shared_ptr); notify_one outside of lock
Browse files Browse the repository at this point in the history
  • Loading branch information
qyryq committed Aug 19, 2024
1 parent 1e256ad commit 573f9f5
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ydb/public/sdk/cpp/client/ydb_topic/common/callback_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ class TCallbackContext {
~TBorrowed() {
if (!Parent) return;

std::lock_guard lock(Parent->Mutex);
--Parent->BorrowCounter;
if (Parent->Die && Parent->BorrowCounter == 0) {
bool notify = false;
{
std::lock_guard lock(Parent->Mutex);
--Parent->BorrowCounter;
notify = Parent->Die && Parent->BorrowCounter == 0;
}
if (notify) {
Parent->AllDied.notify_one();
}
}
Expand All @@ -60,10 +64,13 @@ class TCallbackContext {
// (relation of 1 owner : n impls)
public:
void Cancel() {
std::shared_ptr<TGuardedObject> waste;
std::unique_lock lock(Mutex);
Die = true;
AllDied.wait(lock, [this] { return BorrowCounter == 0; });
GuardedObjectPtr.reset();

// Swap to the waste object, so it's not destroyed under the lock, which may cause deadlocks.
swap(GuardedObjectPtr, waste);
}

std::shared_ptr<TGuardedObject> TryGet() const {
Expand Down

0 comments on commit 573f9f5

Please sign in to comment.