matcha icon indicating copy to clipboard operation
matcha copied to clipboard

BUG: File descriptor leak in DEBUG_IMAP logger

Open andrinoff opened this issue 7 months ago • 1 comments

Describe the bug In fetcher/fetcher.go lines 37-51, the DEBUG_IMAP file handle is opened via sync.Once but never closed, leaking file descriptor for application lifetime.

To reproduce

  1. Set DEBUG_IMAP environment variable to file path
  2. Start matcha
  3. Examine fetcher/fetcher.go lines 37-51:
var (
    debugIMAPFile *os.File
    debugIMAPOnce sync.Once
)

func getDebugIMAPWriter() io.Writer {
    debugIMAPOnce.Do(func() {
        if path := os.Getenv("DEBUG_IMAP"); path != "" {
            f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
            if err == nil {
                debugIMAPFile = f  // Never closed
            }
        }
    })
    if debugIMAPFile != nil {
        return debugIMAPFile
    }
    return nil
}
  1. Notice file handle opened but no cleanup mechanism exists

Expected behavior File descriptor should be closed on application shutdown. Options:

  1. Register cleanup function via defer in main() or init()
  2. Add CloseDebugFiles() function called during shutdown
  3. Use atexit-style cleanup registration

Additional context

  • Good first issue: straightforward resource cleanup
  • Low severity (only affects debug mode, single FD leak)
  • Pattern could apply to other debug loggers in codebase
  • Comment at line 35 acknowledges "opened once via debugIMAPOnce to avoid leaking a descriptor per connection" but doesn't address lifetime leak

Suggested fix:

// In fetcher package, add cleanup function:
func CloseDebugFiles() {
    if debugIMAPFile != nil {
        debugIMAPFile.Close()
    }
}

// In main package shutdown:
defer fetcher.CloseDebugFiles()

OS All platforms

andrinoff avatar Apr 21 '26 08:04 andrinoff

This has had no activity for 30 days. It will be closed in 14 days unless updated. Comment or remove the stale label to keep it open.

floatpanebot avatar May 22 '26 06:05 floatpanebot