Skip to content
Tcl Source Code View Ticket
Login

View Ticket

fconfigure -sockname fails with "invalid argument" after a failed async connect

Open7586dbc9f53aad9e461dc547c1e83de06bd1382bCreated 35 days agoUpdated 35 days ago
Descriptionwiki2026-10-28 11:40:57
After an async connect has failed, fconfigure -sockname calls getsockname() on the failed socket. On DragonFly this fails with EINVAL, so it raises can't get sockname: invalid argument. On other systems it returns whatever the system reports for the dead socket. -peername raises can't get peername in the same case.

This makes socket-14.17 fail intermittently on DragonFly 6.4: if both ::1 and 127.0.0.1 are refused before fconfigure runs, the connect is no longer pending. A loop of socket -async localhost $closedport followed by fconfigure -sockname failed 144 times in 2000 iterations on 8.6 and 9.0 and 120 times on 9.1 in one session, and 1 to 4 times in 2000 in another.

Also, in socket test mode (testsocket testflags $s 1), fconfigure -sockname or -peername on a pending async socket dereferences a NULL errorCodePtr in WaitForConnect() and crashes.

serhiy.storchakawiki2026-10-28 12:45:51

getsockname() on a socket whose non-blocking connect to a closed loopback port was refused:

Systemgetsockname()getpeername()
Linux 7.0local address and portENOTCONN
FreeBSD 15.1local address and portENOTCONN
Windows 11wildcard address and a portWSAENOTCONN
OpenIndianawildcard address, port 0ENOTCONN
NetBSD 10.1EINVALENOTCONN
OpenBSD 7.9EINVALENOTCONN
DragonFly 6.4ECONNRESET (IPv4), EINVAL (IPv6)ENOTCONN

So after a failed async connect, -sockname returns a meaningless address on some systems and raises an error on others, and on the latter plain fconfigure $s fails too. -peername raises an error everywhere.

Possible fixes: raise an error on all systems, as -peername already does and -sockname does on the BSDs, but skip it for plain fconfigure $s; or return "", as while the connect is pending, which no system does now. With the error, socket-14.16 and 14.17 would still need to accept either result. win/tclWinSock.c has the same structure.

Log in (anonymous is fine) to add a comment.