コードインスペクション: 取得したロックの順序に一貫性がない
クラスにスレッドセーフを実現するためにロックで保護された複数のリソースがある場合、JetBrains Rider はマルチスレッド環境での可能な実行パス(クラスのパブリック API が複数のスレッドで同時に使用されることを想定)と、そのような実行パスで取得されるロックの順序を分析し、実行時にデッドロックが発生する可能性のあるサイクルを下記の例のように見つけます。 警告メッセージは、形成される可能性のあるサイクルの詳細な説明と例を提供します。
class MultiThreadedComponent
{
private List<string> _resource1 = new();
private List<string> _resource2 = new();
public void PublicApi01()
{
lock (_resource1)
lock (_resource2)
{
// do work
}
}
public void PublicApi02()
{
lock (_resource2)
{
// do work
HelperMethod02();
// do work
}
}
private void HelperMethod02()
{
// The expression is used in several lock statements with inconsistent execution order,
// forming a cycle. This might lead to a possible deadlock if methods (properties)
// of this type are executed concurrently by multiple threads.
// Lock objects involved in the cycle: '_resource1', '_resource2'
// Example of the deadlock situation:
// - Thread #1 executes 'PublicApi01', takes locks '_resource1' -> '_resource2'
// - Thread #2 executes 'PublicApi02', takes locks '_resource2' -> '_resource1'
lock (_resource1)
{
// do work
}
}
}
2026 年 6 月 12 日