From 5f145238cf744a29342cf88de1bb13c600be92a0 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sun, 26 Apr 2026 02:30:52 -0700 Subject: [PATCH] fix: defer kitty debug-log Close (#909) --- fetcher/fetcher.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fetcher/fetcher.go b/fetcher/fetcher.go index 1422aa6d4cc7239837ed040b574c7ab468065f7a..52b450a63939d7ace3cb2c3fa86c785315674276 100644 --- a/fetcher/fetcher.go +++ b/fetcher/fetcher.go @@ -1041,10 +1041,16 @@ func FetchEmailBodyFromMailbox(account *config.Account, mailbox string, uid uint msg := fmt.Sprintf("[kitty-img] body selection html=%s plain=%s chosen=%s\n", htmlPartID, plainPartID, textPartID) log.Print(msg) if path := os.Getenv("DEBUG_KITTY_LOG"); path != "" { - if f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { + // Use a closure with defer so a panic between open and + // WriteString doesn't leak the file descriptor (#894). + func() { + f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return + } + defer f.Close() _, _ = f.WriteString(msg) - _ = f.Close() - } + }() } } if textPartID != "" {