Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Fix race condtion that cause corrupted tail of output TS#875

Open
moveman wants to merge 1 commit intojustdan96:masterfrom
moveman:feature/202405/corruption
Open

Fix race condtion that cause corrupted tail of output TS#875
moveman wants to merge 1 commit intojustdan96:masterfrom
moveman:feature/202405/corruption

Conversation

@moveman
Copy link
Contributor

@moveman moveman commented May 9, 2024

We found that occasioinally the tail of the TS output is corrupted. After some debugging and guessing, the problem should be caused by this code

m_nothingToExecute = m_writeQueue.empty();

This code is not atomic. It is equivalent to this:

bool flag = m_writeQueue.empty();
m_nothingToExecute = flag;

Other threads could jump in after the first line and before the second line.
This includes BufferedFileWriter::addWriterData(). When addWriteData() is indeed be called,
the situation will be like this:

bool flag = m_writeQueue.empty(); // (true)

  m_nothingToExecute = false;
  m_writeQueue.push(data);

m_nothingToExecute = flag; // (true)

writeQueue is inserted a data and is NOT empty; however, m_nothingToExecute is set to true by the final line.
That will cause waitForWriting() to return and then close() is called before the remaining byte is flushed to the output.

@ValeZAA
Copy link

ValeZAA commented May 25, 2024

Can you provide a sample source file and output: before and after?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants