I’m using python 2.7 on an ubuntu machine.
The client tries to connect to the server. I get a EINPROGRESS which is expected for non-blocking sockets.
To check whether or not the connection succeeded, I do what the man page for {connect} suggest:
# EINPROGRESS The socket is nonblocking and the connection cannot be
# completed immediately. It is possible to select(2) or poll(2) for
# completion by selecting the socket for writing. After select(2)
# indicates writability, use getsockopt(2) to read the SO_ERROR option at
# level SOL_SOCKET to determine whether connect() completed successfully
# (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error
# codes listed here, explaining the reason for the failure)
When the server is offline, this gives me a ECONNREFUSED. So far so good.
When the connection fails, I want to try again a few times.
Problem: the second time I try to connect that same socket, {connect} sends me ECONNABORTED. This one isn’t in the man page of {connect}. What does it mean?
ECONNABORTEDis set in two places of the Linux Kernel Source Socket Code.As per the
errnoman page and /include/asm-generic/errno.h#define ECONNABORTED 103 /* Software caused connection abort */The first is in the function that defines the syscall
accept4in /net/socket.c.Relevant Source Code
The relevant explanation of logic is below.
If the address of the peer socket from userspace is defined
and If the new socket doesn’t have name, then set error state to
ECONNABORTEDand goto the labelout_fd.The second is in the function that defines the symbol
inet_stream_connectin /net/ipv4/af_inet.c.Relevant Source Code
The relevant explanation of logic is below.
The only code that has a goto to the
sock_errorlabel ininet_stream_connectis the check to see if the socket was closed by RST,timeout, another process or error.In the
sock_errorlabelIf we can recover a socket error report, do so , otherwise the error state to
ECONNABORTEDLike Celada‘s comment I also recommend opening a new socket each time.