Skip to content

Commit 0219d1a

Browse files
author
easonLiangWorldedtech
committed
fix(webview): stop touching a provider that has begun disposal
The Lifecycle Resource Cleanup row at head 420c995: the cross-view profile mutation path could do work on a provider that was already being torn down. dispose() set _disposed on its first line but only unregistered the instance from activeInstances after every awaited teardown step - task eviction plus six guarded manager cleanups. For that whole window the provider was already _disposed and yet still enumerable through getAllInstances(), getVisibleInstance() and getInstanceForView(), so a sibling profile upsert or deletion could persist durable view state and post to a webview that is gone. 1. dispose() now unregisters the instance synchronously at the start of disposal, before its first await, and the stale comment about the unregistration running "below" is corrected. No teardown step looks this instance up in the registry, so moving it costs nothing and closes the window completely. 2. refreshViewLocalStateForUpdatedProfile and rePinViewLocalStateForDeletedProfile exclude instances that have begun disposal from the affected set, and re-check the flag before each sibling persistence, post, and compensating restore. The affected set is enumerated before any await, so a sibling can start disposing after the enumeration and before each of those operations. Skipping a disposing sibling is safe rather than lossy: viewId comes from the monotonic nextViewId counter, so that view's viewStates entry is orphaned the moment it begins disposal - no future view reads that key again, and prunePersistedViewStates() bounds the map. Five tests, each with a negative control reverted byte-for-byte: - dispose unregisters before the awaited cleanup finishes (gate held open on the last teardown step); moving the unregistration back turns exactly this red. - refresh reaches a live sibling but not one that has begun disposal; removing the enumeration filter together with the loop's first re-check turns exactly this red. The filter alone is an equivalent mutant - the loop's first re-check tests the same predicate with no await in between, so it subsumes the filter. - refresh does not post to a sibling that starts disposing mid-flight; removing the post guard turns exactly this red. - the deletion re-pins a live sibling but not a disposing one; same equivalence note as above for the filter. - the re-pin skips both the post and the compensating restore for a sibling that starts disposing mid-flight; each of those two guards on its own turns this red. Verification: ClineProvider.spec.ts 303 passed, core/webview 756 passed, core/config 254 passed, activate 112 passed; full eslint . --ext=ts --max-warnings=0 exit 0; tsc --noEmit 0 errors under the local tsconfig paths override; eslint suppressions unchanged (prune: 0 semantic diffs). Two recorded observations, neither caused by this commit and neither worth a production change: EnvironmentTeardownError "Closing rpc while onUserConsoleLog was pending" is a HEAD-baseline flake in this spec - the unmodified HEAD spec threw it in 2 of 3 runs (16, 13, 0 unhandled) against 2 of 3 for this file (0, 1, 6), with every test passing in all six runs. DEFAULT_WRITE_DELAY_MS reds in the misc suite are a node_modules junction artifact resolved by aliasing @roo-code/types to this worktree's packages/types/src.
1 parent 420c995 commit 0219d1a

2 files changed

Lines changed: 363 additions & 6 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,13 @@ export class ClineProvider
11591159
}
11601160

11611161
this._disposed = true
1162+
// Unregister at the START of disposal, synchronously and before the first await below.
1163+
// The teardown awaits task eviction and several managers, and while it runs a provider that
1164+
// is already _disposed but still in activeInstances is enumerated by getAllInstances(),
1165+
// getVisibleInstance() and getInstanceForView() - so a sibling profile mutation could persist
1166+
// durable view state and post to a view that is on its way out. Unregistering here makes that
1167+
// window zero-length; no teardown step below looks this instance up in the registry.
1168+
ClineProvider.activeInstances.delete(this)
11621169
this._postStateToWebviewThrottled.cancel()
11631170
this.log("Disposing ClineProvider...")
11641171

@@ -1207,8 +1214,9 @@ export class ClineProvider
12071214
}
12081215

12091216
// Each teardown step gets its own guard: a rejecting step must not strand the steps after
1210-
// it, and the unregistration below must run no matter what - _disposed is already set, so
1211-
// a half-disposed provider would never get another chance to finish here.
1217+
// it. The registry unregistration already happened at the top of dispose(), so a step
1218+
// that throws can no longer leave this provider enumerable; the remaining steps still run,
1219+
// because _disposed is set and this provider never gets another chance to finish here.
12121220
const cleanupFailures: string[] = []
12131221
const attemptCleanup = async (label: string, step: () => unknown) => {
12141222
try {
@@ -1238,8 +1246,6 @@ export class ClineProvider
12381246
this.log("Disposed all disposables")
12391247
}
12401248

1241-
ClineProvider.activeInstances.delete(this)
1242-
12431249
// Clean up any event listeners attached to this provider
12441250
this.removeAllListeners()
12451251

@@ -2911,7 +2917,9 @@ export class ClineProvider
29112917
providerSettings: ProviderSettings,
29122918
): Promise<void> {
29132919
const affected = ClineProvider.getAllInstances().filter(
2914-
(instance) => instance !== this && instance.pinnedProfileName === name,
2920+
// Direct private access: compile-time safe across sibling instances. A sibling that has
2921+
// begun disposal must not be written to - see the note in the loop below.
2922+
(instance) => instance !== this && !instance._disposed && instance.pinnedProfileName === name,
29152923
)
29162924

29172925
if (affected.length === 0) {
@@ -2920,8 +2928,21 @@ export class ClineProvider
29202928

29212929
await Promise.all(
29222930
affected.map(async (instance) => {
2931+
// A provider that has begun disposal owns an orphaned viewStates key: viewId comes from
2932+
// the monotonic nextViewId counter, so no future view ever reads that entry again and
2933+
// prunePersistedViewStates() bounds it. Skipping work on such an instance is therefore
2934+
// safe; performing it is the defect - writing durable state and posting to a webview that
2935+
// is already being torn down.
2936+
// Re-checked before every operation, not just at enumeration: this sibling can start
2937+
// disposing after the filter above ran.
2938+
if (instance._disposed) {
2939+
return
2940+
}
29232941
// Direct private access: compile-time safe across sibling instances.
29242942
await instance._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2943+
if (instance._disposed) {
2944+
return
2945+
}
29252946
await instance.postStateToWebview()
29262947
}),
29272948
)
@@ -2940,7 +2961,10 @@ export class ClineProvider
29402961
replacementSettings: ProviderSettings | undefined,
29412962
): Promise<void> {
29422963
const affected = ClineProvider.getAllInstances().filter(
2943-
(instance) => instance !== this && instance.pinnedProfileName === deletedProfileName,
2964+
// Direct private access: compile-time safe across sibling instances. A sibling that has
2965+
// begun disposal is excluded from the deletion's re-pin set entirely.
2966+
(instance) =>
2967+
instance !== this && !instance._disposed && instance.pinnedProfileName === deletedProfileName,
29442968
)
29452969

29462970
if (affected.length === 0) {
@@ -2966,8 +2990,21 @@ export class ClineProvider
29662990
values.apiConfiguration = replacementSettings
29672991
}
29682992

2993+
// A provider that has begun disposal owns an orphaned viewStates key: viewId comes from
2994+
// the monotonic nextViewId counter, so no future view ever reads that entry again and
2995+
// prunePersistedViewStates() bounds it. Skipping work on such an instance is therefore
2996+
// safe; performing it is the defect - writing durable state and posting to a webview that
2997+
// is already being torn down.
2998+
// Re-checked before every operation, not just at enumeration: the sibling can start
2999+
// disposing while earlier siblings are already being written.
3000+
if (snapshot.instance._disposed) {
3001+
return
3002+
}
29693003
// Direct private access: compile-time safe across sibling instances.
29703004
await snapshot.instance._saveViewLocalStateFromMutation(values)
3005+
if (snapshot.instance._disposed) {
3006+
return
3007+
}
29713008
await snapshot.instance.postStateToWebview()
29723009
}),
29733010
)
@@ -2981,6 +3018,14 @@ export class ClineProvider
29813018
// leaves the view re-pointed. Each restore is awaited on its own so one failing restore
29823019
// cannot skip the next one.
29833020
for (const snapshot of snapshots) {
3021+
// A provider that has begun disposal owns an orphaned viewStates key: viewId comes from
3022+
// the monotonic nextViewId counter, so no future view ever reads that entry again and
3023+
// prunePersistedViewStates() bounds it. Re-checked here too: the sibling can start
3024+
// disposal while the re-pin is already in flight, and compensating through a provider
3025+
// that is tearing down is the defect this guard exists to avoid.
3026+
if (snapshot.instance._disposed) {
3027+
continue
3028+
}
29843029
try {
29853030
await snapshot.instance._saveViewLocalStateFromMutation({
29863031
currentApiConfigName: snapshot.previousPin,

0 commit comments

Comments
 (0)