Skip to content

Bug: sysfs_remove, sysfs_rescan, and sysfs_reset proceed to open() after detecting missing path #16

@tsyrulb

Description

@tsyrulb

Description

Three functions in pci/device.py check whether a sysfs path exists but continue to open() the path even when the check fails. This causes a FileNotFoundError instead of gracefully skipping the operation.

Affected Code

File: pci/device.py

sysfs_remove (lines 449-454):

def sysfs_remove(self):
    remove_path = os.path.join(self.dev_path, "remove")
    if not os.path.exists(remove_path):
        debug("%s remove not present: '%s'", self, remove_path)
    # Missing return here — falls through to open()
    with open(remove_path, "w") as f:
        f.write("1")

sysfs_rescan (lines 456-461): Same pattern — missing return after debug log.

sysfs_reset (lines 493-499): Same pattern — missing return after error log.

Expected Behavior

These functions should return early when the path doesn't exist, matching the pattern used by other functions in the same file:

  • sysfs_power_control_set (line 444): has return
  • sysfs_unbind (line 467): has return
  • sysfs_bind (line 476): has return
  • sysfs_get_driver (line 484): has return None
  • sysfs_get_module (line 490): has return None

Suggested Fix

Add return after the existence check in each of the three functions:

def sysfs_remove(self):
    remove_path = os.path.join(self.dev_path, "remove")
    if not os.path.exists(remove_path):
        debug("%s remove not present: '%s'", self, remove_path)
        return  # <-- add this
    with open(remove_path, "w") as f:
        f.write("1")

Same fix for sysfs_rescan and sysfs_reset.

Version

Tag v2025.11.21 (commit 6495b91)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions