lpeterse / haskell-socket

A Haskell binding to the POSIX sockets API
MIT License
47 stars 10 forks source link

"connecting already connected socket" test fails #17

Closed lpeterse closed 8 years ago

lpeterse commented 8 years ago

Here is a relevant strace:

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 4
fcntl(4, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(4, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
bind(3, {sa_family=AF_INET, sin_port=htons(39000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
listen(3, 5)                            = 0
connect(4, {sa_family=AF_INET, sin_port=htons(39000), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
select(5, [], [4], NULL, NULL)          = 1 (out [4])
getsockopt(4, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
connect(4, {sa_family=AF_INET, sin_port=htons(39000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
close(3)                                = 0
close(4)                                = 0

The second connect does not fail. This is obviously not a library error.

lpeterse commented 8 years ago

man connect states:

Generally, connection-based protocol sockets may successfully connect() only once;

Linux' behaviour might be covered by documentation (depending on interpretation).

Here someone noted this behaviour as well and here someone's claiming a second connect is compulsary.

lpeterse commented 8 years ago

Solution: Perform a second connect when writeability has been signaled.

On Linux, it will succeed. On Windows (and possibily others) it will yield EISCONN which we let pass.