r/C_Programming • u/having-four-eyes • 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 offcntl(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 ofEAGAIN/EWOULDBLOCK, they wait onselect(fd[0] + 1, &fds, ...)ensuring thatselectwill return a positive value; - writers 10000 times writing a single byte to the socket:
write(fd[1], &c, 1), also handlingEAGAIN/EWOULDBLOCK, as the socket buffer may be overloaded. Also ensuring thatselect(fd[1], nullptr, &fds, ...)returns positive value;
- readers 10000 times reading a single byte from the socket:
- 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
View all comments
Show parent comment
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)