@@ -39,6 +39,7 @@ class CommunityAuthentication(authentication.Authenticator):
3939 """
4040 ALLOWED_TIME_DELAY = 1 * commons_constants .MINUTE_TO_SECONDS
4141 LOGIN_TIMEOUT = 20
42+ DEVICE_NOT_FOUND_RETRY_DELAY = 1
4243 AUTHORIZATION_HEADER = "authorization"
4344 SESSION_HEADER = "X-Session"
4445 GQL_AUTHORIZATION_HEADER = "Authorization"
@@ -234,20 +235,31 @@ async def update_selected_device(self):
234235 async def _load_device_if_selected (self ):
235236 # 1. use user selected device id if any
236237 if saved_uuid := self ._get_saved_gql_device_id ():
237- await self .select_device (saved_uuid )
238- else :
239- # 2. fetch all user devices and create one if none, otherwise ask use for which one to use
240- await self .load_user_devices ()
241- if len (self .user_account .get_all_user_devices_raw_data ()) == 0 :
242- await self .select_device (
243- self .user_account .get_device_id (
244- await self .create_new_device ()
245- )
238+ try :
239+ await self .select_device (saved_uuid )
240+ return
241+ except errors .DeviceNotFoundError :
242+ # proceed to 2.
243+ pass
244+ # 2. fetch all user devices and create one if none, otherwise ask use for which one to use
245+ await self .load_user_devices ()
246+ if len (self .user_account .get_all_user_devices_raw_data ()) == 0 :
247+ await self .select_device (
248+ self .user_account .get_device_id (
249+ await self .create_new_device ()
246250 )
247- # more than one possible device, can't auto-select one
251+ )
252+ # more than one possible device, can't auto-select one
248253
249254 async def select_device (self , device_id ):
250- self .user_account .set_selected_device_raw_data (await self .fetch_device (device_id ))
255+ fetched_device = await self .fetch_device (device_id )
256+ if fetched_device is None :
257+ # retry after some time, if still None, there is an issue
258+ await asyncio .sleep (self .DEVICE_NOT_FOUND_RETRY_DELAY )
259+ fetched_device = await self .fetch_device (device_id )
260+ if fetched_device is None :
261+ raise errors .DeviceNotFoundError (f"Can't find device with id: { device_id } " )
262+ self .user_account .set_selected_device_raw_data (fetched_device )
251263 self .user_account .gql_device_id = device_id
252264 self ._save_gql_device_id (self .user_account .gql_device_id )
253265 await self .on_new_device_select ()
0 commit comments