socket close() -w- pthreads linux 2.6.18.2-34 (suse) SMP

Interesting issue. There was some discussion on the LKML last year regarding the potential problems in concurrent applications reusing file descriptors in various scenarios. The main issue is that the reuse of a file descriptor and reception of data in a threaded application can be confused pretty easily.

Alan Cox suggested using shutdown() before close() to deal with one of the most glaring reuse problems. This doesn't seem to work 100% either.

If I have code like this:

extern void *handleclient(void *arg) {
int rd = -1;
long mcount = 0;
VHNDL in;
char buf[BSZ];
                         pthread_detach(pthread_self());       
                         pthread_mutex_lock(&socketstable);  
                         memcpy(&in,arg,sizeof(in));
                         pthread_mutex_unlock(&socketstable);
                         while ( (rd = read(in.c_sock,buf,BSZ)) > 0) {
                                 printf("TID %d: Read message %d from peer at %s = %s\n",pthread_self(),mcount++,inet_ntoa(in.peer.sin_addr),buf);
                                 bzero(buf,BSZ);
                         }
                         /*pthread_mutex_lock(&socketstable);*/
                         shutdown(in.c_sock,SHUT_RDWR);
                         close(in.c_sock);
                        /*pthread_mutex_unlock(&socketstable);*/
                         pthread_exit(NULL);
}

The reuse of file descriptors still causes EBADF in some cases. The parent process is simple in an accept() loop creating handler threads till MAXTHREADS then recycling. As you see the handler does nothing but read till error and then exits.
I'm still getting EBADF on some closes and as a result having incrementing numbers of close-wait connects.

Any ideas?

Never mind. Logic error. Was inadvertently reusing a thread.