From ae168dd03165d64b0ef89d36c723e0e571b5385f Mon Sep 17 00:00:00 2001 From: Emilio <Emilio.Meschi@cern.ch> Date: Tue, 15 Nov 2022 10:54:23 +0100 Subject: [PATCH] fixes to avoid deadlocks in destructor in case of a fatal uncaught exception --- src/OutputFileHandler.cc | 8 ++++++-- src/OutputFileHandler.h | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/OutputFileHandler.cc b/src/OutputFileHandler.cc index fd539ad8..ac28fd0b 100644 --- a/src/OutputFileHandler.cc +++ b/src/OutputFileHandler.cc @@ -10,7 +10,7 @@ const std::string OutputFileHandler::journal_file_ = "index.journal"; tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>> OutputFileHandler::files_to_close_ = tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>>(); -bool OutputFileHandler::file_handler_running_ = true; +std::atomic<bool> OutputFileHandler::file_handler_running_{true}; void OutputFileHandler::enqueue_current_file_for_close_and_move_maybe() { if (current_file_ != 0) { @@ -93,7 +93,11 @@ void OutputFileHandler::close_and_rename::operator()() const { // std::cout << std::this_thread::get_id() << " try pop" << // files_to_close_.size() << std::endl; LOG(TRACE) << "try pop now. queue size now " << files_to_close_.size(); - files_to_close_.pop(n); + try { + files_to_close_.pop(n); + } catch (tbb::user_abort &e) { + break; + } LOG(TRACE) << "popping file: " << n.first << " for closing, queue size now " << files_to_close_.size(); if (fclose(n.second) != 0) { diff --git a/src/OutputFileHandler.h b/src/OutputFileHandler.h index 68f0449f..19209164 100644 --- a/src/OutputFileHandler.h +++ b/src/OutputFileHandler.h @@ -2,6 +2,7 @@ #define OUTPUTFILEHANDLER_H #include "tbb/concurrent_queue.h" +#include <atomic> #include <cstdint> #include <cstdio> #include <string> @@ -17,8 +18,8 @@ public: working_files_base_path_(base_path_ + "/" + working_dir_), current_run_number_(0), current_index_(0), current_filename_(""), current_file_(0), close_and_rename_(base_path_), t{} { - t = std::thread(close_and_rename_); create_output_directory_maybe(working_files_base_path_); + t = std::thread(close_and_rename_); } ~OutputFileHandler() { @@ -40,7 +41,8 @@ private: std::string format_filename(uint32_t run_number, uint32_t index); void exit_file_handler_loop() { - file_handler_running_ = false; + file_handler_running_.store(false, std::memory_order_release); + files_to_close_.abort(); t.join(); } @@ -63,7 +65,8 @@ private: static tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>> files_to_close_; - static bool file_handler_running_; + + static std::atomic<bool> file_handler_running_; std::string base_path_; std::string filename_prefix_; -- GitLab