Skip to content

Commit bb9c57e

Browse files
roomoteedelauna
authored andcommitted
fix(webview): dispose view listeners and ignore stale views when the webview is replaced
1 parent 3b825bf commit bb9c57e

8 files changed

Lines changed: 514 additions & 24 deletions

‎src/core/webview/ClineProvider.ts‎

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,15 @@ export class ClineProvider
837837
}
838838
}
839839

840+
/**
841+
* True when an in-flight resolve no longer belongs to the provider's current
842+
* view because the view was disposed or replaced (or the provider itself was
843+
* disposed) while the resolve was awaiting HTML or state hydration.
844+
*/
845+
private isStaleViewResolve(webviewView: vscode.WebviewView | vscode.WebviewPanel): boolean {
846+
return this._disposed || this.view !== webviewView
847+
}
848+
840849
/** Drain one task's memoized cleanup without preventing the remaining provider shutdown work. */
841850
private async drainTaskDisposal(task: Task): Promise<void> {
842851
try {
@@ -1030,6 +1039,12 @@ export class ClineProvider
10301039
}
10311040

10321041
async resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
1042+
// A different view replaces the current one: dispose its subscriptions so
1043+
// the replaced view can no longer dispatch messages or refresh provider state.
1044+
if (this.view !== webviewView) {
1045+
this.clearWebviewResources()
1046+
}
1047+
10331048
this.view = webviewView
10341049
const inTabMode = "onDidChangeViewState" in webviewView
10351050

@@ -1052,12 +1067,20 @@ export class ClineProvider
10521067
localResourceRoots: resourceRoots,
10531068
}
10541069

1055-
webviewView.webview.html =
1070+
const html =
10561071
this.contextProxy.extensionMode === vscode.ExtensionMode.Development &&
10571072
process.env.ROO_CODE_THEME_FIXTURE_PROBE !== "1"
10581073
? await this.getHMRHtmlContent(webviewView.webview)
10591074
: await this.getHtmlContent(webviewView.webview)
10601075

1076+
// The HTML load above can finish after the view was disposed or replaced.
1077+
// A stale resolve must not assign HTML to a view the provider no longer owns.
1078+
if (this.isStaleViewResolve(webviewView)) {
1079+
return
1080+
}
1081+
1082+
webviewView.webview.html = html
1083+
10611084
// Initialize out-of-scope variables that need to receive persistent
10621085
// global state values.
10631086
await this.getState().then(
@@ -1088,9 +1111,15 @@ export class ClineProvider
10881111
},
10891112
)
10901113

1114+
// State hydration can also finish after the view was disposed or replaced.
1115+
// A stale resolve must not install listeners on a view the provider no longer owns.
1116+
if (this.isStaleViewResolve(webviewView)) {
1117+
return
1118+
}
1119+
10911120
// Sets up an event listener to listen for messages passed from the webview view context
10921121
// and executes code based on the message that is received.
1093-
this.setWebviewMessageListener(webviewView.webview)
1122+
this.setWebviewMessageListener(webviewView)
10941123
this.webviewDisposables.push(this.webviewFocusTracker.init(this, webviewView))
10951124

10961125
// Listen for when the panel becomes visible.
@@ -1099,6 +1128,11 @@ export class ClineProvider
10991128
// WebviewView and WebviewPanel have all the same properties except
11001129
// for this visibility listener panel.
11011130
const viewStateDisposable = webviewView.onDidChangeViewState(() => {
1131+
// Ignore events from a view that is no longer the current view.
1132+
if (webviewView !== this.view) {
1133+
return
1134+
}
1135+
11021136
if (this.view?.visible) {
11031137
void this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
11041138
} else {
@@ -1110,6 +1144,11 @@ export class ClineProvider
11101144
} else if ("onDidChangeVisibility" in webviewView) {
11111145
// sidebar
11121146
const visibilityDisposable = webviewView.onDidChangeVisibility(() => {
1147+
// Ignore events from a view that is no longer the current view.
1148+
if (webviewView !== this.view) {
1149+
return
1150+
}
1151+
11131152
if (this.view?.visible) {
11141153
void this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
11151154
} else {
@@ -1124,20 +1163,34 @@ export class ClineProvider
11241163
// This happens when the user closes the view or when the view is closed programmatically
11251164
webviewView.onDidDispose(
11261165
async () => {
1166+
// A view that is no longer current must not clear the current
1167+
// view's resources or dispose the provider.
1168+
if (webviewView !== this.view) {
1169+
return
1170+
}
1171+
11271172
if (inTabMode) {
11281173
this.log("Disposing ClineProvider instance for tab view")
11291174
await this.dispose()
11301175
} else {
11311176
this.log("Clearing webview resources for sidebar view")
1177+
this.view = undefined
11321178
this.clearWebviewResources()
11331179
}
11341180
},
11351181
null,
1136-
this.disposables,
1182+
// View-scoped, like the other view subscriptions: disposed both when
1183+
// the view is disposed and when a different view replaces it.
1184+
this.webviewDisposables,
11371185
)
11381186

11391187
// Listen for when color changes
11401188
const configDisposable = vscode.workspace.onDidChangeConfiguration(async (e) => {
1189+
// Ignore configuration refreshes scoped to a view that is no longer current.
1190+
if (webviewView !== this.view) {
1191+
return
1192+
}
1193+
11411194
if (e && e.affectsConfiguration("workbench.colorTheme")) {
11421195
// Sends latest theme name to webview
11431196
await this.postMessageToWebview({ type: "theme", text: JSON.stringify(await getTheme()) })
@@ -1704,13 +1757,19 @@ export class ClineProvider
17041757
* Sets up an event listener to listen for messages passed from the webview context and
17051758
* executes code based on the message that is received.
17061759
*
1707-
* @param webview A reference to the extension webview
1760+
* @param webviewView The view whose webview messages are listened to
17081761
*/
1709-
private setWebviewMessageListener(webview: vscode.Webview) {
1710-
const onReceiveMessage = async (message: WebviewMessage) =>
1711-
webviewMessageHandler(this, message, this.marketplaceManager)
1762+
private setWebviewMessageListener(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
1763+
const onReceiveMessage = async (message: WebviewMessage) => {
1764+
// Ignore messages from a view that is no longer the current view.
1765+
if (webviewView !== this.view) {
1766+
return
1767+
}
1768+
1769+
await webviewMessageHandler(this, message, this.marketplaceManager)
1770+
}
17121771

1713-
const messageDisposable = webview.onDidReceiveMessage(onReceiveMessage)
1772+
const messageDisposable = webviewView.webview.onDidReceiveMessage(onReceiveMessage)
17141773
this.webviewDisposables.push(messageDisposable)
17151774
}
17161775

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ describe("ClineProvider - API Handler Rebuild Guard", () => {
224224
asWebviewUri: vi.fn(),
225225
},
226226
visible: true,
227-
onDidDispose: vi.fn().mockImplementation((callback) => {
228-
callback()
227+
onDidDispose: vi.fn().mockImplementation(() => {
229228
return { dispose: vi.fn() }
230229
}),
231230
onDidChangeVisibility: vi.fn().mockImplementation(() => {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ describe("ClineProvider - Lock API Config Across Modes", () => {
316316
cspSource: "vscode-webview://test-csp-source",
317317
},
318318
visible: true,
319-
onDidDispose: vi.fn().mockImplementation((callback) => {
320-
callback()
319+
onDidDispose: vi.fn().mockImplementation(() => {
321320
return { dispose: vi.fn() }
322321
}),
323322
onDidChangeVisibility: vi.fn().mockImplementation(() => {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ describe("ClineProvider", () => {
536536
cspSource: "vscode-webview://test-csp-source",
537537
},
538538
visible: true,
539-
onDidDispose: vi.fn().mockImplementation((callback) => {
540-
callback()
539+
onDidDispose: vi.fn().mockImplementation(() => {
541540
return { dispose: vi.fn() }
542541
}),
543542
onDidChangeVisibility: vi.fn().mockImplementation(() => {
@@ -4512,8 +4511,7 @@ describe("ClineProvider - Router Models", () => {
45124511
asWebviewUri: vi.fn(),
45134512
},
45144513
visible: true,
4515-
onDidDispose: vi.fn().mockImplementation((callback) => {
4516-
callback()
4514+
onDidDispose: vi.fn().mockImplementation(() => {
45174515
return { dispose: vi.fn() }
45184516
}),
45194517
onDidChangeVisibility: vi.fn().mockImplementation(() => {
@@ -4911,8 +4909,7 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
49114909
asWebviewUri: vi.fn(),
49124910
},
49134911
visible: true,
4914-
onDidDispose: vi.fn().mockImplementation((callback) => {
4915-
callback()
4912+
onDidDispose: vi.fn().mockImplementation(() => {
49164913
return { dispose: vi.fn() }
49174914
}),
49184915
onDidChangeVisibility: vi.fn().mockImplementation(() => {

0 commit comments

Comments
 (0)