sensors: close the descriptor getsensorid() opens to test the bus - #185
Merged
Conversation
`getsensorid()` opens the i2c adapter to find out whether it is there, and then never closes it. The probes it goes on to run open the adapter again and close what they open (`get_sensor_id_i2c()` ends in `close_sensor_fd(fd)`), so nothing needs the descriptor taken here — it is a pure leak, one per call, and up to six once the fall-through loop starts sweeping the other adapters. That is invisible for `ipctool`, which runs once and exits. It is not invisible for a daemon: majestic probes the sensor at every pipeline start *and* every SIGHUP reload, so a camera accumulates one open `/dev/i2c-N` per reload for the life of the process. Enough of them and `open()` and `accept()` start failing process-wide — which presents as a streamer that is still running, still holding its ports, and answering nothing, rather than as a descriptor leak. Measured on a lab ssc30kq (SigmaStar infinity6e, imx335), majestic master: | | `/dev/i2c-1` descriptors held | |---|---| | before, 26 reloads | 29 | | before, 7 more reloads | 36 | | with `$SENSOR` set, probe skipped, 5 reloads | 0 | | after this change, 8 reloads | 0 | The `$SENSOR` row is the control: it takes majestic's own path out of the picture and pins the leak on the probe. The test becomes `fd < 0` rather than `!fd`. `open()` reports failure as -1, so the old form read a failed open as success and only the impossible descriptor 0 as failure; the run then fell through to `get_sensor_id_i2c()`, which opened the adapter properly and failed there instead. No behaviour depended on the old spelling. Control flow is otherwise unchanged, deliberately: the fall-through loop still gives up on the first adapter it cannot open rather than skipping to the next one. Making that sweep actually sweep is a change to what gets detected, and it does not belong in a leak fix.
PR Summary by QodoPrevent I²C descriptor leaks during sensor detection
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can type 'qodo, fix this' on a finding and the fix lands right on your PR |
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.
getsensorid()opens the i2c adapter to find out whether it is there, and then never closes it. The probes it goes on to run open the adapter again and close what they open (get_sensor_id_i2c()ends inclose_sensor_fd(fd)), so nothing needs the descriptor taken here — it is a pure leak, one per call, and up to six once the fall-through loop starts sweeping the other adapters.That is invisible for
ipctoolitself, which runs once and exits. It is not invisible for a daemon: majestic probes the sensor at every pipeline start and every SIGHUP reload (since #184 and its majestic-side bump), so a camera accumulates one open/dev/i2c-Nper reload for the life of the process. Enough of them andopen()andaccept()start failing process-wide — which presents as a streamer that is still running, still holding its ports, and answering nothing, rather than as a descriptor leak.Measured
Lab ssc30kq (SigmaStar infinity6e, imx335), majestic master+e2b88b18:
/dev/i2c-1descriptors held$SENSORset so the probe is skipped, 5 reloadsThe
$SENSORrow is the control: it takes majestic's own path out of the picture and pins the leak on the probe rather than on the vendor SDK.After the change, total process descriptors are flat at 41→42 across the reloads,
Autodetected sensor as 'imx335_i2c'is still logged on every one of them — so each is a genuine probe, not a remembered answer — and the camera keeps answering (/image.jpg200) throughout a soak of 27 reloads under two steady RTSP pullers, four churning clients, snapshot polling at 0.5 Hz and motion firing every few seconds.Also
The test becomes
fd < 0rather than!fd.open()reports failure as -1, so the old form read a failed open as success and only the impossible descriptor 0 as failure; the run then fell through toget_sensor_id_i2c(), which opened the adapter properly and failed there instead. Nothing depended on the old spelling.Deliberately not here
Control flow is otherwise unchanged: the fall-through loop still gives up on the first adapter it cannot open rather than skipping to the next one. Making that sweep actually sweep changes which sensors get detected on which boards, and that does not belong in a leak fix.