sonar-dotnet
sonar-dotnet copied to clipboard
SE: Avoid FPs in concurrent code
Concurrent code can easily cause FPs in our symbolic execution engine. E.g.:
class ErrorLogger
{
public Queue<Exception> Queue { get; set; }
public async Task Start()
{
Queue = new();
do
{
await Task.Delay(1000);
if (Queue.Dequeue() is { } exception) // FP: S4158
Console.WriteLine(exception);
}
while (true);
}
}
We should investigate approaches to increase our precision for concurrent code.
Some ideas:
- Disable rules when specific code patterns are found.
-
lock
- we already do this for S2583/S2589 -
async
- this is most likely too broad - concurrent collections
-
- Do not learn/forget constraints for specific data types like concurrent collections.
- Do not track fields and properties for
async
methods and methods that includelock
statements. This would fix the example above.