From 2a10b22c5c784c9aaf82618f78bb6e57ecb73a60 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 25 Feb 2026 09:42:22 +0100 Subject: [PATCH] fix: use session-aware file creation to avoid nested transactions files.Create() and files.CreateWithMime() internally create their own sessions and transactions. When called from within an existing transaction (now that db.NewSession() auto-begins), this creates nested transactions that deadlock on SQLite. Switch to files.CreateWithSession() and files.CreateWithMimeAndSession() to participate in the caller's existing transaction instead. --- pkg/models/project_duplicate.go | 2 +- pkg/modules/avatar/upload/upload.go | 2 +- pkg/modules/background/unsplash/unsplash.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/models/project_duplicate.go b/pkg/models/project_duplicate.go index 26985333a..651865fee 100644 --- a/pkg/models/project_duplicate.go +++ b/pkg/models/project_duplicate.go @@ -301,7 +301,7 @@ func duplicateProjectBackground(s *xorm.Session, pd *ProjectDuplicate, doer web. } defer f.File.Close() - file, err := files.Create(f.File, f.Name, f.Size, doer) + file, err := files.CreateWithSession(s, f.File, f.Name, f.Size, doer) if err != nil { return err } diff --git a/pkg/modules/avatar/upload/upload.go b/pkg/modules/avatar/upload/upload.go index 0375011e9..97f53d5bd 100644 --- a/pkg/modules/avatar/upload/upload.go +++ b/pkg/modules/avatar/upload/upload.go @@ -171,7 +171,7 @@ func StoreAvatarFile(s *xorm.Session, u *user.User, src io.Reader) (err error) { } // Save the file - f, err := files.CreateWithMime(bytes.NewReader(buf.Bytes()), "avatar.png", uint64(buf.Len()), u, "image/png") + f, err := files.CreateWithMimeAndSession(s, bytes.NewReader(buf.Bytes()), "avatar.png", uint64(buf.Len()), u, "image/png", true) if err != nil { return err } diff --git a/pkg/modules/background/unsplash/unsplash.go b/pkg/modules/background/unsplash/unsplash.go index 310e4874d..6207eb654 100644 --- a/pkg/modules/background/unsplash/unsplash.go +++ b/pkg/modules/background/unsplash/unsplash.go @@ -312,7 +312,7 @@ func (p *Provider) Set(s *xorm.Session, image *background.Image, project *models } // Save it as a file in vikunja - file, err := files.Create(tmpFile, "", uint64(written), auth) + file, err := files.CreateWithSession(s, tmpFile, "", uint64(written), auth) if err != nil { return }