From: Eric Dumazet <dada1@cosmosbay.com>
To: Andre Ben Hamou <andre@bluetheta.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Multithread select() bug
Date: Tue, 11 May 2004 00:31:42 +0200 [thread overview]
Message-ID: <40A002CE.4020906@cosmosbay.com> (raw)
In-Reply-To: <409FFE22.4050508@bluetheta.com>
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)
close(fd)
Thats not possible.
Only pthread synchronization are mutexes (or rwlocks, or semaphore).
And you cannot release mutex/rwlock/semaphore *after* entering Thread1)
select()
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)
Eric
> Cheers,
>
> Andre Ben Hamou
> Imperial College London
>
next prev parent reply other threads:[~2004-05-10 22:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-05-10 21:26 Multithread select() bug Andre Ben Hamou
2004-05-10 21:47 ` Davide Libenzi
2004-05-10 21:56 ` Andre Ben Hamou
2004-05-10 22:09 ` Davide Libenzi
2004-05-10 21:57 ` Eric Dumazet
2004-05-10 22:11 ` Andre Ben Hamou
2004-05-10 22:31 ` Eric Dumazet [this message]
2004-05-10 23:01 ` Andre Ben Hamou
2004-05-11 6:07 ` Armin Schindler
2004-05-11 6:24 ` Eric Dumazet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=40A002CE.4020906@cosmosbay.com \
--to=dada1@cosmosbay.com \
--cc=andre@bluetheta.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox