From 28af6bb49361c6b8dc2c0bdf59ff702d647abc31 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Tue, 25 Jan 2022 00:18:04 +0100
Subject: [PATCH] Fix race condition when creating socket folder
If two processes call `mkdir` at the same time, the second one will fail
and wrongly assume the folder wasn't created properly.
We now first check if the folder really doesn't exist and we also catch
any IPC initialization errors and disable IPC instead of crashing the
whole bar.
---
src/ipc/util.cpp | 5 ++++-
src/main.cpp | 7 ++++++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/src/ipc/util.cpp b/src/ipc/util.cpp
index 8dc9d74e..b93cd36e 100644
--- a/src/ipc/util.cpp
+++ b/src/ipc/util.cpp
@@ -20,7 +20,10 @@ namespace ipc {
string ensure_runtime_path() {
string runtime_path = get_runtime_path();
if (!file_util::exists(runtime_path) && mkdir(runtime_path.c_str(), 0700) == -1) {
- throw system_error("Failed to create ipc socket folders");
+ // It's possible the folder was created in the meantime, we have to check again.
+ if (!file_util::exists(runtime_path)) {
+ throw system_error("Failed to create ipc socket folders");
+ }
}
return runtime_path;
diff --git a/src/main.cpp b/src/main.cpp
index a5286f0b..b7929683 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -148,7 +148,12 @@ int main(int argc, char** argv) {
unique_ptr ipc{};
if (conf.get(conf.section(), "enable-ipc", false)) {
- ipc = ipc::ipc::make(loop);
+ try {
+ ipc = ipc::ipc::make(loop);
+ } catch (const std::exception& e) {
+ ipc.reset();
+ logger.err("Disabling IPC channels due to error: %s", e.what());
+ }
}
auto ctrl = controller::make((bool)ipc, loop);