Skip to content

Fix potential crashes when mice connect or disconnect - #1388

Merged
lujjjh merged 1 commit into
mainfrom
fix/device-deallocation-crash-1384-review
Sep 12, 2026
Merged

lujjjh merged 1 commit into
mainfrom
fix/device-deallocation-crash-1384-review

Conversation

@lujjjh

@lujjjh lujjjh commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Mouse events are processed on a background thread, while device lists are updated on the main thread. Unsynchronized device lookups can race with device additions and removals.

This change makes device lookup safe across those threads and removes two unsafe lazy initializations.

Changes

  • Publish a lock-protected device routing snapshot keyed by HID service registry ID.
  • Retain the selected device while holding the lock, and release retired devices after unlocking to allow reentrant teardown.
  • Initialize Device.id and logitechSession before publishing the device to the event thread.

Validation

  • SwiftFormat, SwiftLint, and git diff --check passed.
  • 8 snapshot tests and 62 Logitech session tests passed with Thread Sanitizer enabled.
  • Removing the snapshot locks in an isolated control triggered a TSan data-race report.

The tests exercise the snapshot and session in isolation. They also pass with the original production routing code, so they do not establish end-to-end regression coverage or validate the Device initialization changes.

Related issue

Related to #1384, which reports crashes while a Bluetooth mouse is idle.

The crash was symbolicated to DevicePickerState.updateDevices(with:) releasing a dictionary and entering Device.deinit. This change addresses plausible contributing races, but the reported crash has not been reproduced or conclusively attributed to them.

@lujjjh lujjjh added the bug Something isn't working label Sep 12, 2026
@lujjjh
lujjjh merged commit 255a498 into main Sep 12, 2026
1 check passed
@lujjjh
lujjjh deleted the fix/device-deallocation-crash-1384-review branch September 12, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant