r/C_Programming • • Dec 20 '23

Are read/write functions on Unix Domain socket guaranteed to be reentrant when multiple threads share the same file descriptor?

Hi,

I'm having a strange deadlock in my code on macOS (works fine on Win & Linux), that I nailed down to a pretty simple case:

  • create non-blocking socketpair: socketpair(AF_UNIX, SOCK_STREAM, ...); + a couple of fcntl(fd, ..., flags | O_NONBLOCK)
  • spawn 128 pairs of threads (might be as little as 32, but will need several iterations to reproduce)

    • readers 10000 times reading a single byte from the socket: read(fd[0], &c, 1) . In the case of EAGAIN/EWOULDBLOCK, they wait on select(fd[0] + 1, &fds, ...) ensuring that select will return a positive value;
    • writers 10000 times writing a single byte to the socket: write(fd[1], &c, 1), also handling EAGAIN/EWOULDBLOCK, as the socket buffer may be overloaded. Also ensuring that select(fd[1], nullptr, &fds, ...) returns positive value;
  • main thread joins writers, then readers.
  • of course, I feed freshly filled fd_set's to the select.

Could anyone review my approach, please?

It works fine on Win/Linux, but on macOS, it ends up in a strange situation when both readers and writers are waiting on their corresponding select and I'm not getting the problem: if a reader is waiting on the select(read_fds), then the socket is writeable and writer's select(write_fds) should return.

I have really no idea how that could happen except that read/write are not thread-safe. However, it looks like POSIX docs and manpages state that they are (at least, reentrant).

There is a bit more detailed thread functions (I apologize for a line of C++ code)

    void reader(...)  // actually, C++ threads, doesn't matter
    {
        int fd_read = fd[0];
        char data;
        for (int i = 0; i < k_packets; ++i)
        {
            while (::read(fd_read, &data, 1) < 1)
            {
                fd_set readfds;
                FD_ZERO(&readfds);
                FD_SET(fd_read, &readfds);
                
                assert(errno == EAGAIN || errno == EWOULDBLOCK);
                int retval = ::select(fd_read + 1, &readfds, nullptr, nullptr, nullptr);
                if (retval < 1)
                    assert(errno == EAGAIN || errno == EWOULDBLOCK);
            }

            ++bytes_read;
        }
    }

    void writer(...)
    {
        int fd_write = fd[1];
        char data = 'x';
        for (int i = 0; i < k_packets; ++i)
        {
            while (::write(fd_write, &data, 1) < 1)
            {
                fd_set writefds;
                FD_ZERO(&writefds);
                FD_SET(fd_write, &writefds);
                
                assert(errno == EAGAIN || errno == EWOULDBLOCK);
                int retval = ::select(fd_write + 1, nullptr, &writefds, nullptr, nullptr);
                if (retval < 1)
                    assert(errno == EAGAIN || errno == EWOULDBLOCK);
            }

            ++bytes_written;
        }
    }

UPD: with a reader code with timeout and debug checks for the amount of pending read bytes with ioctl, it looks like there is a race condition. There are no bytes available before the select timeout, and there's a byte available after the timeout regardless of timeout length:

int bytes_available = 0;
assert(-1 != ::ioctl(fd_read, FIONREAD, &bytes_available));
int select_rc = select(fd_read + 1, &readfds, NULL, &errorfds, &timeout);
assert(-1 != select_rc);
if (0 == select_rc)
{
    assert(0 == bytes_available);                           // <!--- no byte was available
    print_stage("timeout (don't care); ");
}

assert(-1 != ::ioctl(fd_read, FIONREAD, &bytes_available));
assert(1 == bytes_available);                               // <!--- byte is available
assert(0 == FD_ISSET(fd_read, &errorfds));

rc = ::read(fd_read, &byte, 1);  // <!--- actually, reads the byte after the timeout
10 Upvotes

27 comments sorted by

View all comments

Show parent comment

1

u/paulstelian97 Dec 20 '23

Perhaps this is exactly what OP wants — a wasteful wake-all-readers and randomly having a non-busy thread succeed in reading.

2

u/having-four-eyes Dec 21 '23

Yep, although not that wasteful: threads aren't woken themselves, that's the scheduler's job. And the scheduler won't wake 256 at once: in my case, it woke a couple of triple. All but the luckiest one will go for another waiting round in nanoseconds, no biggie.

2

u/paulstelian97 Dec 21 '23

The scheduler, at least in Linux, doesn’t have a say in which threads are woken when a read is finally available. The IO subsystem does, and it could well dequeue all of the threads on the queue, they are sent to the scheduler (which only really manages the ready queue[s]). Then the threads will try to read, then be re-enqueued back to the waiting queue for the given file.

It could theoretically even be different for different files (or, more practically, different filesystems or sources of file descriptors)

1

u/having-four-eyes Dec 21 '23

Having a debug print after each wakeup and a single wakeup after the decent timeout I see that it does not wake all the 256 waiters, so there is some optimization here. However, I agree that this is implementation-dependent.

In any case, insane threads amount was chosen only to help me to reproduce the bug. I suspect that it should be reproducible even with 1 reader and 1 writer. Maybe, will run the test for 2 threads overnight :)

1

u/paulstelian97 Dec 21 '23

Is the debug print in the kernel? If you do it at a high enough level you can see that things can get requeued really quickly, potentially even in the implementation of read() itself.

2

u/having-four-eyes Dec 21 '23

Nope, just in user-mode after the select:

while (rc < 1)
{
    select(...);
    print("wakeup: {}", thread_id);
    rc = read(...);
    assert(errno == EAGAIN || errno == EWOULDBLOCK);
}

1

u/paulstelian97 Dec 21 '23

The select itself can be smart — some spurious wakeups can be compensated for by the kernel itself if the wake-up actually ends up happening after the read that empties the available buffer.

So worth verifying in-kernel, perhaps some counters can be useful. I think one may be able to make such counters with eBPF?

2

u/having-four-eyes Dec 21 '23

I think I got your point: once socket is ready, IO will enqueue waiting threads for wakeup, and then the scheduler will wake up as much as it finds reasonable (don't expect it to be more than the cores amount)

At least, that how I'd naively implemented it

1

u/paulstelian97 Dec 21 '23

The problem is that you could receive 100 bytes and each thread can in theory only read one byte. That way 100 threads would have to be woken. If there’s 200 threads then yeah, the optimization can work.

Or the IO will indeed just wake up as many as cores, BUT every read operation will wake additional threads if stuff gets left over as well. That would be a possible implementation that doesn’t wastefully wake too many threads. Although for atomicity issues having a small amount of extra threads woken up is still better than not enough (potential for deadlocks if too few are woken)

2

u/having-four-eyes Dec 21 '23

You mean, in kernel, read() may request other threads wake-up using a fd?

2

u/paulstelian97 Dec 21 '23

It may do that, dependent on the specific implementation (which may differ between pipes and files too)