Skip to content

Fix thread safety race in UsbDrives property#383

Open
imnasnainaec wants to merge 5 commits into
masterfrom
fix/usb-drive-scan-exception
Open

Fix thread safety race in UsbDrives property#383
imnasnainaec wants to merge 5 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
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.

1 participant