[PATCH] sysdeps: Clear O_CREAT|O_ACCMODE when trying again on sem_open

Sergio Durigan Junior sergiodj@sergiodj.net
Wed Aug 23 14:23:45 GMT 2023


On Wednesday, August 23 2023, I wrote:

> On Wednesday, August 23 2023, Adhemerval Zanella Netto wrote:
>
>> On 23/08/23 01:21, Sergio Durigan Junior via Libc-alpha wrote:
>>> When invoking sem_open with O_CREAT as one of its flags, we'll end up
>>> in the second part of sem_open's "if ((oflag & O_CREAT) == 0 || (oflag
>>> & O_EXCL) == 0)", which means that we don't expect the semaphore file
>>> to exist.
>>> 
>>> In that part, open_flags is initialized as "O_RDWR | O_CREAT | O_EXCL
>>> | O_CLOEXEC" and there's an attempt to open(2) the file, which will
>>> likely fail because it won't exist.  After that first (expected)
>>> failure, some cleanup is done and we go back to the label "try_again",
>>> which lives in the first part of the aforementioned "if".
>>> 
>>> The problem is that, in that part of the code, we expect the semaphore
>>> file to exist, and as such O_CREAT (this time the flag we pass to
>>> open(2)) needs to be cleaned from open_flags, otherwise we'll see
>>> another failure (this time unexpected) when trying to open the file,
>>> which will lead the call to sem_open to fail as well.
>>> 
>>> This can cause very strange bugs, especially with OpenMPI, which makes
>>> extensive use of semaphores.
>>> 
>>> The fix here is to actually make sure that the O_CREAT|O_ACCMODE flags
>>> are clear after we enter "try_again".
>>> 
>>> See also: https://bugs.launchpad.net/ubuntu/+source/h5py/+bug/2031912
>>
>> This need needs a bug report and, if possible, a regression check (I give
>> you that it might be tricky due it is a racy condition).
>
> Sure thing.  I can provide a regression check using the openmpi bug
> we've been investigating, if that's acceptable.

https://sourceware.org/bugzilla/show_bug.cgi?id=30789

Let me know if that's enough.

Thanks,

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
https://sergiodj.net/


More information about the Libc-alpha mailing list