Skip to content

Commit

Permalink
sim: manage: exclusively lock sim directory while killing other sim i…
Browse files Browse the repository at this point in the history
…nstances

If two "manage start" were run it might have end up in situation where both
manage killed each other before any of them started Sim.

Fixes #30
  • Loading branch information
varqox committed Oct 13, 2024
1 parent d21c16d commit c04458f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions subprojects/sim/src/manage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <simlib/process.hh>
#include <simlib/string_view.hh>
#include <simlib/syscalls.hh>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread>
Expand Down Expand Up @@ -96,6 +97,16 @@ struct SimPaths {

static void stop() {
SimPaths paths;
// Lock the sim directory during killing to avoid situation where two instances wanting to start
// sim kill each other before starting sim.
auto sim_dir = path_dirpath(paths.manage).to_string();
auto sim_dir_fd = FileDescriptor{sim_dir.c_str(), O_RDONLY};
if (flock(sim_dir_fd, LOCK_EX)) {
errlog("flock()", errmsg());
_exit(EXIT_FAILURE);
}
// Lock is freed when the sim_dir_fd is closed

// First kill manage so that it won't restart servers
kill_processes_by_exec({paths.manage}, std::chrono::seconds(1), true);
// Kill servers
Expand Down

0 comments on commit c04458f

Please sign in to comment.