Skip to contentSkip to conversation

select for UNIX sockets?

DISCUSSION Krzysztof Halasa 3 participants 6 messages Jun 3, 2003 – Jun 5, 2003

lore
Share & tools
lore
#0 lore raw

Hi,

Should something like this work correctly?

while(1) {
        FD_ZERO(&set);
        FD_SET(fd, &set);
        select(FD_SETSIZE, NULL, &set, NULL, NULL); <<<<<<< for writing

        if (FD_ISSET(fd, &set))
                sendto(fd, &datagram, 1, 0, ...);
}

fd is a normal local datagram socket. It looks select() returns with "fd ready for write" and sendto() then blocks as the queue is full.

I don't know if it's expected behaviour or just a not yet known bug. Of course, I have a more complete test program if needed.

2.4.21rc6, haven't tried any other version.

strace shows:

select(1024, NULL, [3], NULL, NULL) = 1 (out [3])
sendto(3, "\0", 1, 0, {sa_family=AF_UNIX, path="/tmp/tempUn"}, 13 <<< blocks

Signature · 2 lines
Krzysztof Halasa
Network Administrator
#1 lore raw
Quoted 3 lines · Krzysztof Halasa

Hi,

Should something like this work correctly?

Sort of. The wakeup may occur for several reasons and you need to check the return (for signals). Also the wakeup can occur when there is room but another thread fills it, or return room but not enough for a large datagram. Those don't seem to be the case on your example

#4 lore raw
In reply to Krzysztof Halasa
Quoted 19 lines · Krzysztof Halasa

Hi,

Should something like this work correctly?

while(1) {
        FD_ZERO(&set);
        FD_SET(fd, &set);
        select(FD_SETSIZE, NULL, &set, NULL, NULL); <<<<<<< for writing

        if (FD_ISSET(fd, &set))
                sendto(fd, &datagram, 1, 0, ...);
}

fd is a normal local datagram socket. It looks select() returns with "fd ready for write" and sendto() then blocks as the queue is full.

I don't know if it's expected behaviour or just a not yet known bug. Of course, I have a more complete test program if needed.

Quoted 7 lines

2.4.21rc6, haven't tried any other version.

strace shows:

select(1024, NULL, [3], NULL, NULL) = 1 (out [3])
sendto(3, "\0", 1, 0, {sa_family=AF_UNIX, path="/tmp/tempUn"}, 13 <<< blocks

Could. There may be room for the buffer, but unless it is set to nonblock, you may have a stream open to another host that may not accept the data (busy, network congestion...) With the required acks, the return may (should?) be delayed until the ack arrives.

#5 lore raw
Quoted 9 lines · Jesse Pollard
Quoted 3 lines

select(1024, NULL, [3], NULL, NULL) = 1 (out [3])
sendto(3, "\0", 1, 0, {sa_family=AF_UNIX, path="/tmp/tempUn"}, 13 <<< blocks

Could. There may be room for the buffer, but unless it is set to nonblock, you may have a stream open to another host that may not accept the data (busy,
network congestion...) With the required acks, the return may (should?) be delayed until the ack arrives.

But it's local datagram socket (UNIX). OTOH TCP to remote host should not block waiting for acks.

Signature · 2 lines
Krzysztof Halasa
Network Administrator
#2 lore raw
In reply to Alan Cox
Quoted 4 lines · Alan Cox

Sort of. The wakeup may occur for several reasons and you need to check the return (for signals). Also the wakeup can occur when there is room but another thread fills it, or return room but not enough for a large datagram. Those don't seem to be the case on your example

I've traced the problem to datagram_poll(). This function doesn't check if the receiver queue has at least one slot empty, it only checks sock_writeable() i.e. if:

        atomic_read(&sk->wmem_alloc) < (sk->sndbuf / 2);

unix_dgram_sendmsg() can then block on:

        if (unix_peer(other) != sk &&
            skb_queue_len(&other->receive_queue) > other->max_ack_backlog) {
                if (!timeo) {
                        err = -EAGAIN;
                        goto out_unlock;

Now the question is how should the problem be fixed? I've tried using modified datagram_poll():

+ unix_socket *other = NULL;

-       if (sock_writeable(sk))
+       other = unix_peer_get(sk);
+       if (sock_writeable(sk) && other &&
+           skb_queue_len(&other->receive_queue) <= other->max_ack_backlog)
                mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
        else
                set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);

but unix_peer_get(sk) returns NULL.
How can I examine the receiver queue length? Should I worry about "restarts" as in unix_dgram_sendmsg()? Maybe someone has a fix ready?

I've attached the userspace tests, a modified version of someone's real program - recv should be run first, send should be straced.

The problem is present in both 2.5.70 and 2.4.20rc7.

Signature · 2 lines
Krzysztof Halasa
Network Administrator
#3 lore raw
Quoted 1 line · Krzysztof Halasa

but unix_peer_get(sk) returns NULL.

Well... I missed the fact that this program uses "unconnected" UNIX UDP. Still, it shouldn't block on sendmsg after a successful select(), maybe dropping the packet should be better?. It's not that simple.

Thanks for all replies, investigating this issue further.

Signature · 2 lines
Krzysztof Halasa
Network Administrator