Skip to content

Commit 5f14523

Browse files
authored
fix: defer kitty debug-log Close (#909)
1 parent 59bba11 commit 5f14523

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

fetcher/fetcher.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,16 @@ func FetchEmailBodyFromMailbox(account *config.Account, mailbox string, uid uint
10411041
msg := fmt.Sprintf("[kitty-img] body selection html=%s plain=%s chosen=%s\n", htmlPartID, plainPartID, textPartID)
10421042
log.Print(msg)
10431043
if path := os.Getenv("DEBUG_KITTY_LOG"); path != "" {
1044-
if f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
1044+
// Use a closure with defer so a panic between open and
1045+
// WriteString doesn't leak the file descriptor (#894).
1046+
func() {
1047+
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
1048+
if err != nil {
1049+
return
1050+
}
1051+
defer f.Close()
10451052
_, _ = f.WriteString(msg)
1046-
_ = f.Close()
1047-
}
1053+
}()
10481054
}
10491055
}
10501056
if textPartID != "" {

0 commit comments

Comments
 (0)