gen_server:call returns {error, timeout} while server continues running
0 reputation · 04 Mar 2021, 10:20 UTC
0 reputation · 04 Mar 2021, 10:20 UTC
A caller uses gen_server:call(Server, Request, Timeout) and must treat {error, timeout} as final. No late server reply or timer-generated message should be matched by a later receive in the same process.
Under documented OTP timer and gen_server semantics, erlang:cancel_timer/1 may return true while a {timeout, Ref} message is already in the mailbox, and gen_server:call/3 does not stop the server when the caller gives up. Erlang has no built-in cancellation token, so it is unclear whether caller-side cleanup alone prevents observing a stale timeout or late reply.
Does a successful erlang:cancel_timer/1 guarantee that no matching timeout message remains in the mailbox? Should the caller monitor the server and discard late replies explicitly? Is there a documented OTP pattern for canceling server-side work after a gen_server:call timeout?
37775 reputation · 04 Mar 2021, 15:54 UTC
erlang:cancel_timer/1 returning true does not guarantee the {timeout, Ref} message is absent from the mailbox. The message may have been delivered between timer expiry and the cancel_timer call. The only guarantee is that no future timeout for that Ref will be delivered.erlang:monitor(process, Server) only notifies you if the server crashes. It does not prevent late replies or stop server-side work. You must explicitly discard the late reply {Ref, Reply} using the same Ref that gen_server:call/3 returned.handle_call/3 after the caller times out. The server must cooperate — typically by monitoring the caller in handle_call/3 and aborting when it receives a 'DOWN' message.gen_server:call/3 starts a timer via erlang:start_timer, monitors the server, and waits in a receive with an after clause. On timeout it returns {error, timeout}, demonitors the server, and calls erlang:cancel_timer(Ref).handle_call/3 continues running and will eventually call gen_server:reply(From, Reply), sending {Ref, Reply} to the caller.Ref used in the timer, the monitor, and the server's reply is the same local reference returned by make_ref() on the caller node.After receiving {error, timeout}, execute this sequence before any other receive:
case gen_server:call(Server, Request, Timeout) of
{error, timeout} ->
Ref = get_call_ref(), % the Ref from the call (see note below)
true = erlang:cancel_timer(Ref),
receive
{timeout, Ref} -> ok
after 0 -> ok
end,
% Optional: also flush a late reply if you want a clean mailbox
receive
{Ref, _Reply} -> ok
after 0 -> ok
end,
handle_timeout();
Reply ->
handle_reply(Reply)
end.
Note: gen_server:call/3 does not expose the Ref directly. To obtain it, either:
gen_server:call/4 with the {timeout, Timeout} | Timeout option (OTP 25+) which returns {ok, Reply, Ref} | {error, timeout, Ref}, orerlang:monitor(process, Server) and matches the {'DOWN', Ref, process, Server, _} pattern — but the simplest portable approach is to match the late reply with a _ variable if you don't need the Ref value.In handle_call/3, monitor the caller and abort on 'DOWN':
handle_call(Request, From, State) ->
{ok, MonRef} = erlang:monitor(process, From),
case do_long_work(Request, State, MonRef) of
{ok, Result, NewState} ->
erlang:demonitor(MonRef, [flush]),
{reply, Result, NewState};
{timeout, NewState} ->
erlang:demonitor(MonRef, [flush]),
{reply, timeout, NewState}
end.
do_long_work(_, _, MonRef) ->
receive
{'DOWN', MonRef, process, _From, _Reason} ->
{timeout, current_state()}
after 0 ->
% continue work, but check periodically
continue_work(MonRef)
end.
Without this, a synchronous handle_call/3 doing heavy computation cannot be preempted by the caller's timeout.
The after 0 flush only removes messages already queued at that instant. A timeout message delivered after the flush (tiny race between cancel_timer returning and the receive executing) will remain. In practice the window is negligible because cancel_timer/1 is synchronous with the timer wheel.
Are you on OTP 25+? If yes, use gen_server:call/4 with {timeout, Timeout} to get the Ref directly in the {error, timeout, Ref} tuple, eliminating the need to capture it separately.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.