News for hackers. Stuff that matters.
Re:POSIX close(2) is broken by ay on Saturday December 17, 2011 @07:59AM (#3364161)
What about wrapping the first solution into a mutex lock ? Then the multithreaded behavior will disappear. And closing the fd might not be the most frequent operation usually so should not have much of performance impact.
Re:POSIX close(2) is broken by eis on Saturday December 17, 2011 @08:37AM (#3364204)
Only locking the close() is not enough. Another thread could still get the same file descriptor through whatever syscall while you loop around the close(). You would need to lock all syscalls that create or close a file descriptor which is a) infeasable and b) way too slow.

In other words: it's a race condition between allocating and deallocating a fd and not between two deallocations.

Re:POSIX close(2) is broken by JoeAltmaier on Saturday December 17, 2011 @08:03AM (#3364168)
Which mutex? What about threads you don't 'own', inside libraries for instance? I don't think you will be able to find "one ring to rule them all"
Re:POSIX close(2) is broken by ay on Saturday December 17, 2011 @08:17AM (#3364179)
Oh if they use the close direct, too bad. But libraries that launch the starships behind your back with no control left to you are also not the best gift.

Standard's bug aside, I wonder how the practical implementations behave. Is this condition reproducible ?