Skip to content

[1.4] core: services: ardupilot_manager: Prevent i2c file descriptor leak (backport #3878) - #4125

Merged
patrickelectric merged 1 commit into
bluerobotics:1.4-devfrom
joaoantoniocardoso:backports/1.4/i2c-fd-leak
Aug 11, 2026
Merged

[1.4] core: services: ardupilot_manager: Prevent i2c file descriptor leak (backport #3878)#4125
patrickelectric merged 1 commit into
bluerobotics:1.4-devfrom
joaoantoniocardoso:backports/1.4/i2c-fd-leak

Conversation

@joaoantoniocardoso

@joaoantoniocardoso joaoantoniocardoso commented Aug 11, 2026

Copy link
Copy Markdown
Member

While working on #4121, I found the same problem already fixed on master.

Backport of #3878 (e6c97dc4a) to 1.4-dev.

check_for_i2c_device opens an SMBus and never closes it. Every failed probe leaks one /dev/i2c-* file descriptor, so on a vehicle whose Navigator is missing or unresponsive, ardupilot-manager walks the whole detector list on every available_boards call and steadily burns through the 1024 FD soft limit.

Reproduced on hardware

Raspberry Pi 4 with the Navigator removed, running current 1.4-dev:

  • Board detection correctly finds no Navigator, available_boards returns SITL only.
  • Within ~4 minutes the ardupilot-manager process holds 1024/1024 FDs, 1015 of them /dev/i2c-1.
  • Its HTTP server then dies with OSError: [Errno 24] Too many open files on socket.accept(); GET /v1.0/board and /v1.0/available_boards stop responding entirely.
  • Every rejected connection dumps a loguru backtrace, growing the ardupilot-manager log directory by roughly 20 MB/min (189 MB in 15 minutes, rotating 10 MB files every ~30 s).

A Raspberry Pi 5 with its Navigator attached, same image, stays flat at 15 FDs total (5 i2c) and under 1 MB of logs, which matches the leak being on the OSError path.

Change

Use the context manager so the bus is closed on both the success and failure paths:

with SMBus(bus_number) as bus:
    bus.read_byte_data(address, 0)

Test plan

  • Reproduced FD exhaustion and API death on a Pi 4 without the Navigator on unpatched 1.4-dev
  • With the patch applied on the same board (still no Navigator), 200 available_boards requests driving ~1770 detection sweeps left the FD count flat at 8 with zero open /dev/i2c-* handles, no Errno 24, API still answering 200, and the log file at 1.2 MB after several minutes

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

(cherry picked from commit e6c97dc)
@joaoantoniocardoso
joaoantoniocardoso marked this pull request as draft August 11, 2026 20:07
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown

Automated PR Review

0. Summary

  • Verdict: LGTM :shipit:

Straight backport of #3878 to 1.4-dev. Wraps SMBus(bus_number) in a with block inside check_for_i2c_device so the /dev/i2c-* file descriptor is closed on both the success and OSError paths, preventing FD exhaustion in ardupilot-manager when the Navigator is missing.

No further comments, nice job 👍

Generated by PR Review Bot. This is advisory, a human reviewer must still approve.

@joaoantoniocardoso
joaoantoniocardoso marked this pull request as ready for review August 11, 2026 20:19
@joaoantoniocardoso
joaoantoniocardoso requested a review from a team August 11, 2026 20:19
@patrickelectric
patrickelectric merged commit 71500f9 into bluerobotics:1.4-dev Aug 11, 2026
8 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