From 4bebe39975aab28962ac3850aa25a7d768c349bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 22 Oct 2017 14:53:04 +0100 Subject: Ensure stream terminate is called when switching protocols --- src/cowboy_http.erl | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/cowboy_http.erl') diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 4ff9f8c..2ed0840 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -878,22 +878,17 @@ commands(State0=#state{ref=Ref, parent=Parent, socket=Socket, transport=Transpor %% @todo This should be the last stream running otherwise we need to wait before switching. %% @todo If there's streams opened after this one, fail instead of 101. State = cancel_timeout(State0), - %% @todo When we actually do the upgrade, we only have the one stream left, plus - %% possibly some processes terminating. We need a smart strategy for handling the - %% children shutdown. We can start with brutal_kill and discarding the EXIT messages - %% received before switching to Websocket. Something better would be to let the - %% stream processes finish but that implies the Websocket module to know about - %% them and filter the messages. For now, kill them all and discard all messages - %% in the mailbox. - + %% Send a 101 response, then terminate the stream. + State = #state{streams=Streams} = commands(State, StreamID, [{inform, 101, Headers}]), + #stream{state=StreamState} = lists:keyfind(StreamID, #stream.id, Streams), + %% @todo We need to shutdown processes here first. + stream_call_terminate(StreamID, switch_protocol, StreamState), + %% Terminate children processes and flush any remaining messages from the mailbox. cowboy_children:terminate(Children), - flush(), - %% Everything good, upgrade! - _ = commands(State, StreamID, [{inform, 101, Headers}]), %% @todo This is no good because commands return a state normally and here it doesn't %% we need to let this module go entirely. Perhaps it should be handled directly in - %% cowboy_clear/cowboy_tls? Perhaps not. We do want that Buffer. + %% cowboy_clear/cowboy_tls? Protocol:takeover(Parent, Ref, Socket, Transport, Opts, <<>>, InitialState); %% Stream shutdown. commands(State, StreamID, [stop|Tail]) -> -- cgit v1.2.3