Repository navigation
fix(hostsensor): close kernel variable files per entry, not per directory - #1039
Conversation
…tory Signed-off-by: manoj-1407 <manojkumar148700@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
matthyx
left a comment
There was a problem hiding this comment.
Approve — no material blockers found in 02d2e583437831c14b039a0534f1306c7ad8ee83, targeting main (PR-reported base 02f0bf7b21fd98da0d6f6935d3c49503d6978738). Independently checked current main ef6ef6a1adc4d6b823e043ac8ba56bb3135a2e29: subsequent changes do not alter the host-sensor package or this fix.
The fix is needed: the existing loop retains processed sibling descriptors until the walk returns. readVarsEntry gives each descriptor one owner and closes it before the next sibling, including skip/error paths, while keeping directory handles alive during recursion. Values, source paths, ordering, permission handling, and error wrapping remain compatible. No public API, configuration, schema, or dependency changes. Independent correctness and architecture reviews found no blockers.
Validation in a credential-free, capability-dropped Go 1.27.1 container:
go test -p 3 ./pkg/hostsensormanager/ -run KernelVariables -count=1 -v: all four tests passed, without skips.go vet -p 3 ./pkg/hostsensormanager/: passed.- Replaced only
sensor_kernelvars.gowith the target-base implementation and repeated the tests: the descriptor regression failed withtoo many open files; the other three passed. - Formatting and diff whitespace checks passed. Initial container attempts ran out of disk space; rerunning with temporary build storage succeeded. Full-suite, race, and live-node checks were not run. Permission/read-error branches were reviewed statically.
History searches across open/closed/merged PRs and issues used kernel-variable, hostsensormanager, proc/sys/kernel, descriptor, RLIMIT_NOFILE, affected-symbol/file, and too-many-open-files terms (100-result cap per query; search indexing is not exhaustive). The original #681 discussion already identified this exact deferred-close problem; this PR addresses it. #998 concerns procfs event-path resolution, and #984 adds sensor exclusions: neither fixes descriptor accumulation. Excluding this sensor avoids collection rather than preserving it. No competing fix or explicit rejection of this approach was found. Related host-identity #982 was deferred (“closing for now”), not a rejection of this fix.
No existing review threads or outstanding reviews were present on final recheck. DCO, GitGuardian, and CodeRabbit passed; test workflows await approval. That unrun CI does not change this approval.
Overview
walkVarsDirinsensor_kernelvars.goopened each entry of a /proc/sys/kernel directory and diddefer varFile.Close()inside the loop. A defer runs when the function returns, not at the end of the iteration, so every file in a directory stayed open until the whole directory (and everything nested in it) had been read. Open file descriptors grew with the number of entries in a directory instead of staying constant.The per-entry work is now in
readVarsEntry, where the defer is correct, so each handle is closed as soon as that entry is done. Behaviour is the same otherwise: same skip rules for "permission denied" and "operation not permitted", same errors, same recursion. Open descriptors now depend on directory depth instead of directory size.Additional Information
On a normal node you probably won't hit this: a real /proc/sys/kernel doesn't have that many entries and the usual fd limit is high. It shows up with a low
RLIMIT_NOFILEor when the agent is already using a lot of descriptors. Any error return halfway through the walk also left all those handles pending.The regression test fakes a host root with 300 variables, lowers
RLIMIT_NOFILEto the current open fds + 64, and walks it. On the old code it fails withfailed to open file (.../proc/sys/kernel/var204): ... too many open files. It restores the limit afterwards and skips if the limit can't be lowered. Since the limit is process-wide it's worth knowing about if parallel tests ever get added to this package (none uset.Paralleltoday).How to Test
go test ./pkg/hostsensormanager/ -run KernelVariables -count=1 -vThere are also tests for collecting files and nested directories, an empty directory, and a missing /proc/sys/kernel.
Summary by CodeRabbit