Skip to content

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

:bug: 退出应用时数据库未完全写入 https://github.com/siyuan-note/siyuan/issues/6318

Liang Dingdb04bfa572a8
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 92b98711fb..19d47e9a60 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -353,9 +353,9 @@ var exitLock = sync.Mutex{}
// force:是否不执行同步过程而直接退出
// execInstallPkg:是否执行新版本安装包
//
-// 0:默认按照设置项 System.DownloadInstallPkg 检查并推送提示
-// 1:不执行新版本安装
-// 2:执行新版本安装
+// 0:默认按照设置项 System.DownloadInstallPkg 检查并推送提示
+// 1:不执行新版本安装
+// 2:执行新版本安装
func Close(force bool, execInstallPkg int) (exitCode int) {
exitLock.Lock()
defer exitLock.Unlock()
@@ -363,7 +363,16 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
logging.LogInfof("exiting kernel [force=%v, execInstallPkg=%d]", force, execInstallPkg)
util.PushMsg(Conf.Language(95), 10000*60)
- WaitForWritingFiles()
+
+ wg := sync.WaitGroup{}
+ go func() {
+ wg.Add(1)
+ time.Sleep(util.FrontendQueueInterval)
+ WaitForWritingFiles()
+ time.Sleep(50 * time.Millisecond)
+ sql.WaitForWritingDatabase()
+ wg.Done()
+ }()
if !force {
SyncData(false, true, false)
if 0 != ExitSyncSucc {
@@ -371,6 +380,7 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
return
}
}
+ wg.Wait()
//util.UIProcessIDs.Range(func(key, _ interface{}) bool {
// pid := key.(string)
diff --git a/kernel/model/outline.go b/kernel/model/outline.go
index 178ac21cc2..f2aa341d89 100644
--- a/kernel/model/outline.go
+++ b/kernel/model/outline.go
@@ -22,10 +22,11 @@ import (
"github.com/88250/lute/ast"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
func Outline(rootID string) (ret []*Path, err error) {
- time.Sleep(512 * time.Millisecond /* 前端队列轮询间隔 */)
+ time.Sleep(util.FrontendQueueInterval)
WaitForWritingFiles()
ret = []*Path{}
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 3b422014fb..760d119a0b 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -45,7 +45,7 @@ type EmbedBlock struct {
}
func SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {
- time.Sleep(512 * time.Millisecond /* 前端队列轮询间隔 */)
+ time.Sleep(util.FrontendQueueInterval)
WaitForWritingFiles()
return searchEmbedBlock(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb)
}
diff --git a/kernel/sql/queue.go b/kernel/sql/queue.go
index ee1fadbd01..afc88d4674 100644
--- a/kernel/sql/queue.go
+++ b/kernel/sql/queue.go
@@ -32,10 +32,6 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-const (
- upsertTreesFlushDelay = 3000
-)
-
var (
operationQueue []*treeQueueOperation
upsertTreeQueueLock = sync.Mutex{}
@@ -57,7 +53,7 @@ type treeQueueOperation struct {
func AutoFlushTreeQueue() {
for {
flushTreeQueue()
- time.Sleep(time.Duration(upsertTreesFlushDelay) * time.Millisecond)
+ time.Sleep(util.SQLFlushInterval)
}
}
@@ -78,7 +74,7 @@ func WaitForWritingDatabase() {
}
func isWritingDatabase() bool {
- time.Sleep(time.Duration(upsertTreesFlushDelay+50) * time.Millisecond)
+ time.Sleep(util.SQLFlushInterval + 50*time.Millisecond)
if 0 < len(operationQueue) || util.IsMutexLocked(&txLock) {
return true
}
diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go
index 5445c3aa26..93df3663c7 100644
--- a/kernel/util/runtime.go
+++ b/kernel/util/runtime.go
@@ -89,3 +89,11 @@ func SetNetworkProxy(proxyURL string) {
logging.LogInfof("use network proxy [%s]", proxyURL)
}
}
+
+const (
+ // FrontendQueueInterval 为前端请求队列轮询间隔。
+ FrontendQueueInterval = 512 * time.Millisecond
+
+ // SQLFlushInterval 为数据库事务队列写入间隔。
+ SQLFlushInterval = 3000 * time.Millisecond
+)