Skip to content

Commit ce3b00a

Browse files
fix(webview): run each provider release once and drop the dispose-time hydrate
disposeInternal duplicated the teardown releases that dispose()s finally already owns. Measured on 057e866, a single dispose() ran the diagnostics broker release, removeAllListeners and McpServerManager.unregisterProvider twice ({"broker":2,"listeners":2,"unregister":2}), and because dispose() returns early when _disposed is set, a second dispose() changed nothing - so the existing idempotency test could not see the doubling. The releases now live only in the finally, which is what the teardown bodys own comment already claimed. The unawaited `void this.loadViewState()` in the teardown body came from the merge resolution at 17890eb: neither parent carried it - one occurrence at :563 on f4, one at :523 on f3, zero on origin/main. Starting a state load against a provider that is already closing mutates view-local state mid-teardown with no live view left to deliver to, so it is gone, and the seam comment beside cleanupStep no longer claims a dispose-time load re-runs the constructors hydrate. Tests: the broker release, the listener removal and the MCP unregistration are asserted to happen exactly once across a double dispose, and dispose is asserted not to start a view-state load. Control with only the production change reverted: the count test fails on 2/2/2 and the load test fails on one call. With the fix ClineProvider.spec.ts is 459 passed, tsc and eslint clean.
1 parent 057e866 commit ce3b00a

2 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,12 +1599,13 @@ export class ClineProvider
15991599
// release steps, the first error is preserved, and it is reported once
16001600
// every step has run.
16011601
let cleanupError: unknown
1602-
void this.loadViewState()
16031602

16041603
const cleanupStep = async (label: string, fn: () => unknown | Promise<unknown>) => {
16051604
// #1562 seam: the constructor's `void this.loadViewState()` (construction-time hydrate)
16061605
// is the other #1562 consumer of the delivery signal; it has no caller to report to, so
1607-
// a false return there is intentionally dropped - the dispose-time load below re-runs it.
1606+
// a false return there is intentionally dropped. Disposal does not re-hydrate: there is
1607+
// no live view left to deliver to, and a load started here would mutate the view-local
1608+
// state while the teardown is already running.
16081609
try {
16091610
await fn()
16101611
} catch (error) {
@@ -1698,13 +1699,9 @@ export class ClineProvider
16981699
await cleanupStep("taskHistoryStore", () => this.taskHistoryStore.dispose())
16991700

17001701
this.log("Disposed all disposables")
1701-
ClineProvider.activeInstances.delete(this)
1702-
this.diagnosticsRequestBroker.dispose()
1703-
1704-
// Clean up any event listeners attached to this provider
1705-
this.removeAllListeners()
1706-
1707-
McpServerManager.unregisterProvider(this)
1702+
// The releases themselves live in dispose's finally, not here: the body used to duplicate
1703+
// all four after this log, so every dispose ran the broker release, the listener removal
1704+
// and the MCP unregistration twice.
17081705

17091706
if (cleanupError !== undefined) {
17101707
throw cleanupError

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { safeWriteJson } from "../../../utils/safeWriteJson"
3939
import { t } from "../../../i18n"
4040

4141
import { ClineProvider } from "../ClineProvider"
42+
import { McpServerManager } from "../../../services/mcp/McpServerManager"
4243
import { webviewMessageHandler } from "../webviewMessageHandler"
4344
import type { MdmService } from "../../../services/mdm/MdmService"
4445
import { Terminal } from "../../../integrations/terminal/Terminal"
@@ -3699,6 +3700,62 @@ describe("ClineProvider", () => {
36993700
// ...and disposal releases it.
37003701
expect(ClineProvider.getAllInstances()).not.toContain(provider)
37013702
})
3703+
// Measured before the fix: one dispose() ran each of these releases twice - the teardown
3704+
// body performed the broker release, the listener removal and the MCP unregistration after
3705+
// the caller's finally had already done them. The second dispose() was already a no-op, so
3706+
// the existing guard test could not see the doubling; only a count across the pair can.
3707+
it("releases the broker, the listeners and the MCP registration exactly once across a double dispose", async () => {
3708+
const provider = new ClineProvider(
3709+
mockContext,
3710+
mockOutputChannel,
3711+
"sidebar",
3712+
new ContextProxy(mockContext),
3713+
new WebviewFocusTracker(),
3714+
)
3715+
await new Promise((resolve) => setImmediate(resolve))
3716+
3717+
const broker = vi.spyOn(provider["diagnosticsRequestBroker"], "dispose")
3718+
const listeners = vi.spyOn(provider, "removeAllListeners")
3719+
const unregister = vi.spyOn(McpServerManager, "unregisterProvider")
3720+
3721+
await provider.dispose()
3722+
await provider.dispose()
3723+
3724+
expect(broker).toHaveBeenCalledTimes(1)
3725+
expect(listeners).toHaveBeenCalledTimes(1)
3726+
expect(unregister).toHaveBeenCalledTimes(1)
3727+
expect(ClineProvider.getAllInstances()).not.toContain(provider)
3728+
3729+
broker.mockRestore()
3730+
listeners.mockRestore()
3731+
unregister.mockRestore()
3732+
})
3733+
3734+
// The dispose-time hydrate was a merge artifact that neither parent carried: it started a
3735+
// state load against a provider that is already closing, with no live view to deliver to.
3736+
it("does not start a view-state load while disposing", async () => {
3737+
const provider = new ClineProvider(
3738+
mockContext,
3739+
mockOutputChannel,
3740+
"sidebar",
3741+
new ContextProxy(mockContext),
3742+
new WebviewFocusTracker(),
3743+
)
3744+
await new Promise((resolve) => setImmediate(resolve))
3745+
3746+
// loadViewState is private and private keys sit outside keyof, so the spy needs one
3747+
// assertion onto the method shape it actually has (AGENTS.md: last resort, commented).
3748+
const load = vi.spyOn(
3749+
ClineProvider.prototype as unknown as { loadViewState: () => Promise<boolean> },
3750+
"loadViewState",
3751+
)
3752+
3753+
await provider.dispose()
3754+
3755+
expect(load).not.toHaveBeenCalled()
3756+
3757+
load.mockRestore()
3758+
})
37023759

37033760
it("releases the provider and reports the failure when a cleanup step rejects", async () => {
37043761
// Force a cleanup step inside dispose() to reject. _disposed is set

0 commit comments

Comments
 (0)