Skip to content

Question about comment in acl_threadsupport.c regarding design and race conditions #276

@IlanTruanovsky

Description

@IlanTruanovsky

This came up because I was trying to solve a Coverity issue regarding the same code.

Regarding this comment in acl_threadsupport.c, I find it hard to see how this was properly designed. The comment suggests that the thread calling the acl_reset_condvar function may need to unlock C->waiter_mutex since it could have been locked by another thread that exited before unlocking the mutex. There are two problems that I see with this. First, according to https://linux.die.net/man/3/pthread_mutex_unlock:

If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results.

Isn't this design doing exactly what the manual tells us is undefined behavior?

Also, by calling pthread_mutex_trylock, we introduce a race condition where we unlock an already unlocked mutex. For example, suppose thread A is calling acl_reset_condvar and thread B is some other thread holding the mutex.
Then, pthread_mutex_trylock in thread A does not lock the mutex. Suppose that after that function call, thread B unlocks the mutex. Now the mutex is no longer held by anyone, but thread A will try to unlock it anyways in line 300 which leads to undefined behavior.

Maybe we should assume that the mutex is already unlocked when calling acl_reset_condvar? To me, if some other thread exited while holding the mutex, that seems more like a problem in the user's code.

Resolving these issues may also resolve many Coverity issues complaining about a double unlock of the mutex (although I'm not 100% sure).

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions