[kubectl-plugin] Skip out-of-range file modes and close files during log download - #5054
Open
SrikarChittemsetty wants to merge 1 commit into
Conversation
…log download downloadRayLogFiles checked whether a tar entry's mode fits in an os.FileMode but did not act on the result: the warning was printed and the code fell through to create the file with a truncated mode anyway. The accompanying //nolint:gosec comment claimed the cast was guarded when it was not. Adding a bare continue is not sufficient, because the extraction loop advances the tar reader after the switch; skipping to the next iteration would re-read the same header forever. Guard the write with an else branch instead. Separately, the per-file close was deferred inside the extraction loop, so every descriptor opened while extracting an archive stayed open until the whole download finished. Move the write into a helper so the file is closed at the end of each entry, and surface close errors instead of discarding them. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Srikar Chittemsetty <srikarchittemsetty@gmail.com>
SrikarChittemsetty
force-pushed
the
fix-log-download-mode-guard-and-fd-leak
branch
from
September 3, 2026 20:47
2edeb8e to
d2bda5c
Compare
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.
Why are these changes needed?
Two bugs in
downloadRayLogFiles(kubectl-plugin/pkg/cmd/log/log.go), reported in #4779.1. The mode bounds check does nothing. The code checks whether
header.Modefits in anos.FileModeand prints a warning, but has nocontinue/guard — it falls straight through toos.OpenFile(..., os.FileMode(header.Mode))and creates the file with a truncated mode. The//nolint:gosec // overflow is guarded by bounds check abovecomment asserted a guard that was not there, so the linter stayed silent.Worth noting for reviewers: the fix suggested in the issue (adding a bare
continue) would hang. The extraction loop advances the tar reader after theswitch:so
continuewould re-read the same header forever. This PR guards the write with anelsebranch instead, which skips the file without skipping the advance. A regression test asserts the call returns rather than stalling.2. File descriptors accumulate for the whole download.
defer outFile.Close()sat inside the extraction loop, sodeferonly ran whendownloadRayLogFilesreturned — every file in the archive stayed open until the entire download finished. Extracting a log archive with many files holds that many descriptors at once.The per-entry write moves into a
writeTarFilehelper so the file closes at the end of each entry, and close errors are surfaced rather than discarded.Related issue number
Closes #4779
Checks
go build ./...,go vet, andgofmtare clean.TestDownloadRayLogFilescoverage withTestDownloadRayLogFilesSkipsOutOfRangeMode, which asserts the out-of-range entry is skipped, that the following valid entry still extracts, and that extraction does not stall. Against the previous behaviour it fails with:Disclosure: this change was prepared with the assistance of an AI coding agent. The analysis, fix, and tests were verified against the source and the test suite before submitting.