crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.26k stars 1.61k forks source link

Unhandled exception on HTTP::Handler #14566

Closed yanecc closed 2 months ago

yanecc commented 2 months ago

I'm not sure whether it's a problem within crystal.

require "grip"
class Application < Grip::Application
  def initialize(@port : Int32)
    super(environment: "development", serve_static: true)
  end

  getter port : Int32
  getter pubilc_dir : String = "./public"
  getter static_routes : Hash(String, String) = {
    "/foo/bar" => "./public",
    "/test"    => "./public",
  }
  getter fallthrough : Bool = true
  getter directory_listing : Bool = true
end

app = Application.new(80)
app.run

I made some changes to grip that I think was harmless. When I try to access static resources by routes, it works as expected but with an unhandled exception.

ERROR - http.server: Unhandled exception on HTTP::Handler
Closed stream (IO::Error)
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:204 in 'check_headers'
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:40 in 'status='
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:89 in 'status_code='
  from C:\Users\Sunrise\Documents\CodeRepo\Crystal\test\static-server\lib\grip\src\grip\handlers\exception.cr:16 in 'call'
  from A:\Scoop\apps\crystal\current\src\http\server\request_processor.cr:51 in 'process'
  from A:\Scoop\apps\crystal\current\src\http\server.cr:521 in 'handle_client'
  from A:\Scoop\apps\crystal\current\src\http\server.cr:451 in '->'
  from A:\Scoop\apps\crystal\current\src\fiber.cr:141 in 'run'
  from A:\Scoop\apps\crystal\current\src\fiber.cr:93 in '->'

My fork: https://github.com/yanecc/grip

Blacksmoke16 commented 2 months ago

If you monkey patch in some pp ex within the exception handler, is the actual exception this Closed stream error? Is possible its trying to handle a different error originally that might make the problem more clear.

jwoertink commented 2 months ago

Possibly related? https://forum.crystal-lang.org/t/help-solving-io-error/130 I get this error ALL the time on every Lucky project I work on. I've never found what causes it, and I'm not sure if it actually affects anything or not.

straight-shoota commented 2 months ago

The exception seems to be caused by the grip framework. It appears to be seting the status code in an error handler at a point where the response connection is already closed (ref. https://github.com/grip-framework/grip/blob/7dbfcde591659e29c21f91e2ed8620ed1748510a/src/grip/handlers/exception.cr#L16C9-L16C112).

I don't think there's anything actionable here. Please report this issue to grip.

If you need more help with debugging (the original error is overshadowed by this bug in the error handler) please consult https://forum.crystal-lang.org/