Skip to content

fix(i2c): prevent file descriptor leak in SMBus usage#3878

Merged
patrickelectric merged 1 commit intobluerobotics:masterfrom
gaojulong:patch-3
Apr 16, 2026
Merged

fix(i2c): prevent file descriptor leak in SMBus usage#3878
patrickelectric merged 1 commit intobluerobotics:masterfrom
gaojulong:patch-3

Conversation

@gaojulong
Copy link
Copy Markdown
Contributor

@gaojulong gaojulong commented Apr 16, 2026

Tested on Raspberry Pi with repeated I2C scans.

Fix a file descriptor leak in check_for_i2c_device.

SMBus instances were opened without being closed, causing file descriptors to accumulate over time. In long-running scenarios, this could reach the system ulimit and trigger:

OSError: [Errno 24] Too many open files

As a result, flight controller boards may fail to be detected or become inaccessible.

Changes

  • Use context manager (with SMBus(...) as bus) to ensure proper cleanup

Impact

  • Prevents FD exhaustion
  • Improves reliability of board detection

Summary by Sourcery

Bug Fixes:

  • Fix a file descriptor leak in I2C SMBus usage during device detection that could lead to exhausting available file handles.

Note

Low Risk
Small, localized resource-management change that preserves existing behavior while reducing the chance of FD exhaustion during repeated I2C scans.

Overview
Prevents file descriptor leaks during I2C probing by switching LinuxFlightController.check_for_i2c_device to use with SMBus(bus_number) as bus so the bus handle is always closed after each check.

This improves reliability of repeated board-detection scans that call check_for_i2c_device (e.g., in navigator.py) by avoiding exhaustion of open file descriptors.

Reviewed by Cursor Bugbot for commit 59240a8. Bugbot is set up for automated code reviews on this repo. Configure here.

Fix a file descriptor leak in check_for_i2c_device.

SMBus instances were opened without being closed, causing file
descriptors to accumulate over time. In long-running scenarios,
this could reach the system ulimit and trigger:

    OSError: [Errno 24] Too many open files

As a result, flight controller boards may fail to be detected
or become inaccessible.

### Changes
- Use context manager (`with SMBus(...) as bus`) to ensure proper cleanup

### Impact
- Prevents FD exhaustion
- Improves reliability of board detection
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 16, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures I2C SMBus file descriptors are properly closed in Linux flight controller detection by using a context manager around SMBus usage in the I2C device check helper.

Sequence diagram for I2C device check with SMBus context manager

sequenceDiagram
    participant FlightControllerDetector
    participant LinuxBoards
    participant SMBus

    FlightControllerDetector->>LinuxBoards: check_for_i2c_device(bus_number, address)
    activate LinuxBoards
    LinuxBoards->>SMBus: __enter__() via with SMBus(bus_number)
    activate SMBus
    SMBus-->>LinuxBoards: bus instance
    LinuxBoards->>SMBus: read_byte_data(address, 0)
    SMBus-->>LinuxBoards: data or raise OSError
    alt read succeeded
        LinuxBoards->>SMBus: __exit__()
        LinuxBoards-->>FlightControllerDetector: True
    else OSError raised
        LinuxBoards->>SMBus: __exit__()
        LinuxBoards-->>FlightControllerDetector: False
    end
    deactivate SMBus
    deactivate LinuxBoards
Loading

Class diagram for LinuxBoards I2C device check using SMBus context manager

classDiagram
    class LinuxBoards {
        +check_for_i2c_device(bus_number int, address int) bool
    }

    class SMBus {
        +SMBus(bus_number int)
        +__enter__() SMBus
        +__exit__(exc_type object, exc_value object, traceback object) bool
        +read_byte_data(address int, register int) int
    }

    LinuxBoards ..> SMBus : uses_with_context_manager
Loading

File-Level Changes

Change Details Files
Ensure SMBus file descriptors are not leaked during I2C device detection.
  • Wrap SMBus(bus_number) usage in a with-statement context manager in the I2C device check helper
  • Retain the read_byte_data call inside the context block and keep existing OSError handling to signal missing/inaccessible devices
core/services/ardupilot_manager/flight_controller_detector/linux/linux_boards.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Member

@patrickelectric patrickelectric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gaojulong!

@patrickelectric patrickelectric merged commit e6c97dc into bluerobotics:master Apr 16, 2026
8 checks passed
@joaoantoniocardoso
Copy link
Copy Markdown
Member

@patrickelectric should we backport it to 1.4?

@patrickelectric
Copy link
Copy Markdown
Member

Not sure, I would prefer not since this touches a really sensible part of the vehicle.
We can release another minor release after stable if necessary with more fixes.

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