An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent kernel disconnect dialog when exiting on mobile https://github.com/siyuan-note/siyuan/issues/18976
Daniel07b6e9e2c2db
diff --git a/app/src/dialog/hostQuit.test.ts b/app/src/dialog/hostQuit.test.tsnew file mode 100644index 0000000000..e8b461eb8b--- /dev/null+++ b/app/src/dialog/hostQuit.test.ts@@ -0,0 +1,25 @@+import * as assert from "node:assert/strict";+import {describe, it} from "node:test";+import {createHostQuitGuard} from "./hostQuit";++describe("host quit guard", () => {+ it("runs the host quit action only once", () => {+ const guard = createHostQuitGuard();+ let calls = 0;++ assert.equal(guard.run(() => calls++), true);+ assert.equal(guard.run(() => calls++), false);+ assert.equal(calls, 1);+ assert.equal(guard.isStarted(), true);+ });++ it("allows retrying when the host quit action throws", () => {+ const guard = createHostQuitGuard();++ assert.throws(() => guard.run(() => {+ throw new Error("quit failed");+ }), /quit failed/);+ assert.equal(guard.isStarted(), false);+ assert.equal(guard.run(() => undefined), true);+ });+});diff --git a/app/src/dialog/hostQuit.ts b/app/src/dialog/hostQuit.tsnew file mode 100644index 0000000000..abb4bc9f13--- /dev/null+++ b/app/src/dialog/hostQuit.ts@@ -0,0 +1,20 @@+export const createHostQuitGuard = () => {+ let started = false;++ return {+ isStarted: () => started,+ run(action: () => void) {+ if (started) {+ return false;+ }+ started = true;+ try {+ action();+ return true;+ } catch (error) {+ started = false;+ throw error;+ }+ },+ };+};diff --git a/app/src/dialog/processSystem.ts b/app/src/dialog/processSystem.tsindex dff37fe7f4..8402bbb6e9 100644--- a/app/src/dialog/processSystem.ts+++ b/app/src/dialog/processSystem.ts@@ -19,6 +19,7 @@ import {hideAllElements} from "../protyle/ui/hideElements";import {saveScroll} from "../protyle/scroll/saveScroll";import {isInAndroid, isInHarmony, isInIOS, setStorageVal} from "../protyle/util/compatibility";import {emitToPlugins} from "../plugin/EventBusCore";+import {createHostQuitGuard} from "./hostQuit";export const processBacklinkIndexCommit = (data: {rootIDs?: string[],@@ -141,26 +142,32 @@ export const lockScreen = async () => {};-// forceQuit 用于内核已断连、无法走 /api/system/exit 的场景:绕过内核 HTTP,直接通知宿主(Electron 主进程 /-// 移动端原生容器)退出。浏览器/Docker 等纯 Web 环境无宿主可调,只能关闭当前页。+const hostQuitGuard = createHostQuitGuard();++export const isHostQuitStarted = hostQuitGuard.isStarted;++// forceQuit 绕过内核 HTTP,直接通知宿主(Electron 主进程 / 移动端原生容器)退出。浏览器/Docker 等纯 Web+// 环境无宿主可调,只能关闭当前页。export const forceQuit = () => {- /// #if !BROWSER- ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);- /// #else- if (isInAndroid()) {- window.JSAndroid.exit();- return;- }- if (isInIOS()) {- window.webkit.messageHandlers.exit.postMessage("");- return;- }- if (isInHarmony()) {- window.JSHarmony.exit();- return;- }- window.close();- /// #endif+ hostQuitGuard.run(() => {+ /// #if !BROWSER+ ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);+ /// #else+ if (isInAndroid()) {+ window.JSAndroid.exit();+ return;+ }+ if (isInIOS()) {+ window.webkit.messageHandlers.exit.postMessage("");+ return;+ }+ if (isInHarmony()) {+ window.JSHarmony.exit();+ return;+ }+ window.close();+ /// #endif+ });};const installNewVersion = (installPkgPath: string, setCurrentWorkspace: boolean) => {@@ -184,19 +191,7 @@ const installNewVersion = (installPkgPath: string, setCurrentWorkspace: boolean)force: true,setCurrentWorkspace,execInstallPkg: 1,- }, () => {- if (isInAndroid()) {- window.JSAndroid.exit();- return;- }- if (isInIOS()) {- window.webkit.messageHandlers.exit.postMessage("");- return;- }- if (isInHarmony()) {- window.JSHarmony.exit();- }- });+ }, forceQuit);/// #endif};@@ -217,24 +212,7 @@ export const exitSiYuan = async (setCurrentWorkspace = true) => {installNewVersion(response.data.installPkgPath, setCurrentWorkspace);return;}- fetchPost("/api/system/exit", {force: true, setCurrentWorkspace}, () => {- /// #if !BROWSER- ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);- /// #else- if (isInAndroid()) {- window.JSAndroid.exit();- return;- }- if (isInIOS()) {- window.webkit.messageHandlers.exit.postMessage("");- return;- }- if (isInHarmony()) {- window.JSHarmony.exit();- return;- }- /// #endif- });+ fetchPost("/api/system/exit", {force: true, setCurrentWorkspace}, forceQuit);});}} else if (response.code === 2) { // 提示新安装包@@ -253,30 +231,10 @@ export const exitSiYuan = async (setCurrentWorkspace = true) => {force: true,setCurrentWorkspace,execInstallPkg: 1 // 0:默认检查新版本,1:不返回安装包,2:返回安装包路径并退出- }, () => {- /// #if !BROWSER- ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);- /// #endif- });+ }, forceQuit);});} else { // 正常退出- /// #if !BROWSER- ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);- /// #else- if (isInAndroid()) {- window.JSAndroid.exit();- return;- }- if (isInIOS()) {- window.webkit.messageHandlers.exit.postMessage("");- return;- }-- if (isInHarmony()) {- window.JSHarmony.exit();- return;- }- /// #endif+ forceQuit();}});};diff --git a/app/src/mobile/util/kernelExit.test.ts b/app/src/mobile/util/kernelExit.test.tsnew file mode 100644index 0000000000..9fbfe8de1b--- /dev/null+++ b/app/src/mobile/util/kernelExit.test.ts@@ -0,0 +1,33 @@+import * as assert from "node:assert/strict";+import {describe, it} from "node:test";+import {handleMobileKernelExit} from "./kernelExit";++describe("mobile kernel exit", () => {+ it("quits the native mobile host", () => {+ let quitCalls = 0;+ let redirectCalls = 0;++ handleMobileKernelExit({+ inMobileApp: true,+ forceQuit: () => quitCalls++,+ redirectBrowser: () => redirectCalls++,+ });++ assert.equal(quitCalls, 1);+ assert.equal(redirectCalls, 0);+ });++ it("redirects a mobile browser", () => {+ let quitCalls = 0;+ let redirectCalls = 0;++ handleMobileKernelExit({+ inMobileApp: false,+ forceQuit: () => quitCalls++,+ redirectBrowser: () => redirectCalls++,+ });++ assert.equal(quitCalls, 0);+ assert.equal(redirectCalls, 1);+ });+});diff --git a/app/src/mobile/util/kernelExit.ts b/app/src/mobile/util/kernelExit.tsnew file mode 100644index 0000000000..165b917861--- /dev/null+++ b/app/src/mobile/util/kernelExit.ts@@ -0,0 +1,11 @@+export const handleMobileKernelExit = (options: {+ inMobileApp: boolean,+ forceQuit: () => void,+ redirectBrowser: () => void,+}) => {+ if (options.inMobileApp) {+ options.forceQuit();+ return;+ }+ options.redirectBrowser();+};diff --git a/app/src/mobile/util/onMessage.ts b/app/src/mobile/util/onMessage.tsindex 6cc585bbe6..063ce660e4 100644--- a/app/src/mobile/util/onMessage.ts+++ b/app/src/mobile/util/onMessage.ts@@ -1,5 +1,6 @@import {openMobileFileById} from "../editor";import {+ forceQuit,processSync,progressLoading,setDefRefCount,@@ -20,6 +21,8 @@ import {Constants} from "../../constants";import {MOBILE_SIDE_PANEL_CONFIG_CHANGE_EVENT} from "./mobileSidePanelConfig";import {appearanceConfigApi} from "../../config/tabs/appearanceRuntime";import {applyCloudUserState} from "../../config/tabs/accountUi";+import {isInMobileApp} from "../../protyle/util/compatibility";+import {handleMobileKernelExit} from "./kernelExit";let statusTimeout: number;const statusElement = document.querySelector("#status") as HTMLElement;@@ -161,6 +164,15 @@ export const onMessage = (app: App, data: IWebSocketData) => {case "openFileById":openMobileFileById(app, data.data.id);break;+ case "exit":+ handleMobileKernelExit({+ inMobileApp: isInMobileApp(),+ forceQuit,+ redirectBrowser: () => {+ window.location.href = "about:blank";+ },+ });+ break;case "filetreeSortChanged":window.siyuan.mobile.docks.file?.onFiletreeSortChanged(data.data);break;diff --git a/app/src/util/kernelFault.ts b/app/src/util/kernelFault.tsindex 6bcff410e4..7fe7a5cc34 100644--- a/app/src/util/kernelFault.ts+++ b/app/src/util/kernelFault.ts@@ -1,11 +1,11 @@import {Constants} from "../constants";import {Dialog} from "../dialog";-import {forceQuit} from "../dialog/processSystem";+import {forceQuit, isHostQuitStarted} from "../dialog/processSystem";import {isBrowser, isKernelInContainer, isMobile} from "./functions";import {isInIOS, isInMobileApp} from "../protyle/util/compatibility";export const kernelError = () => {- if (document.querySelector("#errorLog")) {+ if (isHostQuitStarted() || document.querySelector("#errorLog")) {return;}let title: string;diff --git a/kernel/api/system.go b/kernel/api/system.goindex 11579dc125..a513e94b9f 100644--- a/kernel/api/system.go+++ b/kernel/api/system.go@@ -1534,6 +1534,8 @@ func exit(c *gin.Context) {ret.Data = dataswitch exitCode {case 0:+ // Close 返回后同步和 defer 清理均已完成,此时再通知移动端宿主退出。+ util.BroadcastByType("main", "exit", 0, "", nil)case 1: // 同步执行失败ret.Msg = model.Conf.Language(96) + "<div class=\"fn__space\"></div><button class=\"b3-button b3-button--white\">" + model.Conf.Language(97) + "</button>"case 2: // 提示新安装包diff --git a/kernel/model/conf.go b/kernel/model/conf.goindex 52b9f18436..69ab51bfe8 100644--- a/kernel/model/conf.go+++ b/kernel/model/conf.go@@ -1080,7 +1080,6 @@ func Close(force, setCurrentWorkspace bool, execInstallPkg int) (exitCode int, i}}- util.BroadcastByType("main", "exit", 0, "", nil)util.UnlockWorkspace()time.Sleep(500 * time.Millisecond)