@@ -231,25 +231,29 @@ async def close(self):
231
231
None , None , None
232
232
)
233
233
except Exception as e :
234
- logger .error (
235
- f"Error during connection manager __aexit__ : { e } "
234
+ logger .warning (
235
+ f"Error during connection manager cleanup : { e } "
236
236
)
237
237
238
238
# Clean up the connection manager from the context
239
239
delattr (self .context , "_mcp_connection_manager" )
240
240
logger .info (
241
241
"Connection manager successfully closed and removed from context"
242
242
)
243
-
244
- self .initialized = False
243
+ else :
244
+ logger .debug (
245
+ f"Aggregator closing with ref count { current_count } , "
246
+ "connection manager will remain active"
247
+ )
245
248
except Exception as e :
246
249
logger .error (
247
250
f"Error during connection manager cleanup: { e } " , exc_info = True
248
251
)
249
- # TODO: saqadri (FA1) - Even if there's an error, we should mark ourselves as uninitialized
250
- self .initialized = False
251
252
span .set_status (trace .Status (trace .StatusCode .ERROR ))
252
253
span .record_exception (e )
254
+ finally :
255
+ # Always mark as uninitialized regardless of errors
256
+ self .initialized = False
253
257
254
258
@classmethod
255
259
async def create (
@@ -289,7 +293,12 @@ async def create(
289
293
logger .error (f"Error creating MCPAggregator: { e } " )
290
294
span .set_status (trace .Status (trace .StatusCode .ERROR ))
291
295
span .record_exception (e )
292
- await instance .__aexit__ (None , None , None )
296
+ try :
297
+ await instance .__aexit__ (None , None , None )
298
+ except Exception as cleanup_error :
299
+ logger .warning (
300
+ f"Error during MCPAggregator cleanup: { cleanup_error } "
301
+ )
293
302
294
303
async def load_server (self , server_name : str ):
295
304
"""
0 commit comments