Skip to content

Commit 1e2e2e7

Browse files
libs/eventsfile: fix race condition when rotating log file
The condition to know if file rotation should take place can be accessed in a concurrent manner by 2 or more threads. One thread can enter the if block and open a new file and close the current one while the other thread still tries to write to the current logfile that has been closedl! This leads to segfault. Let's move the mutex before the block to prevent this race condition. Signed-off-by: Cedric CHEDALEUX <cedric.chedaleux@orange.com>
1 parent 4af3a83 commit 1e2e2e7

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

lib/eventsfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ void EventsFile::write(const std::string& event, unsigned long long timestamp, c
5656
string line = ss.str();
5757

5858
// Total size of all rotating files should never exceed the defined max cap size
59+
std::lock_guard<std::mutex> guard(m_fileMutex);
5960
if (m_maxCapSize > 0 && m_currentFileSize + line.size() > m_maxCapSize / ROTATING_FILES_NUMBER) {
6061
rotateFile();
6162
}
6263

63-
std::lock_guard<std::mutex> guard(m_fileMutex);
6464
m_file << line;
6565
m_file.flush();
6666
m_currentFileSize += line.size();

0 commit comments

Comments
 (0)