Skip to content
Closed
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
48 changes: 41 additions & 7 deletions src/cow_http2_machine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,49 @@ setting_from_opt(Settings, Opts, OptName, SettingName, Default) ->

-spec terminate(State::http2_machine()) -> ok.
terminate(#http2_machine{preface_timer=PTRef, settings_timer=STRef}) ->
_ = case PTRef of
undefined -> ok;
_ -> erlang:cancel_timer(PTRef, [{async, true}, {info, false}])
end,
_ = case STRef of
undefined -> ok;
_ -> erlang:cancel_timer(STRef, [{async, true}, {info, false}])
cancel_timeout(PTRef),
cancel_timeout(STRef).

cancel_timeout(TRef) ->
ok = case TRef of
undefined ->
ok;
_ ->
%% Do a synchronous cancel and remove the message if any
%% to avoid receiving stray messages.
_ = erlang:cancel_timer(TRef, [{async, false}, {info, false}]),
receive
{timeout, TRef, _} -> ok
after 0 ->
ok
end
end.

-ifdef(TEST).

terminate_removes_stray_timeout_messages_test() ->
PTRef = erlang:start_timer(5000, self(), {?MODULE, undefined, preface_timeout}),
STRef = erlang:start_timer(5000, self(), {?MODULE, undefined, settings_timeout}),
PTMsg = {timeout, PTRef, {?MODULE, undefined, preface_timeout}},
STMsg = {timeout, STRef, {?MODULE, undefined, settings_timeout}},
self() ! PTMsg,
self() ! STMsg,
terminate(#http2_machine{preface_timer=PTRef, settings_timer=STRef}),
[] = collect_timeout_messages(PTRef, STRef),
ok.

collect_timeout_messages(PTRef, STRef) ->
receive
{timeout, PTRef, {?MODULE, undefined, preface_timeout}} ->
[preface_timeout|collect_timeout_messages(PTRef, STRef)];
{timeout, STRef, {?MODULE, undefined, settings_timeout}} ->
[settings_timeout|collect_timeout_messages(PTRef, STRef)]
after 0 ->
[]
end.

-endif.

-spec init_stream(binary(), State)
-> {ok, cow_http2:streamid(), State} when State::http2_machine().
init_stream(Method, State=#http2_machine{mode=client, local_streamid=LocalStreamID,
Expand Down