Fix session cleanup on error paths

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-07-13 08:03:01 +08:00
parent cb8f587e30
commit 683645be8e
82 changed files with 164 additions and 132 deletions
+21 -2
View File
@@ -90,7 +90,7 @@ func (ss *Session) Finalize() {
if err := ss.UserEng.Commit(); err != nil {
log.Println(err.Error())
ss.closeAllSessions(false)
ss.respondError(http.StatusInternalServerError, honokautils.NewInternalErrorContent(err))
ss.respondError(http.StatusInternalServerError, honokautils.NewInternalErrorContent())
return
}
@@ -106,7 +106,7 @@ func (ss *Session) Abort(err error) {
msg := ss.formatAbortMessage(err)
log.Println(msg)
ss.abortWithContent(http.StatusInternalServerError, honokautils.NewErrorContent("SessionAbort", msg))
ss.abortWithContent(http.StatusInternalServerError, honokautils.NewInternalErrorContent())
}
func (ss *Session) AbortWithStatus(status int, content any) {
@@ -116,6 +116,25 @@ func (ss *Session) AbortWithStatus(status int, content any) {
ss.abortWithContent(status, content)
}
// FinalizeOrRollback commits successful requests and rolls back before a panic reaches recovery.
func (ss *Session) FinalizeOrRollback() {
defer func() {
if recovered := recover(); recovered != nil {
ss.Rollback()
panic(recovered)
}
}()
ss.Finalize()
}
func (ss *Session) Rollback() {
if ss.done {
return
}
ss.done = true
ss.closeAllSessions(true)
}
func (ss *Session) formatAbortMessage(err error) string {
skip := 2
if pc, _, _, ok := runtime.Caller(2); ok {