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
4
u/DSMan195276 Dec 20 '23
Perhaps Mac OS does not wake-up every
select()caller when data is ready to be read from the socket? You should be able to test for that by keeping track of when each reader gets woken up, and then check whether it's all the time or only sporadically (and which threads it happens to).It's gets pretty messy but I could see that behavior freezing your program due to the fact that every thread will only read
k_packetsmax bytes. Aselect()caller may be picked to wake-up when there's a full read-queue and then read one byte and exit because it hitsk_packets. Due to the mentionedselect()behavior, no other callers would be woken up until new bytes come in, and it seems pretty possible that reading only one byte might not be enough to unblock the writerselect()calls.The way to get around that kind of issue would be to always call
read()until you get an error and then callselect(). That however gets messy to implement if you still want eachreader()thread to only readk_packetsworth from the pipe, I suspect you would need to add additional coordination between the threads to make it possible. Ex. Use a second "wakeup" pipe and havereader()threadsselect()on both pipes. When areader()thread exits it writes to the "wakeup" pipe which forces anotherreader()thread'sselect()to exit. When threads wake-up they read from both pipes to clear them.2
u/laurentbercot Dec 20 '23
Note that if you only have one socketpair, what happens when you have several readers is undefined. All the readers should wake up when data becomes available on fd[0], but only one of them will be able to read the byte.
I'm not sure what your goal is, but your architecture looks dubious. Most likely, you either want 1 socketpair with 1 reader for multiple writers, or you want a dedicated communication channel for every (reader, writer) pair.
8
u/programmer9999 Dec 20 '23 edited Dec 20 '23
readandwrite(and any other syscalls) should always be thread safe, they must be protected by an in-kernel spinlock or mutex. Unless the underlying driver is buggy, and I doubt that the socketpair driver is. Maybeselecton macOS is edge-triggered, or has some other kind of quirk? Try usingpollinstead.1
u/dfx_dj Dec 20 '23
Could be that your code is running into some other hurdle? Like some threads reaching the end of the k_packets loop as the loop iterations aren't conditional on read or write successes?
-1
u/[deleted] Dec 20 '23
You did not mention if your macOS system is Intel or Apple Silicon based. If Apply Silicon based, there may be an issue with the way the Unix function calls were implemented.