Skip to content

Fix temp directory leak in getDelve#1665

Open
SebTardif wants to merge 1 commit into
ko-build:mainfrom
SebTardif:fix-getdelve-tmpdir-leak
Open

Fix temp directory leak in getDelve#1665
SebTardif wants to merge 1 commit into
ko-build:mainfrom
SebTardif:fix-getdelve-tmpdir-leak

Conversation

@SebTardif

Copy link
Copy Markdown
Contributor

What

getDelve creates a temp directory with this layout:

<tmpInstallDir>/
├── delve/          <- git clone (~50 MB)
└── bin/
    └── linux_amd64/
        └── dlv     <- built binary

The caller cleans up with os.RemoveAll(filepath.Dir(delveBinary)), which resolves to .../bin/linux_amd64/, only the deepest subdirectory. The git clone and temp root are leaked on every debug build.

Additionally, 3 of 4 error paths inside getDelve (after MkdirTemp) skip cleanup entirely.

Fix

  1. Return (binaryPath, cleanupDir, error) from getDelve so the caller can os.RemoveAll the actual temp root.
  2. Add a deferred cleanup inside getDelve that runs on any error path (disabled on success via a success flag).

Testing

  • gofmt: no issues
  • go vet ./pkg/build/...: pass
  • go build ./...: pass
  • go test ./pkg/build/... -count=1: pass

Bug introduced in

Commit 1c83f790f in #1320 refactored getDelve from go install (flat directory) to git-clone + go build (nested directories) without updating the caller's cleanup path.

getDelve creates a temp directory containing a git clone of Delve and
the built binary. The caller removes filepath.Dir(delveBinary), which
only deletes .../bin/<os_arch>/ and leaves the clone directory and temp
root behind. On a 1GB codebase with frequent debug builds, this leaks
megabytes of temp files per build.

Return the temp root from getDelve so the caller can clean up the
entire directory tree. Also add deferred cleanup on error paths inside
getDelve itself.
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.

1 participant