From b0a17ac9b4cf88cc983a88b757e6cf41ff0d5ea6 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Wed, 16 Jan 2019 23:33:24 +0300 Subject: [PATCH] Do not try to call ExceptionHandler#call_exception_with_status_code on a closed response. Fixes #514 --- spec/exception_handler_spec.cr | 10 ++++++++++ src/kemal/exception_handler.cr | 1 + 2 files changed, 11 insertions(+) diff --git a/spec/exception_handler_spec.cr b/spec/exception_handler_spec.cr index 78da426..b9519e9 100644 --- a/spec/exception_handler_spec.cr +++ b/spec/exception_handler_spec.cr @@ -102,4 +102,14 @@ describe "Kemal::ExceptionHandler" do response.headers["Content-Type"].should eq "application/json" response.body.should eq "Rendered error with 500" end + + it "does not do anything on a closed io" do + get "/" do |env| + halt env, status_code: 404 + end + + request = HTTP::Request.new("GET", "/") + client_response = call_request_on_app(request) + client_response.status_code.should eq 404 + end end diff --git a/src/kemal/exception_handler.cr b/src/kemal/exception_handler.cr index 46241c8..1462832 100644 --- a/src/kemal/exception_handler.cr +++ b/src/kemal/exception_handler.cr @@ -18,6 +18,7 @@ module Kemal end private def call_exception_with_status_code(context : HTTP::Server::Context, exception : Exception, status_code : Int32) + return if context.response.closed? if !Kemal.config.error_handlers.empty? && Kemal.config.error_handlers.has_key?(status_code) context.response.content_type = "text/html" unless context.response.headers.has_key?("Content-Type") context.response.status_code = status_code