Skip to content

Commit bf873fc

Browse files
authored
Improve asyncappender robustness (#403)
* The lock must be acquired before notify_all
1 parent 1a8875a commit bf873fc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/main/cpp/asyncappender.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ struct AsyncAppender::AsyncAppenderPriv : public AppenderSkeleton::AppenderSkele
223223
std::lock_guard<std::mutex> lock(this->bufferMutex);
224224
this->closed = true;
225225
}
226+
227+
/**
228+
* Used to ensure the dispatch thread does not wait when a logging thread is waiting.
229+
*/
230+
int blockedCount{0};
226231
};
227232

228233

@@ -313,11 +318,11 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
313318
priv->bufferNotEmpty.notify_all();
314319
break;
315320
}
316-
priv->bufferNotEmpty.notify_all();
317321
//
318322
// Following code is only reachable if buffer is full or eventCount has overflowed
319323
//
320324
std::unique_lock<std::mutex> lock(priv->bufferMutex);
325+
priv->bufferNotEmpty.notify_all();
321326
//
322327
// if blocking and thread is not already interrupted
323328
// and not the dispatcher then
@@ -328,10 +333,12 @@ void AsyncAppender::append(const spi::LoggingEventPtr& event, Pool& p)
328333
&& !priv->closed
329334
&& (priv->dispatcher.get_id() != std::this_thread::get_id()) )
330335
{
336+
++priv->blockedCount;
331337
priv->bufferNotFull.wait(lock, [this]()
332338
{
333339
return priv->eventCount - priv->dispatchedCount < priv->bufferSize;
334340
});
341+
--priv->blockedCount;
335342
discard = false;
336343
}
337344

@@ -519,7 +526,7 @@ void AsyncAppender::dispatch()
519526
{
520527
std::unique_lock<std::mutex> lock(priv->bufferMutex);
521528
priv->bufferNotEmpty.wait(lock, [this]() -> bool
522-
{ return priv->dispatchedCount != priv->commitCount || priv->closed; }
529+
{ return 0 < priv->blockedCount || priv->dispatchedCount != priv->commitCount || priv->closed; }
523530
);
524531
}
525532
isActive = !priv->isClosed();

0 commit comments

Comments
 (0)