d659e89
commit d659e89
1 file changed
+18 -11
Showing 1 changed file+18-11
▼ELIXIR/lib/cortexa/workspace.ex
+18-11
| @@ -45,21 +45,28 @@ defmodule Cortexa.Workspace do | ||
45 | 45 | lock_path = Path.join(ws.gcc_dir, ".lock") |
46 | 46 | File.mkdir_p!(Path.dirname(lock_path)) |
47 | 47 | |
48 | - case :prim_file.open(String.to_charlist(lock_path), [:write, :raw]) do | |
48 | + max_retries = 50 | |
49 | + retry_delay = 100 | |
50 | + | |
51 | + acquire_lock(lock_path, max_retries, retry_delay, fun) | |
52 | + end | |
53 | + | |
54 | + defp acquire_lock(_lock_path, 0, _delay, _fun) do | |
55 | + raise "Failed to acquire lock: max retries exceeded" | |
56 | + end | |
57 | + | |
58 | + defp acquire_lock(lock_path, retries, delay, fun) do | |
59 | + case File.open(lock_path, [:exclusive, :write]) do | |
49 | 60 | {:ok, fd} -> |
50 | 61 | try do |
51 | - case :prim_file.lock(fd, :exclusive, :infinity) do | |
52 | - :ok -> | |
53 | - fun.() | |
54 | - {:error, reason} -> | |
55 | - raise "Failed to acquire lock: #{inspect(reason)}" | |
56 | - end | |
62 | + fun.() | |
57 | 63 | after |
58 | - :prim_file.unlock(fd) | |
59 | - :prim_file.close(fd) | |
64 | + File.close(fd) | |
65 | + File.rm(lock_path) | |
60 | 66 | end |
61 | - {:error, reason} -> | |
62 | - raise "Failed to open lock file: #{inspect(reason)}" | |
67 | + {:error, _} -> | |
68 | + Process.sleep(delay) | |
69 | + acquire_lock(lock_path, retries - 1, delay, fun) | |
63 | 70 | end |
64 | 71 | end |
65 | 72 | |
66 | 73 | |