Skip to content

Conversation

@j8r
Copy link
Contributor

@j8r j8r commented Oct 19, 2025

Solves #15886.

I noticed removing that line removes the issue altogether: https://github.com/crystal-lang/crystal/blob/180c4bc/src/http/server.cr#L507C10-L508C1.
I don't know if that's an option too, or putting sync = true after the handler has been called like

@handler.call(context)
if input.is_a?(IO::Buffered)
  io.sync = true
end

The latter is what has been done in this PR.

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Oct 20, 2025

IO#sync= also flushes the buffer, so I don't see how the behavior could be different from flushing in IO#close 😕

Worse, if flush raises in IO#sync= then the socket won't be closed until collected by the GC, while IO#close makes sure that it is closed now.

@j8r
Copy link
Contributor Author

j8r commented Oct 20, 2025

What I know is setting IO#sync = true prevents the error from happening, maybe the flush is done differently compared to IO#close?
Why there is in the first place at https://github.com/crystal-lang/crystal/blob/180c4bc/src/http/server.cr#L507C10-L508C1

if io.is_a?(IO::Buffered)
    io.sync = false
end

Any better fix is welcomed, I'm not sure what's the best way to solve the issue.

@ysbaddaden
Copy link
Contributor

Oh, this is flushing the socket buffer (output) before flushing the response buffer (response.output) to the socket buffer, and then flushing the socket buffer again :/

If there's anything to flush, then I guess you'll still get an exception at some point.

@j8r
Copy link
Contributor Author

j8r commented Oct 21, 2025

By the way if this can help, when using sockets input and output point to the same IO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants