Skip to content

agents/sysfsgpio: fix misleading Python error when permission denied #1704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

QSchulz
Copy link
Contributor

@QSchulz QSchulz commented Jul 23, 2025

Description

If the executor doesn't have the proper permissions to control the GPIO or export it, a Permission Denied will be raised. However, del is called regardless and gpio_sysfs_value_fd attribute isn't present if the init doesn't finish.

Thus, Python prints the following:
"""
Exception ignored in: <function GpioDigitalOutput.del at 0x7f5b017f80e0>
Traceback (most recent call last):
File "", line 44, in del
AttributeError: 'GpioDigitalOutput' object has no attribute 'gpio_sysfs_value_fd'
""" before the raised Exception, possibly misleading the user on their debugging quest.

Therefore, let's make sure the attribute exists before anything can throw an Exception in init.

os.close() can only be called on a valid file descriptor, which None isn't, therefore only call os.close() if self.gpio_sysfs_value_fd is not None.

This has been tested by running labgrid with a non-root user with the GPIO not exported and the non-root user not having permission to write to /sys/class/gpio/export.

Checklist

  • PR has been tested

If the executor doesn't have the proper permissions to control the GPIO
or export it, a Permission Denied will be raised. However, __del__ is
called regardless and gpio_sysfs_value_fd attribute isn't present if the
__init__ doesn't finish.

Thus, Python prints the following:
"""
Exception ignored in: <function GpioDigitalOutput.__del__ at 0x7f5b017f80e0>
Traceback (most recent call last):
  File "<loaded sysfsgpio>", line 44, in __del__
AttributeError: 'GpioDigitalOutput' object has no attribute 'gpio_sysfs_value_fd'
""" before the raised Exception, possibly misleading the user on their
debugging quest.

Therefore, let's make sure the attribute exists before anything can
throw an Exception in __init__.

os.close() can only be called on a valid file descriptor, which None
isn't, therefore only call os.close() if self.gpio_sysfs_value_fd is not
None.

Signed-off-by: Quentin Schulz <quentin.schulz@cherry.de>
Copy link

codecov bot commented Jul 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.6%. Comparing base (4398da7) to head (e1df6c1).
⚠️ Report is 9 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1704   +/-   ##
======================================
  Coverage    55.6%   55.6%           
======================================
  Files         172     172           
  Lines       13471   13473    +2     
======================================
+ Hits         7500    7502    +2     
  Misses       5971    5971           
Flag Coverage Δ
3.10 55.6% <100.0%> (+<0.1%) ⬆️
3.11 55.6% <100.0%> (+<0.1%) ⬆️
3.12 55.6% <100.0%> (+<0.1%) ⬆️
3.13 55.6% <100.0%> (+<0.1%) ⬆️
3.9 55.7% <100.0%> (+<0.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Emantor Emantor merged commit f0cb7da into labgrid-project:master Jul 26, 2025
11 checks passed
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.

3 participants