title: select fails with error code 9
body: <p>I am having this server code below, select function in this code throws some times bad file descriptor error along with the error code 9 when some client tries to connect to this, have gone through the code and could not find anything wrong with this, any help will be greatly appreciated, and this behavior is not constant though! </p>
  
  <pre><code>main()
  {
      int server_fd, client_fd;
      socklen_t server_len, client_len;
      struct sockaddr_in server_address, client_address;
      fd_set readfds, testfds;
      char errorstr[128];
      char req_buf[REQ_BUF_LEN];
      int result;
      struct itimerspec time_period = {{10,0},{10,0}}; /* periodic timer */
      struct timeval sel_timeout = {0, 100000};
      int lines = 0;
  
      FD_ZERO(&amp;readfds);
      FD_ZERO(&amp;testfds);
  
      /* Create and name a socket for the server */
      server_fd = socket(AF_INET, SOCK_STREAM, 0);
      if( server_fd &lt; 0 ) {
          printf("socket creation failed \n");
          exit(0);
      }
      int on = 1;
      /* Enable address reuse */
      int ret = setsockopt( server_fd, SOL_SOCKET, SO_REUSEADDR, &amp;on, sizeof(on) );
  
      server_address.sin_family = AF_INET;
      server_address.sin_addr.s_addr = htonl(INADDR_ANY);
      server_address.sin_port = htons(BL_MANAGER_PORT);
      server_len = sizeof(server_address);
  
      result = bind(server_fd, (struct sockaddr *)&amp;server_address, server_len);
      if( result &lt; 0 ) {
          printf("Bind faield \n");
          exit(0);
      }
  
      /* Create a connection queue and wait for clients */
      result = listen(server_fd, MAX_CLIENTS);
      if( result &lt; 0 ) {
          printf("Listen failed\n");
          exit(0);
      }
  
      FD_SET(server_fd, &amp;readfds);
  
      /* create timer to fire every one second */
   int timer_fd  =  timerfd_create(CLOCK_MONOTONIC , 0);
      if( timer_fd &lt; 0 ) {
          printf("Timer fd failed\n");
          exit(0);
      }
  
      result = timerfd_settime(timer_fd, 0, &amp;time_period, NULL);
      if( result &lt; 0 ) {
          printf("timer fd set time failed\n");
          exit(0);
      }
      FD_SET(timer_fd, &amp;readfds);
  
  
      while(1) {
          int fd;
          int nread;
          int result;
          char *ptr;
          int len_read = 0;
          uint64_t expired = 0;
  
          testfds = readfds;
  
          result = select(FD_SETSIZE, &amp;testfds, NULL, NULL, &amp;sel_timeout);
          if( result &lt; 0 ) {
             perror ("The following error occurred");
             printf("select failed");
             exit(0);
          }
  
          /* There is activity. Find the descriptor */
          for(fd = 0; fd &lt; FD_SETSIZE; fd++) {
              if(FD_ISSET(fd, &amp;testfds)) {
                  if (fd == timer_fd) {
                          read(timer_fd, &amp;expired, sizeof(uint64_t));
                          printf("rate = %d\n", (lines/(10 * (uint32_t)expired)));
                          lines = 0;
                  } else if( fd == server_fd ) {         /* is this server fd? */
                      client_fd = accept(server_fd,
                                         (struct sockaddr *)&amp;client_address,
                                         &amp;client_len);
                      FD_SET(client_fd, &amp;readfds);
                  } else {                                 /* is this read fd? */
                      ioctl(fd, FIONREAD, &amp;nread);
                      //assert(nread &lt; REQ_BUF_LEN);
                      if( nread == 0 ) {
                          FD_CLR(fd, &amp;readfds);
   close(fd);
                      } else {
                          nread = read(fd, req_buf, REQ_BUF_LEN-1);
                          //req_buf[nread] = '\0';
                          //printf("%s\n", req_buf);
                          ptr = req_buf;
                          while ((*ptr != EOF) &amp;&amp; ++len_read &lt; nread) {
                                  if (*ptr == '\n') {
                                          ++lines;
                                  }
                                  ptr++;
                          }
                          //printf("num of lines: %d\n", lines);
                      }
                  }
              } /* FD_ISSET(fd, &amp;testfds)? */
          }
      } /* while(1) */
  }
  </code></pre>