From 379571098d1b597a2a5f159dfc7522cc4fde6c63 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 7 Jun 2017 17:59:55 -0700 Subject: Fix a race condition for temperature_logger It's a rare deadlock that happens on fugu when the updater exits too fast due to a format error. Bug: 62379170 Test: fake-ota fails gracefully without hanging. Change-Id: Icbd6cff4f6b44ca20f4d75f8039332111f696cc5 --- install.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/install.cpp b/install.cpp index a1f2e4fbd..db4ba9373 100644 --- a/install.cpp +++ b/install.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -294,11 +295,12 @@ int update_binary_command(const std::string& package, ZipArchiveHandle zip, } #endif // !AB_OTA_UPDATER -static void log_max_temperature(int* max_temperature) { +static void log_max_temperature(int* max_temperature, const std::atomic& logger_finished) { CHECK(max_temperature != nullptr); std::mutex mtx; std::unique_lock lck(mtx); - while (finish_log_temperature.wait_for(lck, 20s) == std::cv_status::timeout) { + while (!logger_finished.load() && + finish_log_temperature.wait_for(lck, 20s) == std::cv_status::timeout) { *max_temperature = std::max(*max_temperature, GetMaxValueFromThermalZone()); } } @@ -403,7 +405,8 @@ static int try_update_binary(const std::string& package, ZipArchiveHandle zip, b } close(pipefd[1]); - std::thread temperature_logger(log_max_temperature, max_temperature); + std::atomic logger_finished(false); + std::thread temperature_logger(log_max_temperature, max_temperature, std::ref(logger_finished)); *wipe_cache = false; bool retry_update = false; @@ -467,6 +470,7 @@ static int try_update_binary(const std::string& package, ZipArchiveHandle zip, b int status; waitpid(pid, &status, 0); + logger_finished.store(true); finish_log_temperature.notify_one(); temperature_logger.join(); -- cgit v1.2.3