mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-16 13:10:24 +00:00
Fix loop handler keepalive race condition
Previously if a loop handler received the timeout message from a previous request on the same connection the socket would be set to {active, once} incorrectly - when a socket packet was already in the message queue. This second packet would not be added to the buffer before a Handler:info/3 call if a user message was in the message queue before both socket packets.
This commit is contained in:
parent
5a25c7f7f2
commit
1c474af8ee
3 changed files with 82 additions and 2 deletions
41
test/http_SUITE_data/http_loop_stream_recv.erl
Normal file
41
test/http_SUITE_data/http_loop_stream_recv.erl
Normal file
|
@ -0,0 +1,41 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(http_loop_stream_recv).
|
||||
-export([init/3]).
|
||||
-export([info/3]).
|
||||
-export([terminate/3]).
|
||||
|
||||
init({_, http}, Req, _) ->
|
||||
receive after 100 -> ok end,
|
||||
self() ! stream,
|
||||
{loop, Req, 1, 100}.
|
||||
|
||||
info(stream, Req, Id) ->
|
||||
case stream_next(Req) of
|
||||
{ok, Id, Req2} ->
|
||||
info(stream, Req2, Id+1);
|
||||
{done, Req2} ->
|
||||
{ok, Req3} = cowboy_req:reply(200, Req2),
|
||||
{ok, Req3, Id}
|
||||
end.
|
||||
|
||||
terminate({normal, shutdown}, _, _) ->
|
||||
ok.
|
||||
|
||||
stream_next(Req) ->
|
||||
stream_next(<<>>, Req).
|
||||
|
||||
stream_next(Buffer, Req) ->
|
||||
case cowboy_req:stream_body(Req) of
|
||||
{ok, Packet, Req2} ->
|
||||
case <<Buffer/binary, Packet/binary>> of
|
||||
<<Id:32>> ->
|
||||
{ok, Id, Req2};
|
||||
Buffer2 when byte_size(Buffer2) < 4 ->
|
||||
stream_next(Buffer2, Req2);
|
||||
_InvalidBuffer ->
|
||||
{error, invalid_chunk}
|
||||
end;
|
||||
Other ->
|
||||
Other
|
||||
end.
|
Loading…
Add table
Add a link
Reference in a new issue