File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 > {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments