Skip to content

Commit a305ee6

Browse files
committed
fix(webview): log synchronous postMessage throws in postMessageToWebview
A disposed webview can throw synchronously from postMessage before any promise exists to attach the rejection handler to. Route that path through the dropped-message log so fire-and-forget callers never surface an unhandled rejection.
1 parent 2faae82 commit a305ee6

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,14 +1907,24 @@ export class ClineProvider
19071907
// path (e.g. the trailing postStateToWebview in handleModeSwitchUnlocked gates the next
19081908
// turn after a mode switch). Message ordering is enforced by the message seq, not the ack.
19091909
// Promise.resolve() normalizes non-promise returns (e.g. test doubles) before the catch.
1910-
void Promise.resolve(webview.postMessage(omitOriginalContentFromExtensionMessage(message))).catch((error) => {
1911-
// Swallow: postMessage rejects when the webview is disposed in flight.
1910+
// A disposed webview can also throw synchronously from postMessage itself, before
1911+
// any promise exists to attach the catch to: route that path through the same
1912+
// log so fire-and-forget callers never surface an unhandled rejection.
1913+
const logDropped = (error: unknown) => {
1914+
// Swallow: postMessage rejects (or throws) when the webview is disposed.
19121915
// Log the dropped message type so a wedged webview channel is diagnosable
19131916
// instead of silently losing state updates.
19141917
this.log(
19151918
`[postMessageToWebview] dropped message type=${message.type}: ${error instanceof Error ? error.message : String(error)}`,
19161919
)
1917-
})
1920+
}
1921+
try {
1922+
void Promise.resolve(webview.postMessage(omitOriginalContentFromExtensionMessage(message))).catch(
1923+
logDropped,
1924+
)
1925+
} catch (error) {
1926+
logDropped(error)
1927+
}
19181928
}
19191929

19201930
public requestWebviewThemeFixture(timeoutMs = 5_000): Promise<WebviewThemeFixture> {

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,23 @@ describe("ClineProvider", () => {
13221322
await expect(provider.postMessageToWebview(message)).resolves.toBeUndefined()
13231323
})
13241324

1325+
test("postMessageToWebview logs a synchronous postMessage throw", async () => {
1326+
await provider.resolveWebviewView(mockWebviewView)
1327+
const logSpy = vi.spyOn(provider, "log").mockImplementation(() => {})
1328+
// A disposed webview can throw synchronously from postMessage, before any
1329+
// promise exists to attach the rejection handler to: the call site must handle
1330+
// it so fire-and-forget callers never see an unhandled rejection.
1331+
mockPostMessage.mockImplementationOnce(() => {
1332+
throw new Error("Webview is disposed")
1333+
})
1334+
const message: ExtensionMessage = { type: "action", action: "chatButtonClicked" }
1335+
await expect(provider.postMessageToWebview(message)).resolves.toBeUndefined()
1336+
expect(logSpy).toHaveBeenCalledWith(
1337+
expect.stringContaining("[postMessageToWebview] dropped message type=action"),
1338+
)
1339+
logSpy.mockRestore()
1340+
})
1341+
13251342
test("postMessageToWebview does not await the webview ack", async () => {
13261343
await provider.resolveWebviewView(mockWebviewView)
13271344

0 commit comments

Comments
 (0)