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

Resolve 'collection modified' exception when logging results #2549

Merged
merged 8 commits into from
Oct 9, 2022

Conversation

michaelcfanning
Copy link
Member

This change introduces thread-safe utilization of a single CachingLogger instance in cases when multiple files with the same file hash are analyzed.

In these circumstances, the scan should not occur again, rather, the cached results (if any) should be updated (to reflect the current file path of the duplicated file) and 'replayed' into the log.

This is tricky because the scan pipeline design is intended for every context object (per scan target) to be accessed only on a single thread.

To make this work, we introduce a slim semaphore in the caching logger, which allows access to only a single thread at a time. This semaphore's Wait method is invoked on receiving the notice that a new scan target is being analyzed.

We also have a new ReleaseLock method, which is called after scan completes. This method releases the semaphore and also puts the caching logger into a 'committed' state, meaning that the first copy, by file hash, of a set of duplicated targets has been analyzed and we now have a fixed set of results that can be 'replayed' into the log.

@suvamM

SingleWriter = true,
SingleReader = true,
};
readyToHashChannel = Channel.CreateBounded<int>(channelOptions);
Copy link
Member Author

@michaelcfanning michaelcfanning Oct 6, 2022

Choose a reason for hiding this comment

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

readyToHashChannel

Currently the hashing channel is single-threaded. There is an opportunity here, I think, to improve performance by making this phase multi-threaded.

/// Gets or sets a boolean value that indicates whether the Results
/// object associated with this logger is fixed and ready to replay.
/// </summary>
public bool CacheFinalized { get; private set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

CacheFinalized

Nice!

SingleWriter = true,
SingleReader = true,
};
readyToHashChannel = Channel.CreateBounded<int>(channelOptions);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a line break here and six lines down to improve readability?

findFiles.Wait();
hashFiles.Wait();
// 4: A single-threaded consumer watches for completed scans
// and logs results, if any. This operation is singlle-threaded
Copy link
Contributor

Choose a reason for hiding this comment

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

singlle

typo -> single

Copy link
Contributor

@marmegh marmegh left a comment

Choose a reason for hiding this comment

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

:shipit:

@michaelcfanning michaelcfanning enabled auto-merge (squash) October 9, 2022 00:32
@michaelcfanning michaelcfanning merged commit a3b8dd1 into main Oct 9, 2022
@michaelcfanning michaelcfanning deleted the current-threading-poc branch October 9, 2022 00:33
This was referenced Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants