From 1f8664d7f82cd27b80a176648c9a1a1132daa24f Mon Sep 17 00:00:00 2001
From: Lajos Koszti <ajnasz@ajnasz.hu>
Date: Fri, 3 Feb 2023 09:14:30 +0100
Subject: [PATCH] fix transaction already commited error

I encountered with the following error:

> 2023/02/05 07:33:00 /app/scanner/face_detection/face_detector.go:92 sql: transaction has already been committed or rolled back
> [0.042ms] [rows:0] SELECT * FROM `media` WHERE `media`.`id` = 823 ORDER BY `media`.`id` LIMIT 1
> 2023/02/05 07:33:00 ERROR: Error detecting faces in image (/photos/Borzsony2017/DSC_0028.NEF): sql: transaction has already been committed or rolled back

It turned out it comes from the api/routes/photos.go

I found a very similar code in album_scanner.go.
The difference I saw was that while in the single photo request the
transaction passed to the `scanner_tasks.Tasks.BeforeProcessMedia` call,
in the album_scann.go the transaction created after this call and
created from the context which returned by `BeforeProcessMedia`.

Another difference was that in the `ProcessSingleMedia` call the
`AfterProcessMedia` call was called with the same - db transaction -
context, in the album_scanner it was called outside of the transaction.

I changed the logic by merging the two behavior:
Create the transaction from the context of `BeforeProcessMedia` and also
use the transaction context in the `AfterProcessMedia`.
After the change the error disappeared.

So to have it in a common place I extracted that logic into a function
and use for both the single photo request and in the album scanner.

I did not go more deeper to find out what's going on with the context
under the hood.
---
 api/routes/photos.go         | 23 ++++++++-------------
 api/routes/videos.go         | 23 ++++++++-------------
 api/scanner/media_scan.go    | 40 ++++++++++++++++++++++++++++++++++++
 api/scanner/scanner_album.go | 30 ++-------------------------
 api/scanner/scanner_media.go | 21 ++-----------------
 5 files changed, 61 insertions(+), 76 deletions(-)
 create mode 100644 api/scanner/media_scan.go

diff --git a/api/routes/photos.go b/api/routes/photos.go
index b14a9a7..b1b91e1 100644
--- a/api/routes/photos.go
+++ b/api/routes/photos.go
@@ -45,21 +45,16 @@ func RegisterPhotoRoutes(db *gorm.DB, router *mux.Router) {
 		}
 
 		if _, err := os.Stat(cachedPath); os.IsNotExist((err)) {
-			err := db.Transaction(func(tx *gorm.DB) error {
-				if err = scanner.ProcessSingleMedia(tx, media); err != nil {
-					log.Printf("ERROR: processing image not found in cache (%s): %s\n", cachedPath, err)
-					return err
-				}
+			// err := db.Transaction(func(tx *gorm.DB) error {
+			if err = scanner.ProcessSingleMedia(db, media); err != nil {
+				log.Printf("ERROR: processing image not found in cache (%s): %s\n", cachedPath, err)
+				w.WriteHeader(http.StatusInternalServerError)
+				w.Write([]byte("internal server error"))
+				return
+			}
 
-				if _, err = os.Stat(cachedPath); err != nil {
-					log.Printf("ERROR: after reprocessing image not found in cache (%s): %s\n", cachedPath, err)
-					return err
-				}
-
-				return nil
-			})
-
-			if err != nil {
+			if _, err = os.Stat(cachedPath); err != nil {
+				log.Printf("ERROR: after reprocessing image not found in cache (%s): %s\n", cachedPath, err)
 				w.WriteHeader(http.StatusInternalServerError)
 				w.Write([]byte("internal server error"))
 				return
diff --git a/api/routes/videos.go b/api/routes/videos.go
index dd1d502..2717eee 100644
--- a/api/routes/videos.go
+++ b/api/routes/videos.go
@@ -51,22 +51,15 @@ func RegisterVideoRoutes(db *gorm.DB, router *mux.Router) {
 
 		if _, err := os.Stat(cachedPath); err != nil {
 			if os.IsNotExist(err) {
-				err := db.Transaction(func(tx *gorm.DB) error {
-					if err := scanner.ProcessSingleMedia(tx, media); err != nil {
-						log.Printf("ERROR: processing video not found in cache: %s\n", err)
-						return err
-					}
+				if err := scanner.ProcessSingleMedia(db, media); err != nil {
+					log.Printf("ERROR: processing video not found in cache: %s\n", err)
+					w.WriteHeader(http.StatusInternalServerError)
+					w.Write([]byte("internal server error"))
+					return
+				}
 
-					if _, err := os.Stat(cachedPath); err != nil {
-						log.Printf("ERROR: after reprocessing video not found in cache: %s\n", err)
-						return err
-					}
-
-					return nil
-				})
-
-				if err != nil {
-					log.Printf("ERROR: %s\n", err)
+				if _, err := os.Stat(cachedPath); err != nil {
+					log.Printf("ERROR: after reprocessing video not found in cache: %s\n", err)
 					w.WriteHeader(http.StatusInternalServerError)
 					w.Write([]byte("internal server error"))
 					return
diff --git a/api/scanner/media_scan.go b/api/scanner/media_scan.go
new file mode 100644
index 0000000..2fccb62
--- /dev/null
+++ b/api/scanner/media_scan.go
@@ -0,0 +1,40 @@
+package scanner
+
+import (
+	"github.com/photoview/photoview/api/graphql/models"
+	"github.com/photoview/photoview/api/scanner/media_encoding"
+	"github.com/photoview/photoview/api/scanner/scanner_task"
+	"github.com/photoview/photoview/api/scanner/scanner_tasks"
+	"github.com/pkg/errors"
+)
+
+func scanMedia(ctx scanner_task.TaskContext, media *models.Media, mediaData *media_encoding.EncodeMediaData, mediaIndex int, mediaTotal int) error {
+	newCtx, err := scanner_tasks.Tasks.BeforeProcessMedia(ctx, mediaData)
+	if err != nil {
+		return errors.Wrapf(err, "before process media (%s)", media.Path)
+	}
+
+	mediaCachePath, err := media.CachePath()
+	if err != nil {
+		return errors.Wrapf(err, "cache directory error (%s)", media.Path)
+	}
+
+	transactionError := newCtx.DatabaseTransaction(func(ctx scanner_task.TaskContext) error {
+		updatedURLs, err := scanner_tasks.Tasks.ProcessMedia(newCtx, mediaData, mediaCachePath)
+		if err != nil {
+			return errors.Wrapf(err, "process media (%s)", media.Path)
+		}
+
+		if err = scanner_tasks.Tasks.AfterProcessMedia(newCtx, mediaData, updatedURLs, mediaIndex, mediaTotal); err != nil {
+			return errors.Wrap(err, "after process media")
+		}
+
+		return nil
+	})
+
+	if transactionError != nil {
+		return errors.Wrap(transactionError, "process media database transaction")
+	}
+
+	return nil
+}
diff --git a/api/scanner/scanner_album.go b/api/scanner/scanner_album.go
index f6abc42..53aec9b 100644
--- a/api/scanner/scanner_album.go
+++ b/api/scanner/scanner_album.go
@@ -86,7 +86,6 @@ func ValidRootPath(rootPath string) bool {
 }
 
 func ScanAlbum(ctx scanner_task.TaskContext) error {
-
 	newCtx, err := scanner_tasks.Tasks.BeforeScanAlbum(ctx)
 	if err != nil {
 		return errors.Wrapf(err, "before scan album (%s)", ctx.GetAlbum().Path)
@@ -101,35 +100,10 @@ func ScanAlbum(ctx scanner_task.TaskContext) error {
 
 	changedMedia := make([]*models.Media, 0)
 	for i, media := range albumMedia {
-		updatedURLs := []*models.MediaURL{}
-
 		mediaData := media_encoding.NewEncodeMediaData(media)
 
-		// define new ctx for scope of for-loop
-		ctx, err := scanner_tasks.Tasks.BeforeProcessMedia(ctx, &mediaData)
-		if err != nil {
-			return err
-		}
-
-		transactionError := ctx.DatabaseTransaction(func(ctx scanner_task.TaskContext) error {
-			updatedURLs, err = processMedia(ctx, &mediaData)
-			if err != nil {
-				return errors.Wrapf(err, "process media (%s)", media.Path)
-			}
-
-			if len(updatedURLs) > 0 {
-				changedMedia = append(changedMedia, media)
-			}
-
-			return nil
-		})
-
-		if transactionError != nil {
-			return errors.Wrap(err, "process media database transaction")
-		}
-
-		if err = scanner_tasks.Tasks.AfterProcessMedia(ctx, &mediaData, updatedURLs, i, len(albumMedia)); err != nil {
-			return errors.Wrap(err, "after process media")
+		if err := scanMedia(ctx, media, &mediaData, i, len(albumMedia)); err != nil {
+			return errors.Wrap(err, "album scan")
 		}
 	}
 
diff --git a/api/scanner/scanner_media.go b/api/scanner/scanner_media.go
index eaefb02..5abb45c 100644
--- a/api/scanner/scanner_media.go
+++ b/api/scanner/scanner_media.go
@@ -10,7 +10,6 @@ import (
 	"github.com/photoview/photoview/api/scanner/media_encoding"
 	"github.com/photoview/photoview/api/scanner/scanner_cache"
 	"github.com/photoview/photoview/api/scanner/scanner_task"
-	"github.com/photoview/photoview/api/scanner/scanner_tasks"
 	"github.com/pkg/errors"
 	"gorm.io/gorm"
 )
@@ -82,24 +81,8 @@ func ProcessSingleMedia(db *gorm.DB, media *models.Media) error {
 	media_data := media_encoding.NewEncodeMediaData(media)
 
 	task_context := scanner_task.NewTaskContext(context.Background(), db, &album, album_cache)
-	new_ctx, err := scanner_tasks.Tasks.BeforeProcessMedia(task_context, &media_data)
-	if err != nil {
-		return err
-	}
-
-	mediaCachePath, err := media.CachePath()
-	if err != nil {
-		return err
-	}
-
-	updated_urls, err := scanner_tasks.Tasks.ProcessMedia(new_ctx, &media_data, mediaCachePath)
-	if err != nil {
-		return err
-	}
-
-	err = scanner_tasks.Tasks.AfterProcessMedia(new_ctx, &media_data, updated_urls, 0, 1)
-	if err != nil {
-		return err
+	if err := scanMedia(task_context, media, &media_data, 0, 1); err != nil {
+		return errors.Wrap(err, "single media scan")
 	}
 
 	return nil
