Skip to content

Exceptions when sending to closed connections on an HTTP::Server bound to a UNIX socket #15886

Open
@j8r

Description

@j8r

While running an HTTP::Server bound to a UNIX socket, trying to send a response after the client closed the connection prints exceptions.
This lets me think it is a bug since this behavior doesn't happen when using HTTP::Server#bind_tcp, which doesn't print any exception after a connection has been closed.

To reproduce it:

  1. Run the below code snippet
  2. On a second terminal, make a HTTP call: curl --unix-socket /tmp/server.sock 'http://localhost/'
  3. When seeing "Receiving request", CTRL+C the curl command
  4. Exceptions will be printed on the STDOUT.

Here is the code to reproduce:

require "http/server"

http_server = HTTP::Server.new do |context|
  puts "Receiving request"
  sleep 5.seconds
  context.response.puts "hello"
end
# http_server.bind_tcp 8080
http_server.bind_unix "/tmp/server.sock"
Signal::INT.trap { http_server.close }
http_server.listen

The exceptions:

Unhandled exception in spawn: Error while flushing data to the client (HTTP::Server::ClientError)
  from /usr/lib/crystal/lib/http/server/response.cr:328:9 in 'unbuffered_flush'
  from /usr/lib/crystal/lib/io/buffered.cr:251:5 in 'flush'
  from /usr/lib/crystal/lib/io/buffered.cr:258:5 in 'close'
  from /usr/lib/crystal/lib/http/server/response.cr:298:9 in 'close'
  from /usr/lib/crystal/lib/http/server/request_processor.cr:63:11 in 'process'
  from /usr/lib/crystal/lib/http/server.cr:521:5 in 'handle_client'
  from /usr/lib/crystal/lib/http/server.cr:451:5 in '->'
  from /usr/lib/crystal/lib/fiber.cr:143:11 in 'run'
  from /usr/lib/crystal/lib/fiber.cr:95:34 in '->'
  from ???
Caused by: Error writing to socket (#<UNIXSocket:0x7f4feac85e00>): Broken pipe (IO::Error)
  from /usr/lib/crystal/lib/crystal/system/unix/event_loop_libevent.cr:227:11 in 'write'
  from /usr/lib/crystal/lib/crystal/system/socket.cr:89:5 in 'system_write'
  from /usr/lib/crystal/lib/socket.cr:451:16 in 'unbuffered_write'
  from /usr/lib/crystal/lib/io/buffered.cr:250:5 in 'flush'
  from /usr/lib/crystal/lib/http/server/response.cr:325:9 in 'unbuffered_flush'
  from /usr/lib/crystal/lib/io/buffered.cr:251:5 in 'flush'
  from /usr/lib/crystal/lib/io/buffered.cr:258:5 in 'close'
  from /usr/lib/crystal/lib/http/server/response.cr:298:9 in 'close'
  from /usr/lib/crystal/lib/http/server/request_processor.cr:63:11 in 'process'
  from /usr/lib/crystal/lib/http/server.cr:521:5 in 'handle_client'
  from /usr/lib/crystal/lib/http/server.cr:451:5 in '->'
  from /usr/lib/crystal/lib/fiber.cr:143:11 in 'run'
  from /usr/lib/crystal/lib/fiber.cr:95:34 in '->'
  from ???

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.topic:stdlib:networking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions