CaIon 2 лет назад
Родитель
Сommit
febcadb42c
2 измененных файлов с 19 добавлено и 1 удалено
  1. 16 0
      common/go-channel.go
  2. 3 1
      model/log.go

+ 16 - 0
common/go-channel.go

@@ -1,5 +1,21 @@
 package common
 package common
 
 
+import (
+	"fmt"
+	"runtime/debug"
+)
+
+func SafeGoroutine(f func()) {
+	go func() {
+		defer func() {
+			if r := recover(); r != nil {
+				SysError(fmt.Sprintf("child goroutine panic occured: error: %v, stack: %s", r, string(debug.Stack())))
+			}
+		}()
+		f()
+	}()
+}
+
 func SafeSend(ch chan bool, value bool) (closed bool) {
 func SafeSend(ch chan bool, value bool) (closed bool) {
 	defer func() {
 	defer func() {
 		// Recover from panic if one occured. A panic would mean the channel was closed.
 		// Recover from panic if one occured. A panic would mean the channel was closed.

+ 3 - 1
model/log.go

@@ -80,7 +80,9 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
 		common.LogError(ctx, "failed to record log: "+err.Error())
 		common.LogError(ctx, "failed to record log: "+err.Error())
 	}
 	}
 	if common.DataExportEnabled {
 	if common.DataExportEnabled {
-		go LogQuotaData(userId, username, modelName, quota, common.GetTimestamp())
+		common.SafeGoroutine(func() {
+			LogQuotaData(userId, username, modelName, quota, common.GetTimestamp())
+		})
 	}
 	}
 }
 }