Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an environment variable to optionally disable compilation locking #14129

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ defmodule Mix do
* `MIX_INSTALL_DIR` *(since v1.12.0)* - specifies directory where `Mix.install/2` keeps
install cache
* `MIX_OS_CONCURRENCY_LOCK` - when set to `0` or `false`, disables mix compilation locking.
While not recommended, this may be necessary in cases where hard links or TCP sockets are
not available. When opting for this behaviour, make sure to not start concurrent compilations
of the same project.
* `MIX_PATH` - appends extra code paths
* `MIX_PROFILE` - a list of comma-separated Mix tasks to profile the time spent on
Expand Down
21 changes: 14 additions & 7 deletions lib/mix/lib/mix/sync/lock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ defmodule Mix.Sync.Lock do
This function can also be called if this process already has the
lock. In such case the function is executed immediately.
When the `MIX_OS_CONCURRENCY_LOCK` environment variable is set to
a falsy value, the lock is ignored and the function is executed
immediately.
## Options
* `:on_taken` - a one-arity function called if the lock is held
Expand All @@ -96,9 +100,9 @@ defmodule Mix.Sync.Lock do
path = Path.join([System.tmp_dir!(), "mix_lock", hash])

pdict_key = {__MODULE__, path}
has_lock? = Process.get(pdict_key)
has_lock? = Process.get(pdict_key, false)

if has_lock? do
if has_lock? or lock_disabled?() do
fun.()
else
lock = lock(path, opts[:on_taken])
Expand All @@ -115,6 +119,8 @@ defmodule Mix.Sync.Lock do
end
end

defp lock_disabled?(), do: System.get_env("MIX_OS_CONCURRENCY_LOCK") in ~w(0 false)

defp lock(path, on_taken) do
File.mkdir_p!(path)

Expand Down Expand Up @@ -198,11 +204,12 @@ defmodule Mix.Sync.Lock do
:invalidated

{:error, reason} ->
raise File.LinkError,
reason: reason,
action: "create hard link",
existing: port_path,
new: lock_path
Mix.raise("""
could not create hard link from #{port_path} to "#{lock_path}: #{:file.format_error(reason)}.
Hard link support is required for Mix compilation locking. If your system \
does not support hard links, set MIX_OS_CONCURRENCY_LOCK=0\
""")
end
end

Expand Down
Loading