DopplerHQ / DopplerHQ/cli

[BUG] Potential file handle leak in completion.go

Open
#523 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
396
Forks
83
Avg merge
1d 2h
Merged PRs (30d)
3

Description

Description

The file pkg/cmd/completion.go has two instances where file handles are closed without using defer, which can cause resource leaks if errors occur between opening and closing the file.

Impact

If an error occurs after opening a file but before the explicit Close() call, the file handle will never be closed. This can lead to:

  • Resource exhaustion with many file handles open
  • Files locked on Windows
  • "Too many open files" errors

Locations

File: pkg/cmd/completion.go

Line 178:

f, err := os.Create(completionFile)
if err != nil {
    return err
}
// ... code that could error ...
f.Close()  // ← Not guaranteed to execute

Line 190:

f, err := os.Create(completionFile)
if err != nil {
    return err
}
// ... code that could error ...
f.Close()  // ← Not guaranteed to execute

Suggested Fix

Use defer to guarantee file closure:

f, err := os.Create(completionFile)
if err != nil {
    return err
}
defer f.Close()  // ← Guaranteed to execute

// ... rest of code ...

Steps to Reproduce

While this is hard to trigger in practice, you can simulate it by:

  1. Making the file write operation fail after opening
  2. Monitoring open file handles
  3. Observing that the file handle is never closed

Additional Context

This is a common Go anti-pattern. The Go community strongly recommends using defer for resource cleanup to prevent leaks.

Reference: Effective Go - Defer

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pkg/cmd/completion.go at the two os.Create paths around lines 178 and 190. Review how errors can occur before each explicit Close call, then verify both completion-generation paths release their file handles on every exit. Confirm the relevant completion commands still work after the cleanup change.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.