Skip to content

Commit 5383cd9

Browse files
committed
fix(extension): dispatch webview postMessage without awaiting the renderer ack
postMessageToWebview awaited the webview postMessage promise, which VS Code only settles once the webview page acknowledges the message. When the page is remounted or reloaded, or the view is disposed while the post is in flight, that promise is orphaned forever and every caller awaiting it wedges on the task critical path. This wedged the tab task in the e2e view-state test: switch_mode awaited handleModeSwitch, whose trailing postStateToWebview was blocked on the orphaned ack during a webview remount, so the task's next turn never started and the 30s waitUntilCompleted timed out. Dispatch the post without awaiting the ack (with a rejection catch). Message ordering is enforced by the message seq, not by the ack. Add a unit regression test asserting postMessageToWebview returns without waiting for the renderer ack.
1 parent 02b5ea8 commit 5383cd9

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,11 +1733,20 @@ export class ClineProvider
17331733
return
17341734
}
17351735

1736-
try {
1737-
await this.view?.webview.postMessage(message)
1738-
} catch {
1739-
// View disposed, drop message silently
1736+
const webview = this.view?.webview
1737+
if (!webview) {
1738+
return
17401739
}
1740+
1741+
// Dispatch without awaiting the renderer ack: VS Code settles postMessage only when the
1742+
// webview page acknowledges the message, and a page reload or view dispose in flight
1743+
// orphans that promise forever. Awaiting it could wedge every caller on the task critical
1744+
// path (e.g. the trailing postStateToWebview in handleModeSwitchUnlocked gates the next
1745+
// turn after a mode switch). Message ordering is enforced by the message seq, not the ack.
1746+
// Promise.resolve() normalizes non-promise returns (e.g. test doubles) before the catch.
1747+
void Promise.resolve(webview.postMessage(message)).catch(() => {
1748+
// Swallow: postMessage rejects when the webview is disposed in flight.
1749+
})
17411750
}
17421751

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

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,26 @@ describe("ClineProvider", () => {
771771
await expect(provider.postMessageToWebview(message)).resolves.toBeUndefined()
772772
})
773773

774+
test("postMessageToWebview does not await the webview ack", async () => {
775+
await provider.resolveWebviewView(mockWebviewView)
776+
777+
let releaseAck!: () => void
778+
const ack = new Promise<void>((resolve) => {
779+
releaseAck = resolve
780+
})
781+
mockPostMessage.mockImplementationOnce(() => ack)
782+
783+
const message: ExtensionMessage = { type: "action", action: "chatButtonClicked" }
784+
785+
// The caller must not wait for the renderer ack: a webview page remounted or disposed
786+
// while the post is in flight never acknowledges it, and awaiting that promise would
787+
// wedge every caller on the task critical path.
788+
await provider.postMessageToWebview(message)
789+
790+
expect(mockPostMessage).toHaveBeenCalledWith(message)
791+
releaseAck()
792+
})
793+
774794
describe("theme fixture probes", () => {
775795
const fixture = {
776796
themeId: "Default Dark Modern",

0 commit comments

Comments
 (0)