[lkml]   [2004]   [May]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Multithread select() bug
Andre Ben Hamou wrote:

> Eric Dumazet wrote:
>> Your program is racy and have undefined behavior.
>> A thread should not close a handle 'used by another thread blocked in
>> a sytemcall'
>> The race is : if a thread does a close(fd), then the fd value may be
>> reused by another thread during an open()/socket()/dup()... syscall,
>> and the first thread could issue the select() syscall (or
>> read()/write()/...) on the bad file.
> Apologies, but I don't follow this.
> It was my understanding that the (potentially) many threads of a
> single process all share a canonical file descriptor table. Hence as
> long as the various calls you mention are issued in a guaranteed
> order, maintaining state as you go (which is what the 1 second sleep
> in the test code was a very quick and dirty way to almost do), I don't
> see how a race condition arises.
> If I were to replace the sleep (1) with, say a global semaphore or
> something similar, would your explanation still hold?
So please how do you guarantee that thread 1 runs *before* thread 2)

Thread 1)
select( fd)

Thread 2)

Thats not possible.

Only pthread synchronization are mutexes (or rwlocks, or semaphore).
And you cannot release mutex/rwlock/semaphore *after* entering Thread1)

So yes, there is a race condition.

In you example, it can happens that thread 1 must sleep 10 seconds
before calling select(), because of system scheduling. Your sleep(1)
cannot garant that the close() is done after the select() call blocked
into kernel. You can try whatever semaphore you want, you wont be able
to have a 100% reliable program (even on Solaris)


> Cheers,
> Andre Ben Hamou
> Imperial College London

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 14:03    [W:0.035 / U:37.756 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site