Skip to content

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

:bug: Serialize document restoration with undo selection https://github.com/siyuan-note/siyuan/issues/20934

Daniele7880375ea27
diff --git a/kernel/model/document_history_fts5_test.go b/kernel/model/document_history_fts5_test.go
index 01d081387a..69578c04f6 100644
--- a/kernel/model/document_history_fts5_test.go
+++ b/kernel/model/document_history_fts5_test.go
@@ -9,7 +9,9 @@ import (
"os"
"path/filepath"
"strings"
+ "sync"
"testing"
+ "time"
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/cache"
@@ -74,7 +76,55 @@ func TestRestoreDocumentHistoryInvalidatesUndoRedoOnlyAfterWrite(t *testing.T) {
if relErr != nil {
t.Fatal(relErr)
}
- err = RollbackDocHistory(relative)
+ if fail {
+ err = RollbackDocHistory(relative)
+ } else {
+ provider := filesys.DEKProvider
+ defer func() { filesys.DEKProvider = provider }()
+ started, allowWrite := make(chan struct{}), make(chan struct{})
+ var once, release sync.Once
+ defer release.Do(func() { close(allowWrite) })
+ filesys.DEKProvider = func(id string) ([]byte, error) {
+ once.Do(func() {
+ close(started)
+ <-allowWrite
+ })
+ return provider(id)
+ }
+ restored := make(chan error, 1)
+ go func() { restored <- RollbackDocHistory(relative) }()
+ select {
+ case <-started:
+ case <-time.After(5 * time.Second):
+ t.Fatal("restore did not reach its write boundary")
+ }
+ history := make(chan bool, 1)
+ go func() {
+ history <- WithTransactionHistory(func(_ func(*Transaction) error) bool {
+ undo, redo, _ := GlobalUndoLog.State(fixture.sourceID)
+ return undo || redo
+ })
+ }()
+ select {
+ case <-history:
+ t.Fatal("history selection entered before restoration finished")
+ case <-time.After(20 * time.Millisecond):
+ }
+ release.Do(func() { close(allowWrite) })
+ select {
+ case err = <-restored:
+ case <-time.After(5 * time.Second):
+ t.Fatal("restore did not finish")
+ }
+ select {
+ case stale := <-history:
+ if stale {
+ t.Fatal("history selection retained pre-restoration operations")
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("history selection did not resume after restore")
+ }
+ }
if fail {
if err == nil || strings.Contains(err.Error(), "history path") {
t.Fatal("expected restore write failure")
diff --git a/kernel/model/history.go b/kernel/model/history.go
index 71566c79e7..875f0491cb 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -320,6 +320,11 @@ func RollbackDocHistory(historyPath string) (err error) {
return
}
boxID = box.ID
+ // Serialize replacement and history invalidation with edit commits and undo/redo selection.
+ // Rollback notebook creation/mounting above may itself wait for transactions.
+ FlushTxQueue()
+ flushLock.Lock()
+ defer flushLock.Unlock()
srcPath := historyPath
var destPath, parentHPath string