Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock in log archiver when rename fails #1607

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,16 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)

if (fImmediate || (fArchiveDaily && ArchiveCheckDate > PrevArchiveCheckDate))
{
std::string rename_error_msg;

{
std::lock_guard<std::mutex> scoped_lock(m_cs);

fclose(m_fileout);

plogfile = m_file_path;

pfile_temp = static_cast<fs::path>(m_file_path.stem().string() + "-" + DateTimeStrFormat("%Y%m%d%H%M%S", nTime) + m_file_path.extension().string());
pfile_temp = LogArchiveDir / static_cast<fs::path>(m_file_path.stem().string() + "-" + DateTimeStrFormat("%Y%m%d%H%M%S", nTime) + m_file_path.extension().string());

pfile_out = LogArchiveDir / static_cast<fs::path>((m_file_path.filename().stem().string() + "-" + DateTimeStrFormat("%Y%m%d%H%M%S", nTime)
+ m_file_path.filename().extension().string() + ".gz"));
Expand All @@ -410,10 +412,10 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
{
fs::rename(plogfile, pfile_temp);
}
catch(...)
catch(const std::exception& e)
{
LogPrintf("ERROR: Logger: archive: Failed to rename logging file\n");
return false;
rename_error_msg = "Failed to rename logging file: ";
rename_error_msg += e.what();
}

// Re-open logging file. (This is subtly different than the flag based reopen above, because the file must be closed first, renamed for compression,
Expand All @@ -428,6 +430,12 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
PrevArchiveCheckDate = ArchiveCheckDate;
}

if (!rename_error_msg.empty())
{
LogPrintf("ERROR: Logger: archive: %s", rename_error_msg);
return false;
}

fsbridge::ifstream infile(pfile_temp, std::ios_base::in | std::ios_base::binary);

if (!infile)
Expand Down