Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1551] Handle external exits of processes #1514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/cowboy_stream_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ info(StreamID, Exit={'EXIT', Pid, {Reason, Stacktrace}}, State=#state{ref=Ref, p
do_info(StreamID, Exit, [
{error_response, 500, #{<<"content-length">> => <<"0">>}, <<>>}
|Commands], State);
info(StreamID, Exit={'EXIT', Pid, Reason}, State=#state{ref=Ref, pid=Pid}) ->
Commands0 = [{internal_error, Exit, 'Stream process crashed.'}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use exited rather than crashed.

Commands = case Reason of
normal -> Commands0;
shutdown -> Commands0;
{shutdown, _} -> Commands0;
_ -> [{log, error,
"Ranch listener ~p, connection process ~p, stream ~p "
"had its request process ~p exit with reason "
"~999999p~n",
[Ref, self(), StreamID, Pid, Reason]}
|Commands0]
end,
do_info(StreamID, Exit, [
{error_response, 500, #{<<"content-length">> => <<"0">>}, <<>>}
|Commands], State);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably extract all this into a separate function, with slightly different behavior depending on the presence of a stacktrace (use undefined for the exit case).

%% Request body, auto mode, no body buffered.
info(StreamID, Info={read_body, Pid, Ref, auto, infinity}, State=#state{read_body_buffer= <<>>}) ->
do_info(StreamID, Info, [], State#state{
Expand Down
8 changes: 7 additions & 1 deletion test/handlers/crash_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ init(_, no_reply) ->
init(Req, reply) ->
_ = cowboy_req:reply(200, Req),
ct_helper:ignore(?MODULE, init, 2),
error(crash).
error(crash);
init(_, internal_exit) ->
ct_helper:ignore(?MODULE, init, 2),
exit(internal_exit);
init(_, external_exit) ->
ct_helper:ignore(?MODULE, init, 2),
exit(self(), external_exit).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ignore function is about crashes not exits. This handler is about crashes as well. I don't think this belongs here. A better place for this test would be in stream_handler_SUITE similar to this test https://github.com/ninenines/cowboy/blob/master/test/stream_handler_SUITE.erl#L341-L362 (just get the pid and exit the process immediately, the test shouldn't be that long). It's OK to only test exit/2 in this case.

117 changes: 117 additions & 0 deletions test/metrics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ init_routes(_) -> [
{"/", hello_h, []},
{"/crash/no_reply", crash_h, no_reply},
{"/crash/reply", crash_h, reply},
{"/crash/internal_exit", crash_h, internal_exit},
{"/crash/external_exit", crash_h, external_exit},
{"/default", default_h, []},
{"/full/:key", echo_h, []},
{"/resp/:key[/:arg]", resp_h, []},
Expand Down Expand Up @@ -542,3 +544,118 @@ error_response_after_reply(Config) ->
%% All good!
gun:close(ConnPid)
end.

error_response_after_internal_exit(Config) ->
doc("Confirm metrics are correct when an error_response command is returned "
"after internal exit."),
%% Perform a GET request.
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/crash/internal_exit", [
{<<"accept-encoding">>, <<"gzip">>},
{<<"x-test-pid">>, pid_to_list(self())}
]),
{response, fin, 500, RespHeaders} = gun:await(ConnPid, Ref, infinity),
timer:sleep(100),
%% Receive the metrics and validate them.
receive
{metrics, From, Metrics} ->
%% Ensure the timestamps are in the expected order.
#{
req_start := ReqStart, req_end := ReqEnd,
resp_start := RespStart, resp_end := RespEnd
} = Metrics,
true = (ReqStart =< RespStart)
and (RespStart =< RespEnd)
and (RespEnd =< ReqEnd),
%% We didn't send a body.
#{
req_body_start := undefined,
req_body_end := undefined,
req_body_length := 0
} = Metrics,
%% We got a 500 response without a body.
#{
resp_status := 500,
resp_headers := ExpectedRespHeaders,
resp_body_length := 0
} = Metrics,
ExpectedRespHeaders = maps:from_list(RespHeaders),
%% The request process executed normally.
#{procs := Procs} = Metrics,
[{_, #{
spawn := ProcSpawn,
exit := ProcExit,
reason := {internal_exit, _StackTrace}
}}] = maps:to_list(Procs),
true = ProcSpawn =< ProcExit,
%% Confirm other metadata are as expected.
#{
ref := _,
pid := From,
streamid := 1,
reason := {internal_error, {'EXIT', _Pid, {internal_exit, _StackTrace}}, 'Stream process crashed.'},
req := #{},
informational := [],
user_data := #{}
} = Metrics,
%% All good!
gun:close(ConnPid)
end.

error_response_after_external_exit(Config) ->
doc("Confirm metrics are correct when an error_response command is returned "
"after external exit."),
%% Perform a GET request.
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/crash/external_exit", [
{<<"accept-encoding">>, <<"gzip">>},
{<<"x-test-pid">>, pid_to_list(self())}
]),
{response, fin, 500, RespHeaders} = gun:await(ConnPid, Ref, infinity),
timer:sleep(100),
%% Receive the metrics and validate them.
receive
{metrics, From, Metrics} ->
%% Ensure the timestamps are in the expected order.
#{
req_start := ReqStart, req_end := ReqEnd,
resp_start := RespStart, resp_end := RespEnd
} = Metrics,
true = (ReqStart =< RespStart)
and (RespStart =< RespEnd)
and (RespEnd =< ReqEnd),
%% We didn't send a body.
#{
req_body_start := undefined,
req_body_end := undefined,
req_body_length := 0
} = Metrics,
%% We got a 500 response without a body.
#{
resp_status := 500,
resp_headers := ExpectedRespHeaders,
resp_body_length := 0
} = Metrics,
ExpectedRespHeaders = maps:from_list(RespHeaders),
%% The request process executed normally.
#{procs := Procs} = Metrics,
[{_, #{
spawn := ProcSpawn,
exit := ProcExit,
reason := external_exit
}}] = maps:to_list(Procs),
true = ProcSpawn =< ProcExit,
%% Confirm other metadata are as expected.
#{
ref := _,
pid := From,
streamid := 1,
reason := {internal_error, {'EXIT', _Pid, external_exit}, 'Stream process crashed.'},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, checking this still seems valuable. So I would leave this test. I would just do the exit from the test case instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright makes sense. On it.

req := #{},
informational := [],
user_data := #{}
} = Metrics,
%% All good!
gun:close(ConnPid)
end.