clinuxnfs

NFS read timeout using c poll()


We have a NFS server which may sometimes have latency issues which then causes problem in my client, so Im trying to find a way to timeout, when it takes more time ( eg. > 200 ms) to read from NFS. Here is a minimal example which uses poll function to try to handle this issue:

struct pollfd pfd[1];
pfd[0].fd = fileno(file); //file opened previously using fopen 
pfd[0].events = POLLIN;
ret = poll(pfd, 1, 200); //wait for 200 milliseconds
if(ret == 0)
{
   fprintf(stderr, "poll timed out\n");
}

To test this I introduced latency of 400 ms using linux tc command. My understanding with poll is that, with POLLIN event, and with the added 400 ms latency, there should be a timeout. But somehow it doesnot happen and ret is always > 0

Is using poll the correct way to handle NFS latency issues?


Solution

  • No, poll() is not the correct choice. In POSIX a regular file is always considered ready, so poll() will return immediately but the actual I/O will still block.

    You might do the I/O in a separate thread, or on Linux you could look into using io_uring.