matcha
matcha copied to clipboard
BUG: File descriptor leak in DEBUG_IMAP logger
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
- Set DEBUG_IMAP environment variable to file path
- Start matcha
- 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
}
- Notice file handle opened but no cleanup mechanism exists
Expected behavior File descriptor should be closed on application shutdown. Options:
- Register cleanup function via defer in main() or init()
- Add CloseDebugFiles() function called during shutdown
- 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
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.