diff --git a/spec/std/http/server/request_processor_spec.cr b/spec/std/http/server/request_processor_spec.cr index 42a6ff7c7f70..f0f1fd3d8945 100644 --- a/spec/std/http/server/request_processor_spec.cr +++ b/spec/std/http/server/request_processor_spec.cr @@ -2,6 +2,7 @@ require "spec" require "log/spec" require "http/server/request_processor" require "../../../support/io" +require "../../../support/tempfile" private def requestize(string) string.gsub('\n', "\r\n") @@ -268,6 +269,22 @@ describe HTTP::Server::RequestProcessor do logs.entry.exception.should be_a(IO::Error) end + it "handles closed UNIXSocket without broken pipe" do + with_tempfile("request_processor.sock") do |path| + server = UNIXServer.new(path) + client = UNIXSocket.new(path) + server.accept do |sock| + sock.sync = false + client.send "GET / HTTP/1.1\r\n\r\n" + processor = HTTP::Server::RequestProcessor.new do |context| + client.close + context.response.puts "hello" + end + processor.process(sock, sock) + end + end + end + it "catches raised error on handler and retains context from handler" do exception = Exception.new "OH NO" processor = HTTP::Server::RequestProcessor.new { Log.context.set foo: "bar"; raise exception } diff --git a/src/http/server/request_processor.cr b/src/http/server/request_processor.cr index 231f09fb20c5..b985bf0ae559 100644 --- a/src/http/server/request_processor.cr +++ b/src/http/server/request_processor.cr @@ -48,7 +48,13 @@ class HTTP::Server::RequestProcessor context = Context.new(request, response) Log.with_context do - @handler.call(context) + begin + @handler.call(context) + ensure + if output.is_a?(IO::Buffered) + output.sync = true + end + end rescue ex : ClientError Log.debug(exception: ex.cause) { ex.message } rescue ex