What does ECONNABORTED mean when trying to connect a socket?

I am using python 2.7 on an ubuntu machine.

The client is trying to connect to the server. I get EINPROGRESS, which is expected for non-blocking sockets.

To check if the connection was successful, I do what the {connect} man page offers:

# 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 down, it gives me ECONNREFUSED. So far so good.

If the connection fails, I want to try several times.

Problem : the second time I try to connect the same socket, {connect} sends me ECONNABORTED. This is not on the man page {connect}. What does it mean?

+6
source share
2 answers

ECONNABORTED installed in two places in the source code of the Linux kernel kernel socket.

According to the errno man page and /include/asm-generic/errno.h

#define ECONNABORTED 103 /* Software caused connection abort */

the first is in the function that defines syscall accept4 in /net/socket.c .

Corresponding source code

 1533 if (upeer_sockaddr) { 1534 if (newsock->ops->getname(newsock, (struct sockaddr *)&address, 1535 &len, 2) < 0) { 1536 err = -ECONNABORTED; 1537 goto out_fd; 1538 } 1539 err = move_addr_to_user((struct sockaddr *)&address, 1540 len, upeer_sockaddr, upeer_addrlen); 1541 if (err < 0) 1542 goto out_fd; 1543 } 

The following is an explanation of the logic.

If a peer-to-peer network address from user space is specified and if the new socket does not have a name, set the error value to ECONNABORTED and go to the out_fd label.

the second is in the function that defines the inet_stream_connect symbol in /net/ipv4/af_inet.c .

Corresponding source code

 645 /* Connection was closed by RST, timeout, ICMP error 646 * or another process disconnected us. 647 */ 648 if (sk->sk_state == TCP_CLOSE) 649 goto sock_error; 662 sock_error: 663 err = sock_error(sk) ? : -ECONNABORTED; 664 sock->state = SS_UNCONNECTED; 665 if (sk->sk_prot->disconnect(sk, flags)) 666 sock->state = SS_DISCONNECTING; 667 goto out; 

The following is an explanation of the logic.

The only code that goto has for the sock_error label in inet_stream_connect is a check to see if the socket was closed by RST, timeout, another process, or an error.

In the label sock_error If we can restore the socket error report, do it, otherwise the error state will be ECONNABORTED

As a comment by Celada , I also recommend opening a new socket every time.

+12
source

See the manual page for errno. On FreeBSD, you can find it as intro (2). It says:

53 ECONNABORTED software caused a connection interruption. The unlink was triggered internally for your host machine.

As for this, you will have to look in the Linux kernel source for sockets. On FreeBSD, only accept seems to return ECONNABORTED.

0
source

Source: https://habr.com/ru/post/910397/


All Articles