Skip to content

Commit ba528f0

Browse files
author
José Valim
committed
Add default timeout to elixir_code_server and unify all call points
1 parent fef7ef7 commit ba528f0

File tree

8 files changed

+49
-47
lines changed

8 files changed

+49
-47
lines changed

lib/elixir/lib/code.ex

+8-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Code do
2020
Returns all the loaded files.
2121
"""
2222
def loaded_files do
23-
server_call :loaded
23+
:elixir_code_server.call :loaded
2424
end
2525

2626
@doc """
@@ -30,7 +30,7 @@ defmodule Code do
3030
allowing it to be required again.
3131
"""
3232
def unload_files(files) do
33-
server_cast { :unload_files, files }
33+
:elixir_code_server.cast { :unload_files, files }
3434
end
3535

3636
@doc """
@@ -198,9 +198,9 @@ defmodule Code do
198198
"""
199199
def load_file(file, relative_to // nil) when is_binary(file) do
200200
file = find_file(file, relative_to)
201-
server_call { :acquire, file }
201+
:elixir_code_server.call { :acquire, file }
202202
loaded = :elixir_compiler.file file
203-
server_cast { :loaded, file }
203+
:elixir_code_server.cast { :loaded, file }
204204
loaded
205205
end
206206

@@ -220,14 +220,14 @@ defmodule Code do
220220
def require_file(file, relative_to // nil) when is_binary(file) do
221221
file = find_file(file, relative_to)
222222

223-
case server_call({ :acquire, file }) do
223+
case :elixir_code_server.call({ :acquire, file }) do
224224
:loaded ->
225225
nil
226226
{ :queued, ref } ->
227227
receive do { :elixir_code_server, ^ref, :loaded } -> nil end
228228
:proceed ->
229229
loaded = :elixir_compiler.file file
230-
server_cast { :loaded, file }
230+
:elixir_code_server.cast { :loaded, file }
231231
loaded
232232
end
233233
end
@@ -237,7 +237,7 @@ defmodule Code do
237237
Check compiler_options/1 for more information.
238238
"""
239239
def compiler_options do
240-
server_call :compiler_options
240+
:elixir_code_server.call :compiler_options
241241
end
242242

243243
@doc """
@@ -256,7 +256,7 @@ defmodule Code do
256256
257257
"""
258258
def compiler_options(opts) do
259-
server_call { :compiler_options, opts }
259+
:elixir_code_server.cast { :compiler_options, opts }
260260
end
261261

262262
@doc """
@@ -377,12 +377,4 @@ defmodule Code do
377377
raise LoadError, file: file
378378
end
379379
end
380-
381-
defp server_call(args) do
382-
:gen_server.call(:elixir_code_server, args)
383-
end
384-
385-
defp server_cast(args) do
386-
:gen_server.cast(:elixir_code_server, args)
387-
end
388380
end

lib/elixir/lib/kernel/cli.ex

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ defmodule Kernel.CLI do
1212
argv = lc arg inlist argv, do: list_to_binary(arg)
1313

1414
{ config, argv } = process_argv(argv, Kernel.CLI.Config.new)
15-
:gen_server.call(:elixir_code_server, { :argv, argv })
15+
:elixir_code_server.cast({ :argv, argv })
1616

1717
run fn ->
1818
Enum.map Enum.reverse(config.commands), process_command(&1, config)
19-
:gen_server.cast(:elixir_code_server, :finished)
19+
:elixir_code_server.cast(:finished)
2020
end, config.halt
2121
end
2222

2323
@doc """
24-
Wait until the CLI finishes procesing options.
24+
Wait until the CLI finishes processing options.
2525
"""
2626
def wait_until_finished do
27-
case :gen_server.call(:elixir_code_server, { :wait_until_finished, self }) do
27+
case :elixir_code_server.call({ :wait_until_finished, self }) do
2828
:wait ->
2929
receive do
3030
{ :elixir_code_server, :finished } -> :ok
@@ -74,7 +74,7 @@ defmodule Kernel.CLI do
7474
## Private
7575

7676
defp at_exit(status) do
77-
hooks = :gen_server.call(:elixir_code_server, :flush_at_exit)
77+
hooks = :elixir_code_server.call(:flush_at_exit)
7878

7979
lc hook inlist hooks do
8080
try do

lib/elixir/lib/system.ex

+2-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule System do
5555
"""
5656
@spec argv() :: [String.t]
5757
def argv do
58-
:gen_server.call(:elixir_code_server, :argv)
58+
:elixir_code_server.call :argv
5959
end
6060
6161
@doc """
@@ -155,7 +155,7 @@ defmodule System do
155155
as argument.
156156
"""
157157
def at_exit(fun) when is_function(fun, 1) do
158-
server_call { :at_exit, fun }
158+
:elixir_code_server.cast { :at_exit, fun }
159159
end
160160
161161
@doc """
@@ -311,10 +311,6 @@ defmodule System do
311311

312312
## Helpers
313313

314-
defp server_call(args) do
315-
:gen_server.call(:elixir_code_server, args)
316-
end
317-
318314
defp filter_stacktrace([{ Kernel, :raise, _, _ }|t]), do: t
319315
defp filter_stacktrace(t), do: t
320316
end

lib/elixir/src/elixir_code_server.erl

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-module(elixir_code_server).
2+
-export([call/1, cast/1]).
23
-export([start_link/0, init/1, handle_call/3, handle_cast/2,
34
handle_info/2, terminate/2, code_change/3]).
45
-behavior(gen_server).
@@ -12,8 +13,19 @@
1213
waiting=[]
1314
}).
1415

16+
call(Args) ->
17+
gen_server:call(?MODULE, Args, get_timeout()).
18+
19+
cast(Args) ->
20+
gen_server:cast(?MODULE, Args).
21+
22+
get_timeout() ->
23+
30000.
24+
25+
%% Callbacks
26+
1527
start_link() ->
16-
{ ok, _ } = gen_server:start_link({local, elixir_code_server}, ?MODULE, [], []).
28+
{ ok, _ } = gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
1729

1830
init(_args) ->
1931
{ ok, #elixir_code_server{} }.
@@ -38,16 +50,6 @@ handle_call({ acquire, Path }, From, Config) ->
3850
{ reply, proceed, Config#elixir_code_server{loaded=Queued} }
3951
end;
4052

41-
handle_call({ at_exit, AtExit }, _From, Config) ->
42-
{ reply, ok, Config#elixir_code_server{at_exit=[AtExit|Config#elixir_code_server.at_exit]} };
43-
44-
handle_call({ argv, Argv }, _From, Config) ->
45-
{ reply, ok, Config#elixir_code_server{argv=Argv} };
46-
47-
handle_call({ compiler_options, Options }, _From, Config) ->
48-
Final = orddict:merge(fun(_,_,V) -> V end, Config#elixir_code_server.compiler_options, Options),
49-
{ reply, ok, Config#elixir_code_server{compiler_options=Final} };
50-
5153
handle_call(loaded, _From, Config) ->
5254
{ reply, [F || { F, true } <- Config#elixir_code_server.loaded], Config };
5355

@@ -75,6 +77,16 @@ handle_call(retrieve_module_name, _From, Config) ->
7577
handle_call(_Request, _From, Config) ->
7678
{ reply, undef, Config }.
7779

80+
handle_cast({ at_exit, AtExit }, Config) ->
81+
{ noreply, Config#elixir_code_server{at_exit=[AtExit|Config#elixir_code_server.at_exit]} };
82+
83+
handle_cast({ argv, Argv }, Config) ->
84+
{ noreply, Config#elixir_code_server{argv=Argv} };
85+
86+
handle_cast({ compiler_options, Options }, Config) ->
87+
Final = orddict:merge(fun(_,_,V) -> V end, Config#elixir_code_server.compiler_options, Options),
88+
{ noreply, Config#elixir_code_server{compiler_options=Final} };
89+
7890
handle_cast(finished, Config) ->
7991
Waiting = Config#elixir_code_server.waiting,
8092
[Pid ! { elixir_code_server, finished } || Pid <- lists:reverse(Waiting)],

lib/elixir/src/elixir_compiler.erl

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ get_opt(Key, Dict) ->
1717
end.
1818

1919
get_opts() ->
20-
gen_server:call(elixir_code_server, compiler_options).
20+
elixir_code_server:call(compiler_options).
2121

2222
%% Compiles the given string.
2323

@@ -107,7 +107,7 @@ module(Forms, File, Options, Bootstrap, Callback) when
107107

108108
core() ->
109109
application:start(elixir),
110-
gen_server:call(elixir_code_server, { compiler_options, [{docs,false},{internal,true}] }),
110+
elixir_code_server:cast({ compiler_options, [{docs,false},{internal,true}] }),
111111
[core_file(File) || File <- core_main()].
112112

113113
%% HELPERS
@@ -139,10 +139,10 @@ module_form(Fun, Exprs, Line, File, Module, Vars) when
139139
%% Generate module names from code server.
140140

141141
retrieve_module_name() ->
142-
gen_server:call(elixir_code_server, retrieve_module_name).
142+
elixir_code_server:call(retrieve_module_name).
143143

144144
return_module_name(I) ->
145-
gen_server:cast(elixir_code_server, { return_module_name, I }).
145+
elixir_code_server:cast({ return_module_name, I }).
146146

147147
%% Receives a module Binary and outputs it in the given path.
148148

lib/ex_unit/lib/ex_unit/cli_formatter.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule ExUnit.CLIFormatter do
55
"""
66

77
@behaviour ExUnit.Formatter
8+
@timeout 30_000
89
use GenServer.Behaviour
910

1011
import Exception, only: [format_entry: 1]
@@ -18,7 +19,7 @@ defmodule ExUnit.CLIFormatter do
1819
end
1920

2021
def suite_finished(id) do
21-
:gen_server.call(id, :suite_finished)
22+
:gen_server.call(id, :suite_finished, @timeout)
2223
end
2324

2425
def case_started(_id, _test_case) do

lib/ex_unit/lib/ex_unit/server.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ExUnit.Server do
22
@moduledoc false
33

4+
@timeout 30_000
45
use GenServer.Behaviour
56

67
defrecord Config, options: [], async_cases: [], sync_cases: [], after_spawn: []
@@ -30,15 +31,15 @@ defmodule ExUnit.Server do
3031
## After run API
3132

3233
def options do
33-
:gen_server.call(__MODULE__, :options)
34+
:gen_server.call(__MODULE__, :options, @timeout)
3435
end
3536

3637
def cases do
37-
:gen_server.call(__MODULE__, :cases)
38+
:gen_server.call(__MODULE__, :cases, @timeout)
3839
end
3940

4041
def run_after_spawn do
41-
funs = :gen_server.call(__MODULE__, :after_spawn)
42+
funs = :gen_server.call(__MODULE__, :after_spawn, @timeout)
4243
lc fun inlist funs, do: fun.()
4344
end
4445

lib/mix/lib/mix/server.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Mix.Server do
1212
end
1313

1414
def call(arg) do
15-
:gen_server.call(__MODULE__, arg)
15+
:gen_server.call(__MODULE__, arg, 30_000)
1616
end
1717

1818
def cast(arg) do

0 commit comments

Comments
 (0)