Hi,
On Debian GNU/Hurd amd64 (hurd 1:0.9.git20261028, GNU Mach
1.8+git20261028 UP), when a connection arrives over the network and the
client resets it before the server calls accept(), accept()/accept4() on
a non-blocking listening socket fails with ENOTCONN.
POSIX specifies ECONNABORTED for this and servers typically handle this
as more begnin than ENOTCONN.
lwip does the same (just with a fifth the performance :) and occasional malloc
"corrupted top size".
This affected a Tor relay running on Hurd. Tor closed its listening port on
every such reset, I'm submitting a workaround patch for tor.
To reproducing with attached small test program:
- In the Hurd guest: a non-blocking TCP listener on 0.0.0.0 that poll()s
and calls accept4(..., SOCK_CLOEXEC|SOCK_NONBLOCK).
- From another host (QEMU user networking, through the guest's e1000):
connect, set SO_LINGER {1, 0}, close. Repeat 20 times.
- Result: 20 x "accept4: errno 1073741881 (Transport endpoint is not
connected)". The next normal connection is accepted, so the queue
does drain.
- Plain accept() behaves the same, with or without an address buffer.
- Resets over loopback inside the guest didn't trigger it.
It seems to be caused by this chain:
- S_socket_accept() passes sock->flags to inet_accept().
- For a non-blocking listener, inet_accept()
(linux-src/net/ipv4/af_inet.c) takes
"if (flags & O_NONBLOCK) goto do_half_success;". That skips the
TCP_CLOSE -> -ECONNABORTED check, which the blocking path does perform.
- S_socket_accept() then calls make_sockaddr_port(newsock, 1, ...). That
calls inet_getname() with peer=1, which returns -ENOTCONN for the
closed socket.
I have two possible fixes:
1. In inet_accept(), apply the TCP_CLOSE/sk2->err checks on the
non-blocking path too, so it returns -ECONNABORTED like the blocking
path.
2. Or, in S_socket_accept(), map an ENOTCONN from make_sockaddr_port()
to ECONNABORTED.
1. seems most reasonable and I'm happy to test it.
Thanks,
Ali
This email was drafted by Claude but checked by myself.
/* Listen on 0.0.0.0:PORT like Tor's ORPort; poll + accept4(addr, CLOEXEC|NONBLOCK)
for SECS seconds, logging every result. */
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int one = 1, s = socket(AF_INET, SOCK_STREAM, 0), port = atoi(argv[1]), secs = atoi(argv[2]);
struct sockaddr_in sa = { .sin_family = AF_INET, .sin_port = htons(port) };
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
if (bind(s, (struct sockaddr *)&sa, sizeof sa) < 0 || listen(s, 16) < 0) { perror("listen"); return 1; }
fcntl(s, F_SETFL, O_NONBLOCK);
time_t end = time(NULL) + secs; int ok = 0, err = 0, spins = 0;
while (time(NULL) < end) {
struct pollfd p = { s, POLLIN, 0 };
if (poll(&p, 1, 500) <= 0) { spins = 0; continue; }
struct sockaddr_storage ss; socklen_t sl = sizeof ss;
int a = accept4(s, (struct sockaddr *)&ss, &sl, SOCK_CLOEXEC | SOCK_NONBLOCK);
if (a >= 0) { ok++; close(a); spins = 0; continue; }
if (errno == EAGAIN) { if (++spins > 100) { printf("SPIN: readable but EAGAIN x100\n"); return 2; } continue; }
err++; printf("accept4: errno %d (%s)\n", errno, strerror(errno));
if (++spins > 100) { printf("SPIN: same error x100, queue not draining\n"); return 2; }
}
printf("accepted %d, errors %d\n", ok, err);
return 0;
}