Skip to content

Fix thread safety race in UsbDrives property#383

Merged
imnasnainaec merged 6 commits into
masterfrom
fix/usb-drive-scan-exception
Jul 2, 2026
Merged

Fix thread safety race in UsbDrives property#383
imnasnainaec merged 6 commits into
masterfrom
fix/usb-drive-scan-exception

Conversation

@imnasnainaec

@imnasnainaec imnasnainaec commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

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 Reviewable

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>
@imnasnainaec imnasnainaec self-assigned this Jun 22, 2026
@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown

Test Results

       8 files     333 suites   2h 22m 49s ⏱️
   992 tests    936 ✔️   56 💤 0
3 157 runs  3 034 ✔️ 123 💤 0

Results for commit c08b0ac.

♻️ This comment has been updated with latest results.

imnasnainaec and others added 2 commits June 22, 2026 16:39
- 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>
@imnasnainaec imnasnainaec changed the title Keep USB drive scanning alive after a transient GetDrives exception Fix thread safety race in UsbDrives property Jun 22, 2026
@imnasnainaec imnasnainaec marked this pull request as ready for review June 22, 2026 21:00

@rmunn rmunn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@imnasnainaec imnasnainaec enabled auto-merge (squash) July 2, 2026 12:00
@imnasnainaec imnasnainaec merged commit b115dd0 into master Jul 2, 2026
5 of 6 checks passed
@imnasnainaec imnasnainaec deleted the fix/usb-drive-scan-exception branch July 2, 2026 13:04
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.

2 participants