Skip to content

Commit

Permalink
Merge pull request #107 from djnym/gen_tcp_fix
Browse files Browse the repository at this point in the history
Make the acceptor crash optional.
  • Loading branch information
etrepum committed Apr 13, 2013
2 parents 15bc558 + 4afdf42 commit 56a32a0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
% -*- mode: erlang -*-
{erl_opts, [debug_info]}.
{erl_opts, [debug_info,
{platform_define, "(R14|R16)", 'gen_tcp_fix'}
]}.
{cover_enabled, true}.
{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{dialyzer_opts, [{warnings, [no_return,
Expand Down
28 changes: 28 additions & 0 deletions scripts/check_for_gen_tcp_fix.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env escript
%% -*- mode: erlang -*-
%%! -pa ../ebin
-export([main/1]).

main([]) ->
application:start (inets),
mochiweb_http:start([{port, 5678},
{loop,
fun(Req) ->
Req:respond({ 200,
[ {"Content-Type", "text/html"} ],
[ "<html><body>Hello</body></html>" ]
})
end}]),

io:format ("~p~n",[not has_bug(1000) and not has_bug(10000)]).

has_bug (Len) ->
case
httpc:request (get, {"http://127.0.0.1:5678/",
[{"X-Random", [$a || _ <- lists:seq(1,Len)]}]}, [], [])
of
{error,socket_closed_remotely} -> true;
{ok,{{"HTTP/1.1",200,"OK"}, _, "<html><body>Hello</body></html>"}} -> false;
{ok,{{"HTTP/1.1",400,"Bad Request"}, _, []}} -> false;
R -> io:format ("don't know what to make of ~p~n",[R]), undefined
end.
20 changes: 12 additions & 8 deletions src/mochiweb_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ loop(Socket, Body) ->
ok = mochiweb_socket:setopts(Socket, [{packet, http}]),
request(Socket, Body).

-ifndef(gen_tcp_fix).
-define(R15B_GEN_TCP_FIX, {tcp_error,_,emsgsize} ->
% R15B02 returns this then closes the socket, so close and exit
mochiweb_socket:close(Socket),
exit(normal);
).
-else.
-define(R15B_GEN_TCP_FIX,).
-endif.

request(Socket, Body) ->
ok = mochiweb_socket:setopts(Socket, [{active, once}]),
receive
Expand All @@ -66,10 +76,7 @@ request(Socket, Body) ->
{ssl_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
{tcp_error,_,emsgsize} ->
% R15B02 returns this then closes the socket, so close and exit
mochiweb_socket:close(Socket),
exit(normal);
?R15B_GEN_TCP_FIX
_Other ->
handle_invalid_request(Socket)
after ?REQUEST_RECV_TIMEOUT ->
Expand Down Expand Up @@ -99,10 +106,7 @@ headers(Socket, Request, Headers, Body, HeaderCount) ->
{tcp_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
{tcp_error,_,emsgsize} ->
% R15B02 returns this then closes the socket, so close and exit
mochiweb_socket:close(Socket),
exit(normal);
?R15B_GEN_TCP_FIX
_Other ->
handle_invalid_request(Socket, Request, Headers)
after ?HEADERS_RECV_TIMEOUT ->
Expand Down
1 change: 1 addition & 0 deletions src/mochiweb_socket_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ init(State=#mochiweb_socket_server{ip=Ip, port=Port, backlog=Backlog, nodelay=No
{packet, 0},
{backlog, Backlog},
{recbuf, ?RECBUF_SIZE},
{exit_on_close, false},
{active, false},
{nodelay, NoDelay}],
Opts = case Ip of
Expand Down

0 comments on commit 56a32a0

Please sign in to comment.