diff --git a/CHANGELOG.md b/CHANGELOG.md index dc49a280939..6fd882805c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - FIXED: Update Node.js binding path from `lib/binding` to `lib/binding_napi_v8` to match node-pre-gyp versioning conventions [#7272](https://github.com/Project-OSRM/osrm-backend/pull/7272) - FIXED: Reduce MSVC compiler warnings by suppressing informational warnings while preserving bug-indicating warnings [#7253](https://github.com/Project-OSRM/osrm-backend/issues/7253) - Misc: + - ADDED: `SHM_LOCK_DIR` environment variable for shared memory lock file directory [#7312](https://github.com/Project-OSRM/osrm-backend/pull/7312) - FIXED: Fix JSON rendering of large OSM IDs (avoids scientific notation) and handle NaN/Infinity gracefully [#7016](https://github.com/Project-OSRM/osrm-backend/issues/7016) - CHANGED: Add std::format compatibility layer with fallback to fmt::format [#7261](https://github.com/Project-OSRM/osrm-backend/pull/7261) - FIXED: Update node_osrm to C++20 to fix ABI mismatch with libosrm (was overlooked in #6877) [#7261](https://github.com/Project-OSRM/osrm-backend/pull/7261) diff --git a/docs/routed.md b/docs/routed.md index 99590c56411..7ffe1617947 100644 --- a/docs/routed.md +++ b/docs/routed.md @@ -1,5 +1,13 @@ ## Environment Variables +### SHM_LOCK_DIR + +If the SHM_LOCK_DIR environment variable is set, OSRM will use it as the +directory for shared memory lock files instead of the system temporary directory. +This is useful in containerized environments (Docker/Kubernetes) where the lock +file directory should persist across container restarts when loading from shared +memory. + ### SIGNAL_PARENT_WHEN_READY If the SIGNAL_PARENT_WHEN_READY environment variable is set osrm-routed will diff --git a/include/storage/shared_memory.hpp b/include/storage/shared_memory.hpp index 3acec0f3d8a..457a688a9ff 100644 --- a/include/storage/shared_memory.hpp +++ b/include/storage/shared_memory.hpp @@ -18,6 +18,7 @@ #endif #include +#include #include #include @@ -29,13 +30,27 @@ namespace osrm::storage { +// Returns directory for OSRM lock files (SHM_LOCK_DIR env var or system temp) +inline std::filesystem::path getLockDir() +{ + if (const char *lock_dir = std::getenv("SHM_LOCK_DIR")) + { + std::filesystem::path dir(lock_dir); + if (!std::filesystem::exists(dir)) + { + throw util::exception("SHM_LOCK_DIR directory does not exist: " + dir.string() + + SOURCE_REF); + } + return dir; + } + return std::filesystem::temp_directory_path(); +} + struct OSRMLockFile { template std::filesystem::path operator()(const IdentifierT &id) { - std::filesystem::path temp_dir = std::filesystem::temp_directory_path(); - std::filesystem::path lock_file = temp_dir / ("osrm-" + std::to_string(id) + ".lock"); - return lock_file; + return getLockDir() / ("osrm-" + std::to_string(id) + ".lock"); } }; diff --git a/src/storage/storage.cpp b/src/storage/storage.cpp index a8e8ba33b66..eb88957181d 100644 --- a/src/storage/storage.cpp +++ b/src/storage/storage.cpp @@ -188,8 +188,7 @@ int Storage::Run(int max_wait, const std::string &dataset_name, bool only_metric util::LogPolicy::GetInstance().Unmute(); - std::filesystem::path lock_path = - std::filesystem::temp_directory_path() / "osrm-datastore.lock"; + std::filesystem::path lock_path = getLockDir() / "osrm-datastore.lock"; if (!std::filesystem::exists(lock_path)) { std::ofstream ofs(lock_path);