CodeQL documentation

Inconsistent lock sequence

ID: cs/inconsistent-lock-sequence
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - concurrency
   - correctness
   - external/cwe/cwe-662
Query suites:
   - csharp-code-quality.qls
   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

Locks held concurrently should be locked in a consistent sequence, otherwise the program can deadlock. This rule detects nested lock statements that lock variables in a different sequence in different parts of the program.

Recommendation

This problem can be avoided by ensuring that nested lock statements always lock variables in the same sequence.

Example

The following example shows a program running two threads, which deadlocks because thread1 holds