fix(i2c): prevent file descriptor leak in SMBus usage#3878
Merged
patrickelectric merged 1 commit intobluerobotics:masterfrom Apr 16, 2026
Merged
fix(i2c): prevent file descriptor leak in SMBus usage#3878patrickelectric merged 1 commit intobluerobotics:masterfrom
patrickelectric merged 1 commit intobluerobotics:masterfrom
Conversation
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
Reviewer's guide (collapsed on small PRs)Reviewer's GuideEnsures 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 managersequenceDiagram
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
Class diagram for LinuxBoards I2C device check using SMBus context managerclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Member
|
@patrickelectric should we backport it to 1.4? |
Member
|
Not sure, I would prefer not since this touches a really sensible part of the vehicle. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
As a result, flight controller boards may fail to be detected or become inaccessible.
Changes
with SMBus(...) as bus) to ensure proper cleanupImpact
Summary by Sourcery
Bug Fixes:
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_deviceto usewith SMBus(bus_number) as busso 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., innavigator.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.