@@ -259,19 +259,31 @@ export class ClineProvider
259259 private taskHistoryStoreInitialized = false
260260 public static readonly PENDING_OPERATION_TIMEOUT_MS = 30000 // 30 seconds
261261 private providerProfileMutationQueue = Promise . resolve ( )
262+ private providerProfileMutationAbortController = new AbortController ( )
262263 private historyTaskCreationQueue = Promise . resolve ( )
263264
264265 private runDelegationTransition < T > ( parentTaskId : string , fn : ( ) => Promise < T > ) : Promise < T > {
265266 return runDelegationTransition ( ClineProvider . delegationTransitionLocks , parentTaskId , fn )
266267 }
267268
268269 private enqueueProviderProfileMutation < T > ( fn : ( signal : AbortSignal ) => Promise < T > ) : Promise < T > {
270+ if ( this . _disposed ) {
271+ return Promise . reject ( new Error ( "ClineProvider is disposed" ) )
272+ }
273+
269274 const controller = new AbortController ( )
275+ const onProviderDispose = ( ) => controller . abort ( )
276+ this . providerProfileMutationAbortController . signal . addEventListener ( "abort" , onProviderDispose , { once : true } )
277+
270278 // Run fn after either outcome so a rejected mutation never poisons the queue.
271- const run = this . providerProfileMutationQueue . then (
272- ( ) => fn ( controller . signal ) ,
273- ( ) => fn ( controller . signal ) ,
274- )
279+ const run = this . providerProfileMutationQueue
280+ . then (
281+ ( ) => fn ( controller . signal ) ,
282+ ( ) => fn ( controller . signal ) ,
283+ )
284+ . finally ( ( ) => {
285+ this . providerProfileMutationAbortController . signal . removeEventListener ( "abort" , onProviderDispose )
286+ } )
275287 const callerResult = this . withProviderProfileMutationTimeout ( run , ( ) => {
276288 controller . abort ( )
277289 this . log ( "Provider profile mutation timed out; aborting in-flight mutation" )
@@ -294,9 +306,8 @@ export class ClineProvider
294306 } ,
295307 )
296308
297- // Advance from the timeout-bounded result. Each fn checks its AbortSignal before
298- // writing state, so advancing the queue on timeout cannot produce stale overwrites.
299- this . providerProfileMutationQueue = callerResult . then (
309+ // Keep serialization in place until the timed-out mutation and its rollback settle.
310+ this . providerProfileMutationQueue = run . then (
300311 ( ) => undefined ,
301312 ( ) => undefined ,
302313 )
@@ -848,6 +859,7 @@ export class ClineProvider
848859
849860 this . _disposed = true
850861 this . _postStateToWebviewThrottled . cancel ( )
862+ this . providerProfileMutationAbortController . abort ( )
851863 this . log ( "Disposing ClineProvider..." )
852864
853865 // Reject any tasks still waiting for a scheduler permit so they don't
@@ -1963,8 +1975,14 @@ export class ClineProvider
19631975 * from is dropped instead of saved and reactivated.
19641976 */
19651977 async updateProfileModel ( name : string , expectedProvider : string , patch : Record < string , unknown > ) : Promise < void > {
1978+ if ( this . _disposed ) {
1979+ return
1980+ }
1981+
19661982 try {
19671983 await this . enqueueProviderProfileMutation ( async ( signal ) => {
1984+ if ( signal . aborted || this . _disposed ) return
1985+
19681986 // Mirrors the profile name the webview is shown (see getStateToPostToWebview).
19691987 const task = this . getCurrentTask ( )
19701988 const { currentApiConfigName, organizationAllowList : stateOrganizationAllowList } =
@@ -1978,7 +1996,7 @@ export class ClineProvider
19781996
19791997 const { name : _name , id, ...stored } = await this . providerSettingsManager . getProfile ( { name } )
19801998
1981- if ( signal . aborted ) return
1999+ if ( signal . aborted || this . _disposed ) return
19822000
19832001 // A profile without an explicit provider is treated as OpenRouter, matching the chat ModelSelector.
19842002 const storedProvider = stored . apiProvider ?? providerIdentifiers . openrouter
@@ -2038,35 +2056,33 @@ export class ClineProvider
20382056 }
20392057
20402058 let savedConfig = false
2041- let updatedContext = false
2059+ let shouldRollbackContext = false
2060+ const originalContextSettings : ProviderSettings = { ...stored , id } as ProviderSettings
20422061 try {
20432062 await this . providerSettingsManager . saveConfig ( name , merged as ProviderSettings )
20442063 savedConfig = true
20452064
2046- if ( signal . aborted ) {
2065+ if ( signal . aborted || this . _disposed ) {
20472066 throw new Error ( "Provider profile mutation aborted" )
20482067 }
20492068
20502069 await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
20512070 if ( name === currentApiConfigName ) {
2071+ shouldRollbackContext = true
20522072 await this . contextProxy . setProviderSettings ( merged as ProviderSettings )
2053- updatedContext = true
20542073 }
20552074
2056- if ( signal . aborted ) {
2075+ if ( signal . aborted || this . _disposed ) {
20572076 throw new Error ( "Provider profile mutation aborted" )
20582077 }
20592078 } catch ( updateError ) {
20602079 if ( savedConfig ) {
20612080 try {
2062- await this . providerSettingsManager . saveConfig ( name , { ... stored , id } as ProviderSettings )
2081+ await this . providerSettingsManager . saveConfig ( name , originalContextSettings )
20632082 await this . updateGlobalState (
20642083 "listApiConfigMeta" ,
20652084 await this . providerSettingsManager . listConfig ( ) ,
20662085 )
2067- if ( updatedContext ) {
2068- await this . contextProxy . setProviderSettings ( { ...stored , id } as ProviderSettings )
2069- }
20702086 } catch ( rollbackError ) {
20712087 this . log (
20722088 `Failed to rollback profile '${ name } ' after update failure: ${
@@ -2075,10 +2091,21 @@ export class ClineProvider
20752091 )
20762092 }
20772093 }
2094+ if ( shouldRollbackContext ) {
2095+ try {
2096+ await this . contextProxy . setProviderSettings ( originalContextSettings )
2097+ } catch ( rollbackError ) {
2098+ this . log (
2099+ `Failed to rollback context settings for '${ name } ' after update failure: ${
2100+ rollbackError instanceof Error ? rollbackError . message : String ( rollbackError )
2101+ } `,
2102+ )
2103+ }
2104+ }
20782105 throw updateError
20792106 }
20802107
2081- if ( signal . aborted ) {
2108+ if ( signal . aborted || this . _disposed ) {
20822109 return
20832110 }
20842111
0 commit comments