From de4ef6f7bc148addb8c67ba4f27b326170d12beb Mon Sep 17 00:00:00 2001 From: azamkassim Date: Sun, 30 Aug 2026 09:42:56 +0800 Subject: [PATCH 1/4] fix(webview): retain instance when destroy fails --- src/lib/webview.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/webview.js b/src/lib/webview.js index 26a3bb7c3..4c12ba2ec 100644 --- a/src/lib/webview.js +++ b/src/lib/webview.js @@ -126,8 +126,8 @@ class WebView { async destroy() { this._checkDestroyed(); - this._destroyed = true; await nativeBridge.destroy(this.id); + this._destroyed = true; instances.delete(this.id); this._messageCallbacks = []; this._eventCallbacks = []; From 853da176952106ad5e34891c1be4ed8118f7ad00 Mon Sep 17 00:00:00 2001 From: azamkassim Date: Sun, 30 Aug 2026 09:43:08 +0800 Subject: [PATCH 2/4] test(webview): cover destroy failure lifecycle --- tests/unit/webview.test.js | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/unit/webview.test.js diff --git a/tests/unit/webview.test.js b/tests/unit/webview.test.js new file mode 100644 index 000000000..a92d85325 --- /dev/null +++ b/tests/unit/webview.test.js @@ -0,0 +1,47 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const { nativeBridge } = vi.hoisted(() => ({ + nativeBridge: { + setMessageCallback: vi.fn(), + create: vi.fn(), + evaluate: vi.fn(), + destroy: vi.fn(), + }, +})); + +vi.mock("../../src/plugins/webview/www/webview", () => ({ + default: nativeBridge, +})); + +import webviewAPI from "lib/webview"; + +describe("WebView lifecycle", () => { + beforeEach(() => { + vi.clearAllMocks(); + nativeBridge.create.mockResolvedValue("webview-1"); + nativeBridge.evaluate.mockResolvedValue("result"); + nativeBridge.destroy.mockResolvedValue(undefined); + }); + + it("keeps the instance usable when native destruction fails", async () => { + const webview = await webviewAPI.create(); + nativeBridge.destroy.mockRejectedValueOnce(new Error("native failure")); + + await expect(webview.destroy()).rejects.toThrow("native failure"); + await expect(webview.evaluate("1 + 1")).resolves.toBe("result"); + expect(nativeBridge.evaluate).toHaveBeenCalledWith("webview-1", "1 + 1"); + + await expect(webview.destroy()).resolves.toBeUndefined(); + expect(nativeBridge.destroy).toHaveBeenCalledTimes(2); + }); + + it("marks the instance destroyed only after native destruction succeeds", async () => { + const webview = await webviewAPI.create(); + + await webview.destroy(); + + await expect(webview.evaluate("1 + 1")).rejects.toThrow( + "WebView has been destroyed", + ); + }); +}); From 60ebb16d95503b03d5751b41fe93a2c8ba6c8d22 Mon Sep 17 00:00:00 2001 From: azamkassim Date: Sun, 30 Aug 2026 09:51:59 +0800 Subject: [PATCH 3/4] fix(webview): serialize concurrent destruction --- src/lib/webview.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/lib/webview.js b/src/lib/webview.js index 4c12ba2ec..3df669e68 100644 --- a/src/lib/webview.js +++ b/src/lib/webview.js @@ -59,6 +59,7 @@ class WebView { this._messageCallbacks = []; this._eventCallbacks = []; this._destroyed = false; + this._destroyPromise = null; instances.set(id, this); } @@ -126,11 +127,21 @@ class WebView { async destroy() { this._checkDestroyed(); - await nativeBridge.destroy(this.id); - this._destroyed = true; - instances.delete(this.id); - this._messageCallbacks = []; - this._eventCallbacks = []; + if (!this._destroyPromise) { + this._destroyPromise = (async () => { + await nativeBridge.destroy(this.id); + this._destroyed = true; + instances.delete(this.id); + this._messageCallbacks = []; + this._eventCallbacks = []; + })(); + } + + try { + await this._destroyPromise; + } finally { + this._destroyPromise = null; + } } _checkDestroyed() { From c299ffcb44e0f51a0943988fc790e214818d5190 Mon Sep 17 00:00:00 2001 From: azamkassim Date: Sun, 30 Aug 2026 09:52:09 +0800 Subject: [PATCH 4/4] test(webview): cover concurrent destruction --- tests/unit/webview.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/unit/webview.test.js b/tests/unit/webview.test.js index a92d85325..2d08ea3a8 100644 --- a/tests/unit/webview.test.js +++ b/tests/unit/webview.test.js @@ -44,4 +44,28 @@ describe("WebView lifecycle", () => { "WebView has been destroyed", ); }); + + it("shares native destruction between concurrent callers", async () => { + let resolveDestroy; + nativeBridge.destroy.mockImplementationOnce( + () => + new Promise((resolve) => { + resolveDestroy = resolve; + }), + ); + const webview = await webviewAPI.create(); + + const firstDestroy = webview.destroy(); + const secondDestroy = webview.destroy(); + + expect(nativeBridge.destroy).toHaveBeenCalledTimes(1); + resolveDestroy(); + await expect(Promise.all([firstDestroy, secondDestroy])).resolves.toEqual([ + undefined, + undefined, + ]); + await expect(webview.evaluate("1 + 1")).rejects.toThrow( + "WebView has been destroyed", + ); + }); });