Skip to content

Commit 5a5e385

Browse files
jonatankloskojosevalim
authored andcommitted
Improve user detection in lock and pubsub implementation (#14801)
1 parent cd580f6 commit 5a5e385

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

lib/mix/lib/mix/sync/lock.ex

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ defmodule Mix.Sync.Lock do
7272
@probe_data "mixlock"
7373
@probe_data_size byte_size(@probe_data)
7474
@probe_timeout_ms 5_000
75-
@version 2
7675

7776
@typedoc """
7877
Options for `with_lock/3`.
@@ -132,12 +131,7 @@ defmodule Mix.Sync.Lock do
132131

133132
defp base_path do
134133
# We include user in the dir to avoid permission conflicts across users
135-
user = System.get_env("USER", "default")
136-
137-
Path.join(
138-
System.tmp_dir!(),
139-
"mix_lock_#{@version}_#{Base.url_encode64(user, padding: false)}"
140-
)
134+
Path.join(System.tmp_dir!(), "mix_lock_user#{Mix.Utils.detect_user_id!()}")
141135
end
142136

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

lib/mix/lib/mix/sync/pubsub.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ defmodule Mix.Sync.PubSub do
282282

283283
defp base_path do
284284
# We include user in the dir to avoid permission conflicts across users
285-
user = System.get_env("USER", "default")
286-
Path.join(System.tmp_dir!(), "mix_pubsub_#{Base.url_encode64(user, padding: false)}")
285+
Path.join(System.tmp_dir!(), "mix_pubsub_user#{Mix.Utils.detect_user_id!()}")
287286
end
288287

289288
defp recv(socket, size, timeout \\ :infinity) do

lib/mix/lib/mix/utils.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,37 @@ defmodule Mix.Utils do
874874

875875
[proxy_auth: {user, pass}]
876876
end
877+
878+
@doc """
879+
Returns the user id of the currently running process.
880+
881+
The user id is obtained by creating a temporary file and checking
882+
it's UID. Note that the UID may be `nil` on non-Unix systems.
883+
"""
884+
def detect_user_id!() do
885+
case Mix.State.fetch(:user_id) do
886+
{:ok, uid} ->
887+
uid
888+
889+
:error ->
890+
dir = System.tmp_dir!()
891+
File.mkdir_p!(dir)
892+
rand = :crypto.strong_rand_bytes(3) |> Base.url_encode64()
893+
path = Path.join(dir, "mix_user_check_#{System.os_time()}_#{rand}")
894+
895+
uid =
896+
try do
897+
File.touch!(path)
898+
File.stat!(path)
899+
else
900+
%{uid: :undefined} -> nil
901+
%{uid: uid} -> uid
902+
after
903+
File.rm(path)
904+
end
905+
906+
Mix.State.put(:user_id, uid)
907+
uid
908+
end
909+
end
877910
end

0 commit comments

Comments
 (0)