Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve committed transaction state after panic https://github.com/siyuan-note/siyuan/issues/18278

Danielec23a2d83bfb
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 6505cb3075..1b56108477 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -207,11 +207,12 @@ func performTx(tx *Transaction) (ret *TxErr) {
if e := recover(); nil != e {
msg := fmt.Sprintf("PANIC RECOVERED: %v\n\t%s\n", e, logging.ShortStack())
logging.LogError(msg)
- ret = &TxErr{code: TxErrCodePushMsg, msg: fmt.Sprintf("transaction panic: %v", e)}
- if 1 == tx.state.Load() {
+ state := tx.state.Load()
+ if 1 == state {
tx.rollback()
}
+ ret = txErrFromPanic(state, e)
}
}()
@@ -391,6 +392,13 @@ func performTx(tx *Transaction) (ret *TxErr) {
return
}
+func txErrFromPanic(state int32, recovered any) *TxErr {
+ if 2 == state {
+ return nil
+ }
+ return &TxErr{code: TxErrCodePushMsg, msg: fmt.Sprintf("transaction panic: %v", recovered)}
+}
+
func (tx *Transaction) processLargeDelete() bool {
opSize := len(tx.DoOperations)
if 32 > opSize {
@@ -1503,8 +1511,9 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
id := operation.ID
updateData, ok := operation.Data.(string)
if !ok || "" == updateData {
- logging.LogErrorf("update data is nil")
- return &TxErr{code: TxErrCodeBlockNotFound, id: id}
+ msg := "update data is invalid"
+ logging.LogError(msg)
+ return &TxErr{code: TxErrCodePushMsg, msg: msg, id: id}
}
tree, err := tx.loadTree(id)
@@ -1515,8 +1524,9 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
data := strings.ReplaceAll(updateData, editor.FrontEndCaret, "")
if "" == data {
- logging.LogErrorf("update data is nil")
- return &TxErr{code: TxErrCodeBlockNotFound, id: id}
+ msg := "update data is invalid"
+ logging.LogError(msg)
+ return &TxErr{code: TxErrCodePushMsg, msg: msg, id: id}
}
subTree := tx.luteEngine.BlockDOM2Tree(data)
diff --git a/kernel/model/transaction_test.go b/kernel/model/transaction_test.go
index 26beb10057..dba0789c8b 100644
--- a/kernel/model/transaction_test.go
+++ b/kernel/model/transaction_test.go
@@ -26,5 +26,17 @@ func TestDoUpdateRejectsInvalidData(t *testing.T) {
if nil == err {
t.Fatalf("expected invalid update data [%v] to be rejected", data)
}
+ if TxErrCodePushMsg != err.Code() {
+ t.Fatalf("expected invalid update data [%v] to return code [%d], got [%d]", data, TxErrCodePushMsg, err.Code())
+ }
+ }
+}
+
+func TestTxErrFromPanic(t *testing.T) {
+ if err := txErrFromPanic(1, "test"); nil == err {
+ t.Fatal("expected an active transaction panic to return an error")
+ }
+ if err := txErrFromPanic(2, "test"); nil != err {
+ t.Fatal("expected a committed transaction panic to preserve the committed result")
}
}