From cd70e766400496e08f52e7d361346ad224c13bad Mon Sep 17 00:00:00 2001 From: Jason Li Date: Fri, 13 Feb 2015 15:31:12 +1100 Subject: [PATCH] Fixed up crash when setting options to closed sockets. Setting options to closed sockets will crash the program. The changes in this commit allows the program to exit gracefully instead. --- src/mochiweb_http.erl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl index 897d6257..0c64ac15 100644 --- a/src/mochiweb_http.erl +++ b/src/mochiweb_http.erl @@ -75,7 +75,13 @@ loop(Socket, Opts, Body) -> request(Socket, Opts, Body). request(Socket, Opts, Body) -> - ok = mochiweb_socket:setopts(Socket, [{active, once}]), + case mochiweb_socket:setopts(Socket, [{active, once}]) of + {error,_} -> + mochiweb_socket:close(Socket), + exit(normal); + ok -> + ok + end, receive {Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl -> ok = mochiweb_socket:setopts(Socket, [{packet, httph}]),