Skip to content

Fix device selection not persisting across restarts#151

Open
gratis-katze wants to merge 1 commit intoandyvorld:masterfrom
gratis-katze:fix/device-persistence
Open

Fix device selection not persisting across restarts#151
gratis-katze wants to merge 1 commit intoandyvorld:masterfrom
gratis-katze:fix/device-persistence

Conversation

@gratis-katze
Copy link
Copy Markdown

Problem

After every PC restart, users must manually click "Rediscover Devices" and re-select their device. The battery icon either shows "Not Initialised" indefinitely or disappears entirely.

Root cause

Three issues combine to cause this:

1. string.GetHashCode() is non-deterministic in .NET Core (primary cause)

In HidppDevice.cs, devices without HID++ feature 0x0003 fall back to:

Identifier = $"{DeviceName.GetHashCode():X04}";

In .NET Core, string.GetHashCode() is randomized per-process by design. The same device name produces a different hash on every launch, so the saved device ID never matches the newly computed one. The placeholder created by LoadPreviouslySelectedDevices is never matched by OnInitMessage, which instead adds a second unchecked entry with the new hash — forcing the user to re-select every time.

2. Startup race condition

LogiDeviceCollection subscribed to device messages before calling LoadPreviouslySelectedDevices(). An InitMessage arriving in that narrow window would see an empty Devices list and create an unchecked entry, after which LoadPreviouslySelectedDevices would add a duplicate checked placeholder — causing a SingleOrDefault exception on subsequent messages.

3. New devices not respecting saved selections

When OnInitMessage creates a brand-new device entry (not found in Devices), it always set IsChecked = false, even if the device ID was in SelectedDevices.

Fix

  • LGSTrayHID/HidppDevice.cs: Replace GetHashCode() with SHA256, which is deterministic across processes and restarts.
  • LGSTrayUI/LogiDeviceCollection.cs: Move LoadPreviouslySelectedDevices() before the subscriber setup to eliminate the race condition.
  • LGSTrayUI/LogiDeviceCollection.cs: In OnInitMessage, pre-check newly discovered devices that are already in SelectedDevices.

- Replace DeviceName.GetHashCode() with SHA256 in HidppDevice.cs;
  GetHashCode() is randomized per-process in .NET Core, causing the
  saved device ID to never match the newly computed one on restart.
- Load previously selected device placeholders before subscribing to
  device messages to close a startup race condition.
- In OnInitMessage, pre-check newly discovered devices that are already
  in SelectedDevices, as a belt-and-suspenders fix.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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