Search code examples
cmultithreadingpthreadsassert

Pthread mutex lock assertion fails


It's several days I'm trying to get ahead of this issue but seems like not my fault. I can't post any code because it's big enough. I'll try to explain it as better as I can. First it is a multithreaded server application which receive request from multiple clients, also at same time, and elaborate it using master-workers (pool of threads) style with AF_UNIX sockets. While it's running it often return this error :

[name]: ../nptl/pthread_mutex_lock.c:81: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed.

I'm not able to replicate this error or to catch it with valgrind or gdb (I tried several times). I use several global pthread_mutex_t initialized with PTHREAD_MUTEX_INITIALIZERand I never destroy them. I'm pretty sure to lock and unlock them safely around the code like this:

  1. pthread_mutex_lock(&mutex);
  2. if something fails and I've to return from a thread: pthread_mutex_unlock(&mutex); and return (void*) -1;
  3. else : do things with shared variables
  4. pthread_mutex_unlock(&mutex);

I've read that it could be caused by a lower level race condition in pthread_mutex_lock file but I'm not sure. By the way I would understand why this assertion could fails generically


Solution

  • This usually indicates that the lock has become corrupted in some way. One possibility is that you are trying to lock a lock that was never initialized or has already been destroyed and not re-initialized. Another possibility is that some other piece of code is stomping on the mutex, possibly by accessing an array out of bounds or by accessing some memory after it has been freed.