summaryrefslogtreecommitdiff
path: root/p11p-daemon/src/p11p_remote.erl
diff options
context:
space:
mode:
authorLinus Nordberg <linus@sunet.se>2019-07-01 14:55:42 +0200
committerLinus Nordberg <linus@sunet.se>2019-07-01 14:55:42 +0200
commit1436c870dc5dd4b09e8b0fcf50df36dc32f8b10c (patch)
tree925f48138ac75effa2321e9633315948959c5ce3 /p11p-daemon/src/p11p_remote.erl
parentb4d03448f320146363b80a99b1ea07e4ba67f79d (diff)
make events casts instead of calls; ask remotes to stop themselves
casts bc don't risk blocking avoiding gen_server:stop/1 so that we terminate processes which we are not sure are alive
Diffstat (limited to 'p11p-daemon/src/p11p_remote.erl')
-rw-r--r--p11p-daemon/src/p11p_remote.erl21
1 files changed, 16 insertions, 5 deletions
diff --git a/p11p-daemon/src/p11p_remote.erl b/p11p-daemon/src/p11p_remote.erl
index 84299c5..9047cad 100644
--- a/p11p-daemon/src/p11p_remote.erl
+++ b/p11p-daemon/src/p11p_remote.erl
@@ -14,7 +14,7 @@
%% API.
-export([start_link/3]).
--export([request/2, add_to_outbuf/2]).
+-export([request/2, add_to_outbuf/2, stop/2]).
-include("p11p_rpc.hrl").
@@ -48,11 +48,20 @@ request(Remote, Request) ->
add_to_outbuf(Remote, Data) ->
gen_server:call(Remote, {add_to_outbuf, Data}).
+%% Use stop/1 instead of gen_server:stop/1 if you're uncertain whether
+%% Pid is alive or not. An example of when that can happen is when the
+%% manager receiving a server_event about a lost client. If the server
+%% process terminated on request from a remote which has timed out on
+%% an rpc call, chances are that the remote has already terminated by
+%% the time the manager is to act on the lost client.
+stop(Pid, Reason) ->
+ gen_server:cast(Pid, {stop, Reason}).
+
%% Genserver callbacks.
init([TokName, ModPath]) ->
Port = open_port({spawn_executable, ?P11KITREMOTE_PATH},
[stream, exit_status, {args, [ModPath, "-v"]}]),
- lager:debug("~p: ~s: New port: ~p", [self(), ?P11KITREMOTE_PATH, Port]),
+ lager:debug("~p: ~s: new remote port: ~p", [self(), ?P11KITREMOTE_PATH, Port]),
{ok, #state{port = Port, token = TokName}}.
handle_call({add_to_outbuf, Data}, _From, State) ->
@@ -65,8 +74,10 @@ handle_call(Request, _From, State) ->
lager:debug("~p: Unhandled call: ~p~n", [self(), Request]),
{reply, unhandled, State}.
-handle_cast(Request, State) ->
- lager:debug("~p: Unhandled cast: ~p~n", [self(), Request]),
+handle_cast({stop, Reason}, State) ->
+ {stop, Reason, State};
+handle_cast(Cast, State) ->
+ lager:debug("~p: unhandled cast: ~p~n", [self(), Cast]),
{noreply, State}.
%% TODO: dedup code w/ p11p_server
@@ -78,7 +89,7 @@ handle_info({Port, {data, Data}}, #state{msg = Msg} = State) when Port == State#
{noreply, handle_remote_data(State, Msg, Data)};
handle_info({timeout, Timer, Port}, #state{token = TokName, replyto = Server} = State) when Port == State#state.port, Timer == State#state.timer ->
lager:info("~p: rpc request timed out, exiting", [self()]),
- ok = p11p_remote_manager:server_event(timeout, [TokName, Server]),
+ p11p_remote_manager:server_event(timeout, [TokName, Server]),
NewState = State#state{timer = undefined},
{stop, normal, NewState};
handle_info(Info, State) ->