A mutex is locked a second time in the same block without being unlocked in between

tx0/c:mutex-locked-twicedefaultwarningreliability/concurrencydepth fastbug, cwe-764

What it finds

pthread_mutex_lock(&m); ... pthread_mutex_lock(&m);: a normal mutex locked again by the thread that holds it deadlocks, and an error-checking one fails. Usually an unlock was lost when the code between was edited.

Both calls must be statements of the same block with the same argument, and nothing between them may unlock, wait on a condition or call a function that is given the same argument.

From the rule's source, mutex_locked_twice.rs.

Example from the rule's tests
#include <pthread.h> static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;static int counter; void bump(void){  pthread_mutex_lock(&lock);  counter++;  pthread_mutex_lock(&lock);tx0 reports: &lock is locked again without being unlocked since the last lock  pthread_mutex_unlock(&lock);}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:mutex-locked-twice" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:mutex-locked-twice] reason="..."

tx0 explain tx0/c:mutex-locked-twice shows the same in the terminal. Found a false positive? Report it.