module

SocketConnectFix

Requiring this file changes Socket#connect for the whole program.

After the event loop reports a connection as established, the kernel's pending socket error (SO_ERROR) is checked. A non-zero value is yielded as a Socket::ConnectError, exactly as an immediate failure would be.

Effects:

  • TCPSocket.new(host, port) tries the next resolved address when an earlier one refused, e.g. localhost falls back from ::1 to 127.0.0.1.
  • A refused connection raises Socket::ConnectError at construction instead of returning a socket that fails on first use.

Applies to every TCPSocket, including those created by HTTP::Client and database drivers. Has no effect on Windows.

Compile with -Dsocket_connect_fix_disabled to leave Socket#connect untouched, e.g. to check whether the underlying Crystal bug is still present.

Constants

SO_ERROR = 4
VERSION = {{ (`shards version /tmp/tmp.DLLAeL/src/src`).chomp.stringify }}

Class methods

pending_error(socket : Socket) : Int32

Returns the pending error on socket and clears it; 0 means none.

Source