Repository: pwr-Solaar/Solaar

Issue:
OSError exception in logitech_receiver/base.py
**Information**
- Solaar version (`solaar --version` or `git describe --tags` if cloned from this repository): **1.0.4**
- Distribution: **Fedora 33**
- Kernel version (ex. `uname -srmo`): **5.10.17-200.fc33.x86_64**
- Output of `solaar show`: **N/A** _(asked reporter to provide)_

**Describe the bug**
Solaar crashes with unhandled OSError exception.
```
Truncated backtrace:
base.py:262:_read:logitech_receiver.base.NoReceiver: {'reason': OSError(5, 'Input/output error')}

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/logitech_receiver/base.py", line 258, in _read
    data = _hid.read(int(handle), _MAX_READ_SIZE, timeout)
  File "/usr/lib/python3.9/site-packages/hidapi/udev.py", line 365, in read
    data = _os.read(device_handle, bytes_count)
OSError: [Errno 5] Input/output error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/solaar/ui/__init__.py", line 154, in <lambda>
    application.connect('startup', lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window), startup_hook)
  File "/usr/lib/python3.9/site-packages/solaar/ui/__init__.py", line 114, in _startup
    startup_hook()
  File "/usr/lib/python3.9/site-packages/solaar/listener.py", line 301, in start_all
    _process_receiver_event('add', device_info)
  File "/usr/lib/python3.9/site-packages/solaar/listener.py", line 377, in _process_receiver_event
    _start(device_info)
  File "/usr/lib/python3.9/site-packages/solaar/listener.py", line 286, in _start
    receiver.status.changed(True)
  File "/usr/lib/python3.9/site-packages/logitech_receiver/status.py", line 307, in changed
    for s in d.settings:
  File "/usr/lib/python3.9/site-packages/logitech_receiver/device.py", line 341, in settings
    self._feature_settings_checked = _check_feature_settings(self, self._settings)
  File "/usr/lib/python3.9/site-packages/logitech_receiver/settings_templates.py", line 763, in check_feature_settings
    setting = check_feature(device, name, featureId, featureFn)
  File "/usr/lib/python3.9/site-packages/logitech_receiver/settings_templates.py", line 741, in check_feature
    if featureId not in device.features:
  File "/usr/lib/python3.9/site-packages/logitech_receiver/hidpp20.py", line 315, in __contains__
    reply = self.device.request(0x0000, _pack('!H', ivalue))
  File "/usr/lib/python3.9/site-packages/logitech_receiver/device.py", line 424, in request
    return _base.request(
  File "/usr/lib/python3.9/site-packages/logitech_receiver/base.py", line 399, in request
    reply = _read(handle, timeout)
  File "/usr/lib/python3.9/site-packages/logitech_receiver/base.py", line 262, in _read
    raise NoReceiver(reason=reason)
logitech_receiver.base.NoReceiver: {'reason': OSError(5, 'Input/output error')}

Local variables in innermost frame:
handle: <_ThreadedHandle(/dev/hidraw0)>
timeout: 4000
```

**To Reproduce**
Steps to reproduce the behavior:
_unknown, asked reporter to reproduce_

**Additional context**
[Downstream bug report (private)](https://bugzilla.redhat.com/show_bug.cgi?id=1933399).
Looks similar to the last one in #995 .


Discussion:
This event may be happening after a resume when Solaar is trying to connect to all the devices that eere connected at the suspend.  One of the devices is not connected so a read from the device fails.   I'll see what happens under these circumstances on my setup.  It appears that the --restart-on-wakeup flag is required to cause this event at resume. I've not seen a need for this flag and it is marked experimental so if this is the case my first suggestion is to just run Solaar without this flag.

Alternatively if this is happening at some other time it looks as if the problem is that somehow a device was detected but the first interaction with the device failed.  This looks to be a rare event and I am unable to reproduce it and further it is unclear what should happen in this case.
I have a potential fix for this problem, but I'm going to release 1.0.5 without it as my cure might cause Solaar to become unstable - instead of crashing immediately.

Resolving patch:
diff --git a/lib/solaar/listener.py b/lib/solaar/listener.py
--- a/lib/solaar/listener.py
+++ b/lib/solaar/listener.py
@@ -392,3 +392,5 @@ def _process_receiver_event(action, device_info):
                     _error_callback('permissions', device_info.path)
             except Exception:
                 _error_callback('permissions', device_info.path)
+        except _base.NoReceiver:
+            _error_callback('nodevice', device_info.path)
diff --git a/lib/solaar/ui/__init__.py b/lib/solaar/ui/__init__.py
--- a/lib/solaar/ui/__init__.py
+++ b/lib/solaar/ui/__init__.py
@@ -54,6 +54,12 @@ def _error_dialog(reason, object):
             _('Found a Logitech Receiver (%s), but did not have permission to open it.') % object + '\n\n' +
             _("If you've just installed Solaar, try removing the receiver and plugging it back in.")
         )
+    elif reason == 'nodevice':
+        title = _('Cannot connect to device error')
+        text = (
+            _('Found a Logitech receiver or device at %s, but encountered an error connecting to it.') % object + '\n\n' +
+            _('Try removing the device and plugging it back in or turning it off and then on.')
+        )
     elif reason == 'unpair':
         title = _('Unpairing failed')
         text = (


Test patch: