Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'InvalidOperationException' with message Collection was modified; enumeration operation may not execute in MultithreadedAnalyzeCommandBase, which is raised when analyzing with the --hashes switch. #2447

Merged
merged 11 commits into from
Feb 14, 2022
Merged
24 changes: 6 additions & 18 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public abstract class MultithreadedAnalyzeCommandBase<TContext, TOptions> : Plug
private OptionallyEmittedData _dataToInsert;
private Channel<int> _resultsWritingChannel;
private Channel<int> _fileEnumerationChannel;
private Dictionary<string, List<string>> _hashToFilesMap;
private IDictionary<string, HashData> _pathToHashDataMap;
private ConcurrentDictionary<int, TContext> _fileContexts;

Expand Down Expand Up @@ -470,23 +469,11 @@ private async Task<bool> HashAsync()
{
if (_computeHashes)
{
if (_hashToFilesMap == null)
{
_hashToFilesMap = new Dictionary<string, List<string>>();
}

TContext context = _fileContexts[index];
string localPath = context.TargetUri.LocalPath;

HashData hashData = HashUtilities.ComputeHashes(localPath);

if (!_hashToFilesMap.TryGetValue(hashData.Sha256, out List<string> paths))
{
paths = new List<string>();
_hashToFilesMap[hashData.Sha256] = paths;
}

paths.Add(localPath);
context.Hashes = hashData;

if (_pathToHashDataMap != null && !_pathToHashDataMap.ContainsKey(localPath))
Expand Down Expand Up @@ -873,21 +860,22 @@ protected virtual TContext DetermineApplicabilityAndAnalyze(

IAnalysisLogger logger = context.Logger;

int numberOfFiles = 1;
if (_computeHashes)
{
numberOfFiles = _hashToFilesMap[context.Hashes.Sha256].Count;
if (numberOfFiles > 1 && _analysisLoggerCache.ContainsKey(context.Hashes.Sha256))
if (_analysisLoggerCache.ContainsKey(context.Hashes.Sha256))
{
return context;
}
}

context.Logger.AnalyzingTarget(context);

if (_computeHashes && numberOfFiles > 1)
if (_computeHashes)
{
_analysisLoggerCache[context.Hashes.Sha256] = logger;
if (!_analysisLoggerCache.TryAdd(context.Hashes.Sha256, logger))
{
return context;
}
}

IEnumerable<Skimmer<TContext>> applicableSkimmers = DetermineApplicabilityForTarget(context, skimmers, disabledSkimmers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ public void AnalyzeCommandBase_MultithreadedShouldUseCacheIfFilesAreTheSame()
generateSameOutput: false,
expectedResultCode: 0,
expectedResultCount: 7,
expectedCacheSize: 0);
expectedCacheSize: 20);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expectedCacheSize

Can you explain what you learned here?

}

private static readonly IList<string> ComprehensiveKindAndLevelsByFileName = new List<string>
Expand Down