aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-11-03 17:52:18 +0000
committerLoïc Hoguin <[email protected]>2017-11-03 17:52:18 +0000
commitda304799fee8ac93ce83d9349bb8128af3985671 (patch)
treee33da406c01c4dadc2f569d0b179b587934e41cd /src
parentaf58babd940f0add48c9ff6e47a4196f91417728 (diff)
downloadcowboy-da304799fee8ac93ce83d9349bb8128af3985671.tar.gz
cowboy-da304799fee8ac93ce83d9349bb8128af3985671.tar.bz2
cowboy-da304799fee8ac93ce83d9349bb8128af3985671.zip
Add a terminate event to the tracer and more tests
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_tracer_h.erl14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/cowboy_tracer_h.erl b/src/cowboy_tracer_h.erl
index 1f054e3..b69faf4 100644
--- a/src/cowboy_tracer_h.erl
+++ b/src/cowboy_tracer_h.erl
@@ -80,7 +80,7 @@ init_tracer(StreamID, Req, Opts=#{tracer_match_specs := List, tracer_callback :=
start_tracer(StreamID, Req, Opts)
end;
%% When the options tracer_match_specs or tracer_callback
-%% arenot provided we do not enable tracing.
+%% are not provided we do not enable tracing.
init_tracer(_, _, _) ->
no_tracing.
@@ -138,6 +138,8 @@ start_tracer(StreamID, Req, Opts) ->
-spec tracer_process(_, _, _) -> no_return().
tracer_process(StreamID, Req=#{pid := Parent}, Opts=#{tracer_callback := Fun}) ->
+ %% This is necessary because otherwise the tracer could stop
+ %% before it has finished processing the events in its queue.
process_flag(trap_exit, true),
State = Fun(init, {StreamID, Req, Opts}),
tracer_loop(Parent, Fun, State).
@@ -148,7 +150,7 @@ tracer_loop(Parent, Fun, State) ->
Fun(Msg, State),
tracer_loop(Parent, Fun, State);
{'EXIT', Parent, Reason} ->
- exit(Reason);
+ tracer_terminate(Reason, Fun, State);
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {Fun, State});
Msg ->
@@ -157,6 +159,10 @@ tracer_loop(Parent, Fun, State) ->
tracer_loop(Parent, Fun, State)
end.
+tracer_terminate(Reason, Fun, State) ->
+ _ = Fun(terminate, State),
+ exit(Reason).
+
%% System callbacks.
-spec system_continue(pid(), _, {fun(), any()}) -> no_return().
@@ -164,8 +170,8 @@ system_continue(Parent, _, {Fun, State}) ->
tracer_loop(Parent, Fun, State).
-spec system_terminate(any(), _, _, _) -> no_return().
-system_terminate(Reason, _, _, _) ->
- exit(Reason).
+system_terminate(Reason, _, _, {Fun, State}) ->
+ tracer_terminate(Reason, Fun, State).
-spec system_code_change(Misc, _, _, _) -> {ok, Misc} when Misc::any().
system_code_change(Misc, _, _, _) ->