Skip to content

Commit 4ec0138

Browse files
committed
Fix race condition in Tesla.Mock.agent_set
1 parent 727cb0f commit 4ec0138

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

lib/tesla/mock.ex

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,24 @@ defmodule Tesla.Mock do
238238
end
239239

240240
defp agent_set(fun) do
241-
case Process.whereis(__MODULE__) do
242-
nil ->
243-
ExUnit.Callbacks.start_supervised!(
244-
%{
245-
id: __MODULE__,
246-
start: {Agent, :start_link, [fn -> fun end, [{:name, __MODULE__}]]}
247-
},
248-
[]
249-
)
250-
251-
pid ->
241+
mock_start_result =
242+
ExUnit.Callbacks.start_supervised(
243+
%{
244+
id: __MODULE__,
245+
start: {Agent, :start_link, [fn -> fun end, [{:name, __MODULE__}]]}
246+
},
247+
[]
248+
)
249+
250+
case mock_start_result do
251+
{:ok, _pid} ->
252+
:ok
253+
254+
{:error, {:already_started, pid}} ->
252255
Agent.update(pid, fn _ -> fun end)
256+
257+
other ->
258+
raise other
253259
end
254260
end
255261

0 commit comments

Comments
 (0)