From 7aad96b1991a981245cc119bce189de327ea36ce Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 4 Mar 2026 16:52:35 +0100 Subject: [PATCH] fix: close source file handle when duplicating attachments Save the source file handle before calling NewAttachment (which overwrites attachment.File) and use defer to ensure it gets closed. This prevents file descriptor leaks on both success and error paths. --- pkg/models/task_duplicate.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/models/task_duplicate.go b/pkg/models/task_duplicate.go index a3e29e67a..a2b1d8a8d 100644 --- a/pkg/models/task_duplicate.go +++ b/pkg/models/task_duplicate.go @@ -119,7 +119,7 @@ func (td *TaskDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) { attachment.File = &files.File{ID: attachment.FileID} if err := attachment.File.LoadFileMetaByID(); err != nil { if files.IsErrFileDoesNotExist(err) { - continue + continue } return err } @@ -127,13 +127,12 @@ func (td *TaskDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) { return err } - err := attachment.NewAttachment(s, attachment.File.File, attachment.File.Name, attachment.File.Size, doer) + sourceFile := attachment.File.File + defer sourceFile.Close() + err := attachment.NewAttachment(s, sourceFile, attachment.File.Name, attachment.File.Size, doer) if err != nil { return err } - if attachment.File.File != nil { - _ = attachment.File.File.Close() - } } // Create "copied from/to" relation