Skip to content

Commit

Permalink
Improve asyncappender robustness (#403)
Browse files Browse the repository at this point in the history
* The lock must be acquired before notify_all
  • Loading branch information
swebb2066 authored Jul 26, 2024
1 parent 1a8875a commit bf873fc
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/cpp/asyncappender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ struct AsyncAppender::AsyncAppenderPriv : public AppenderSkeleton::AppenderSkele
std::lock_guard<std::mutex> lock(this->bufferMutex);
this->closed = true;
}

/**
* Used to ensure the dispatch thread does not wait when a logging thread is waiting.
*/
int blockedCount{0};
};


Expand Down Expand Up @@ -313,11 +318,11 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
priv->bufferNotEmpty.notify_all();
break;
}
priv->bufferNotEmpty.notify_all();
//
// Following code is only reachable if buffer is full or eventCount has overflowed
//
std::unique_lock<std::mutex> lock(priv->bufferMutex);
priv->bufferNotEmpty.notify_all();
//
// if blocking and thread is not already interrupted
// and not the dispatcher then
Expand All @@ -328,10 +333,12 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
&& !priv->closed
&& (priv->dispatcher.get_id() != std::this_thread::get_id()) )
{
++priv->blockedCount;
priv->bufferNotFull.wait(lock, [this]()
{
return priv->eventCount - priv->dispatchedCount < priv->bufferSize;
});
--priv->blockedCount;
discard = false;
}

Expand Down Expand Up @@ -519,7 +526,7 @@ void AsyncAppender::dispatch()
{
std::unique_lock<std::mutex> lock(priv->bufferMutex);
priv->bufferNotEmpty.wait(lock, [this]() -> bool
{ return priv->dispatchedCount != priv->commitCount || priv->closed; }
{ return 0 < priv->blockedCount || priv->dispatchedCount != priv->commitCount || priv->closed; }
);
}
isActive = !priv->isClosed();
Expand Down

0 comments on commit bf873fc

Please sign in to comment.