Skip to content

Commit f18c3cc

Browse files
committed
Merge pull request #1980 from pguyot/w46/fix-gen-api
Change `gen:call/4` signature to match OTP's These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents d4877b9 + ff20686 commit f18c3cc

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

libs/estdlib/src/gen.erl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@
3636
-type server_ref() :: atom() | pid().
3737
-type from() :: {pid(), reference()}.
3838

39-
%% @private
39+
%%-----------------------------------------------------------------------------
40+
%% @doc Perform a call on a gen server. This API not documented by OTP,
41+
%% yet Elixir uses it, so this function matches the current OTP-28 behavior.
42+
%% @end
43+
%% @param ServerRef the server to call
44+
%% @param Label the label for the message, typically `system' or `$gen_call'
45+
%% @param Request the message to send
46+
%% @param Timeout timeout for sending the message
47+
%% @returns `{ok, Result}' or raises an exit exception
48+
%%-----------------------------------------------------------------------------
4049
-spec call(ServerRef :: server_ref(), Label :: atom(), Request :: term(), Timeout :: timeout()) ->
41-
Reply :: term() | {error, Reason :: term()}.
50+
{ok, Reply :: term()} | {error, Reason :: term()}.
4251
call(ServerRef, Label, Request, Timeout) ->
4352
MonitorRef = monitor(process, ServerRef),
4453
ok =
@@ -59,7 +68,7 @@ call(ServerRef, Label, Request, Timeout) ->
5968
exit(Atom);
6069
{MonitorRef, Reply} ->
6170
demonitor(MonitorRef, [flush]),
62-
Reply
71+
{ok, Reply}
6372
after Timeout ->
6473
% If Timeout is small enough (0), the error message might be timeout
6574
% instead of noproc as there could be a race condition with the monitor.

libs/estdlib/src/gen_server.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ call(ServerRef, Request) ->
422422
-spec call(ServerRef :: server_ref(), Request :: term(), TimeoutMs :: timeout()) ->
423423
Reply :: term() | {error, Reason :: term()}.
424424
call(ServerRef, Request, TimeoutMs) ->
425-
try
426-
gen:call(ServerRef, '$gen_call', Request, TimeoutMs)
425+
try gen:call(ServerRef, '$gen_call', Request, TimeoutMs) of
426+
{ok, Result} -> Result
427427
catch
428428
exit:Reason ->
429429
exit({Reason, {?MODULE, ?FUNCTION_NAME, [ServerRef, Request, TimeoutMs]}})

libs/estdlib/src/gen_statem.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ call(ServerRef, Request) ->
210210
-spec call(ServerRef :: server_ref(), Request :: term(), Timeout :: timeout()) ->
211211
Reply :: term() | {error, Reason :: term()}.
212212
call(ServerRef, Request, Timeout) ->
213-
try
214-
gen:call(ServerRef, '$gen_call', Request, Timeout)
213+
try gen:call(ServerRef, '$gen_call', Request, Timeout) of
214+
{ok, Reply} -> Reply
215215
catch
216216
exit:Reason ->
217217
exit({Reason, {?MODULE, ?FUNCTION_NAME, [ServerRef, Request]}})

libs/estdlib/src/sys.erl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ change_code(Name, Module, OldVsn, Extra) ->
130130
Timeout :: timeout()
131131
) -> ok | {error, any()}.
132132
change_code(Name, Module, OldVsn, Extra, Timeout) ->
133-
gen:call(Name, system, {change_code, Module, OldVsn, Extra}, Timeout).
133+
{ok, Reply} = gen:call(Name, system, {change_code, Module, OldVsn, Extra}, Timeout),
134+
Reply.
134135

135136
%% @equiv get_state(Name, 5000)
136137
-spec get_state(Name :: name()) -> any().
@@ -147,7 +148,8 @@ get_state(Name) ->
147148
%%-----------------------------------------------------------------------------
148149
-spec get_state(Name :: name(), timeout()) -> any().
149150
get_state(Name, Timeout) ->
150-
case gen:call(Name, system, get_state, Timeout) of
151+
{ok, Reply} = gen:call(Name, system, get_state, Timeout),
152+
case Reply of
151153
{ok, State} -> State;
152154
{error, Reason} -> error(Reason)
153155
end.
@@ -168,7 +170,8 @@ get_status(Name) ->
168170
-spec get_status(Name :: name(), Timeout :: timeout()) ->
169171
{status, pid(), {module, module()}, [SItem :: any()]}.
170172
get_status(Name, Timeout) ->
171-
gen:call(Name, system, get_status, Timeout).
173+
{ok, Reply} = gen:call(Name, system, get_status, Timeout),
174+
Reply.
172175

173176
%% @equiv replace_state(Name, StateFun, 5000)
174177
-spec replace_state(Name :: name(), StateFun :: fun((any()) -> any())) -> ok.
@@ -185,7 +188,8 @@ replace_state(Name, StateFun) ->
185188
%%-----------------------------------------------------------------------------
186189
-spec replace_state(Name :: name(), StateFun :: fun((any()) -> any()), Timeout :: timeout()) -> ok.
187190
replace_state(Name, StateFun, Timeout) ->
188-
case gen:call(Name, system, {replace_state, StateFun}, Timeout) of
191+
{ok, Reply} = gen:call(Name, system, {replace_state, StateFun}, Timeout),
192+
case Reply of
189193
{ok, State} -> State;
190194
{error, Reason} -> error(Reason)
191195
end.
@@ -204,7 +208,8 @@ resume(Name) ->
204208
%%-----------------------------------------------------------------------------
205209
-spec resume(Name :: name(), Timeout :: timeout()) -> ok.
206210
resume(Name, Timeout) ->
207-
gen:call(Name, system, resume, Timeout).
211+
{ok, ok} = gen:call(Name, system, resume, Timeout),
212+
ok.
208213

209214
%% @equiv suspend(Name, 5000)
210215
-spec suspend(Name :: name()) -> ok.
@@ -221,7 +226,8 @@ suspend(Name) ->
221226
%%-----------------------------------------------------------------------------
222227
-spec suspend(Name :: name(), Timeout :: timeout()) -> ok.
223228
suspend(Name, Timeout) ->
224-
gen:call(Name, system, suspend, Timeout).
229+
{ok, ok} = gen:call(Name, system, suspend, Timeout),
230+
ok.
225231

226232
%% @equiv terminate(Name, Reason, 5000)
227233
-spec terminate(Name :: name(), Reason :: any()) -> ok.
@@ -238,7 +244,8 @@ terminate(Name, Reason) ->
238244
%%-----------------------------------------------------------------------------
239245
-spec terminate(Name :: name(), Reason :: any(), Timeout :: timeout()) -> ok.
240246
terminate(Name, Reason, Timeout) ->
241-
gen:call(Name, system, {terminate, Reason}, Timeout).
247+
{ok, ok} = gen:call(Name, system, {terminate, Reason}, Timeout),
248+
ok.
242249

243250
%% @equiv trace(Name, Flag, 5000)
244251
-spec trace(Name :: name(), Flag :: boolean()) -> ok.
@@ -255,7 +262,8 @@ trace(Name, Flag) ->
255262
%%-----------------------------------------------------------------------------
256263
-spec trace(Name :: name(), Flag :: boolean(), Timeout :: timeout()) -> ok.
257264
trace(Name, Flag, Timeout) ->
258-
gen:call(Name, system, {debug, {trace, Flag}}, Timeout).
265+
{ok, ok} = gen:call(Name, system, {debug, {trace, Flag}}, Timeout),
266+
ok.
259267

260268
%%-----------------------------------------------------------------------------
261269
%% Process Implementation Functions

0 commit comments

Comments
 (0)