Fix thread safety race in UsbDrives property#383
Merged
Conversation
The try/catch wrapped the entire while loop, so a ManagementException or similar hardware error from GetDrives permanently stopped drive detection for the session. Move the try/catch inside the loop so each scan cycle is isolated: exceptions are logged and the loop retries after the normal 3-second sleep. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Use ex.ToString() instead of ex.Message so stack trace and inner exceptions are preserved in the log. - Return _usbDrives.ToList() inside the lock so callers enumerate a snapshot rather than the live list, closing a pre-existing race where the background thread could mutate the list during enumeration. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…lures Silent logging masked the error for users with persistently faulty hardware. Restore the outer try/catch with ErrorReport.ReportNonFatalException so those users still receive feedback. Keep the ToList() snapshot fix in UsbDrives. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
rmunn
approved these changes
Jul 2, 2026
rmunn
left a comment
Contributor
There was a problem hiding this comment.
This is a decent solution to solve the immediate problem. A more effective solution would involve rewriting the class to use thread-safe collections like ConcurrentDictionary (there's no ConcurrentList, but since in this case order doesn't matter, it can be simulated by using a dictionary where the keys and values are the same). But making a copy before returning it is definitely enough for our use case: we don't need the list of USB drives to be updated "live" when someone plugs a new drive in, because if the drive doesn't show up, we can exit and reenter the dialog and then the drive will appear.
So this is fine as-is, and rewriting to use a thread-safe collection isn't truly needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The `UsbDrives` property returned the live `List` reference while holding the lock, but callers (e.g. `SyncStartModel`) enumerated the result after the lock was released. The background scan thread can concurrently call `Clear()`/`AddRange()` during that enumeration, causing `InvalidOperationException: Collection was modified during enumeration`.
Return `_usbDrives.ToList()` inside the lock so callers get a snapshot.
(This is a pre-existing issue unrelated to #261, which was already addressed in #272.)
Devin review: https://app.devin.ai/review/sillsdev/chorus/pull/383
This change is