@@ -372,15 +372,30 @@ async def finalize_composio_connection(
372372
373373 try :
374374 logger .info (f"Waiting for Composio connection { connected_account_id } to become active..." )
375- # Use the SDK's built-in wait function instead of manual polling for robustness.
376- connected_account = await asyncio .to_thread (
377- composio .connected_accounts .wait_for_connection ,
378- id = connected_account_id ,
379- timeout = 120
380- )
381375
382- if not connected_account or connected_account .status != "ACTIVE" :
383- raise HTTPException (status_code = 400 , detail = "Connection could not be verified or is not active." )
376+
377+ # USING A MANUAL POLLING LOOP since the SDK's internal wait_for_connection method is broken.
378+ start_time = time .time ()
379+ timeout = 120 # seconds
380+ connected_account = None
381+
382+ while time .time () - start_time < timeout :
383+ # Use asyncio.to_thread to run the synchronous SDK call in a separate thread
384+ # The .get() method is an alias for .retrieve() and fetches the account by its ID.
385+ connected_account = await asyncio .to_thread (
386+ composio .connected_accounts .get , connected_account_id
387+ )
388+
389+ if connected_account and connected_account .status == "ACTIVE" :
390+ break # Success!
391+
392+ if connected_account and connected_account .status == "FAILED" :
393+ raise HTTPException (status_code = 400 , detail = "Connection failed during authentication with the provider." )
394+
395+ await asyncio .sleep (2 ) # Wait for 2 seconds before polling again
396+ else :
397+ # This block runs if the while loop finishes without a `break`
398+ raise TimeoutError ("Connection verification timed out." )
384399
385400 logger .info (f"Finalized Composio connection for { service_name } : ID { connected_account_id } " )
386401
0 commit comments