Skip to content

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

:bug: macOS 端报错 `数据库被锁定` Fix https://github.com/siyuan-note/siyuan/issues/6492

Liang Ding51a0db582234
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 9c11f5e112..2cce8f2c0b 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -24,8 +24,10 @@ import (
"net/http/pprof"
"net/url"
"os"
+ "os/exec"
"path"
"path/filepath"
+ "strconv"
"strings"
"time"
@@ -34,6 +36,7 @@ import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
+ goPS "github.com/mitchellh/go-ps"
"github.com/mssola/user_agent"
"github.com/olahol/melody"
"github.com/siyuan-note/logging"
@@ -74,6 +77,11 @@ func Serve(fastMode bool) {
serveTemplates(ginServer)
api.ServeAPI(ginServer)
+ if !fastMode {
+ // 杀掉占用 6806 的已有内核进程
+ killByPort(util.FixedPort)
+ }
+
var host string
if model.Conf.System.NetworkServe || util.ContainerDocker == util.Container {
host = "0.0.0.0"
@@ -451,3 +459,91 @@ func corsMiddleware() gin.HandlerFunc {
c.Next()
}
}
+
+func killByPort(port string) {
+ if !isPortOpen(port) {
+ return
+ }
+
+ portJSON := filepath.Join(util.HomeDir, ".config", "siyuan", "port.json")
+ os.RemoveAll(portJSON)
+
+ pid := pidByPort(port)
+ if "" == pid {
+ return
+ }
+
+ pidInt, _ := strconv.Atoi(pid)
+ proc, _ := goPS.FindProcess(pidInt)
+ var name string
+ if nil != proc {
+ name = proc.Executable()
+ }
+ kill(pid)
+ logging.LogInfof("killed process [name=%s, pid=%s]", name, pid)
+}
+
+func isPortOpen(port string) bool {
+ timeout := time.Second
+ conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", port), timeout)
+ if nil != err {
+ return false
+ }
+ if nil != conn {
+ conn.Close()
+ return true
+ }
+ return false
+}
+
+func kill(pid string) {
+ var kill *exec.Cmd
+ if gulu.OS.IsWindows() {
+ kill = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
+ } else {
+ kill = exec.Command("kill", "-9", pid)
+ }
+ gulu.CmdAttr(kill)
+ kill.CombinedOutput()
+}
+
+func pidByPort(port string) (ret string) {
+ if gulu.OS.IsWindows() {
+ cmd := exec.Command("cmd", "/c", "netstat -ano | findstr "+port)
+ gulu.CmdAttr(cmd)
+ data, err := cmd.CombinedOutput()
+ if nil != err {
+ logging.LogErrorf("netstat failed: %s", err)
+ return
+ }
+ output := string(data)
+ lines := strings.Split(output, "\n")
+ for _, l := range lines {
+ if strings.Contains(l, "LISTENING") {
+ l = l[strings.Index(l, "LISTENING")+len("LISTENING"):]
+ l = strings.TrimSpace(l)
+ ret = l
+ return
+ }
+ }
+ return
+ }
+
+ cmd := exec.Command("lsof", "-Fp", "-i", ":"+port)
+ gulu.CmdAttr(cmd)
+ data, err := cmd.CombinedOutput()
+ if nil != err {
+ logging.LogErrorf("lsof failed: %s", err)
+ return
+ }
+ output := string(data)
+ lines := strings.Split(output, "\n")
+ for _, l := range lines {
+ if strings.HasPrefix(l, "p") {
+ l = l[1:]
+ ret = l
+ return
+ }
+ }
+ return
+}
diff --git a/kernel/util/working.go b/kernel/util/working.go
index 6b95693d75..97f0ec292f 100644
--- a/kernel/util/working.go
+++ b/kernel/util/working.go
@@ -32,7 +32,6 @@ import (
"github.com/88250/gulu"
figure "github.com/common-nighthawk/go-figure"
- goPS "github.com/mitchellh/go-ps"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
@@ -347,71 +346,6 @@ func initMime() {
mime.AddExtensionType(".pdf", "application/pdf")
}
-func KillByPort(port string) {
- if pid := PidByPort(port); "" != pid {
- pidInt, _ := strconv.Atoi(pid)
- proc, _ := goPS.FindProcess(pidInt)
- var name string
- if nil != proc {
- name = proc.Executable()
- }
- Kill(pid)
- logging.LogInfof("killed process [name=%s, pid=%s]", name, pid)
- }
-}
-
-func Kill(pid string) {
- var kill *exec.Cmd
- if gulu.OS.IsWindows() {
- kill = exec.Command("cmd", "/c", "TASKKILL /F /PID "+pid)
- } else {
- kill = exec.Command("kill", "-9", pid)
- }
- gulu.CmdAttr(kill)
- kill.CombinedOutput()
-}
-
-func PidByPort(port string) (ret string) {
- if gulu.OS.IsWindows() {
- cmd := exec.Command("cmd", "/c", "netstat -ano | findstr "+port)
- gulu.CmdAttr(cmd)
- data, err := cmd.CombinedOutput()
- if nil != err {
- logging.LogErrorf("netstat failed: %s", err)
- return
- }
- output := string(data)
- lines := strings.Split(output, "\n")
- for _, l := range lines {
- if strings.Contains(l, "LISTENING") {
- l = l[strings.Index(l, "LISTENING")+len("LISTENING"):]
- l = strings.TrimSpace(l)
- ret = l
- return
- }
- }
- return
- }
-
- cmd := exec.Command("lsof", "-Fp", "-i", ":"+port)
- gulu.CmdAttr(cmd)
- data, err := cmd.CombinedOutput()
- if nil != err {
- logging.LogErrorf("lsof failed: %s", err)
- return
- }
- output := string(data)
- lines := strings.Split(output, "\n")
- for _, l := range lines {
- if strings.HasPrefix(l, "p") {
- l = l[1:]
- ret = l
- return
- }
- }
- return
-}
-
func initPandoc() {
if ContainerStd != Container {
return