Browse Source

Make sure ensure_window doesn't crash if stream is closed

Loïc Hoguin 5 years ago
parent
commit
3d163136e5
1 changed files with 12 additions and 5 deletions
  1. 12 5
      src/cow_http2_machine.erl

+ 12 - 5
src/cow_http2_machine.erl

@@ -1381,12 +1381,19 @@ ensure_window(Size, State=#http2_machine{opts=Opts, remote_window=RemoteWindow})
 -spec ensure_window(cow_http2:streamid(), non_neg_integer(), State)
 	-> ok | {ok, pos_integer(), State} when State::http2_machine().
 ensure_window(StreamID, Size, State=#http2_machine{opts=Opts}) ->
-	Stream = #stream{remote_window=RemoteWindow} = stream_get(StreamID, State),
-	case ensure_window(Size, RemoteWindow, stream, Opts) of
-		ok ->
+	case stream_get(StreamID, State) of
+		%% For simplicity's sake, we do not consider attempts to ensure the window
+		%% of a terminated stream to be errors. We simply act as if the stream
+		%% window is large enough.
+		undefined ->
 			ok;
-		{ok, Increment} ->
-			{ok, Increment, stream_store(Stream#stream{remote_window=RemoteWindow + Increment}, State)}
+		Stream = #stream{remote_window=RemoteWindow} ->
+			case ensure_window(Size, RemoteWindow, stream, Opts) of
+				ok ->
+					ok;
+				{ok, Increment} ->
+					{ok, Increment, stream_store(Stream#stream{remote_window=RemoteWindow + Increment}, State)}
+			end
 	end.
 
 %% No need to update the window when we are not expecting data.