I am making use of the Android Async Http Library in my app to make async http requests.
I have come into a situation in my android app where the following happens. My web api makes use of an access token and a refresh token. On every request that I make I check if the access token is still valid. If it is not I then issue a http post to go get a new access token using the refresh token.
Now i have noticed the following situation.
user of my app leaves their phone inactive for enough time for the access token to expire. When they wake the phone up. In my onResume()
function I fire off two separate http requests.
- Request 1 checks the access token and determines its not valid. it then issues a
refreshAccessToken
request. - While Request 1 is waiting for the response, Request 2 also checks the access token and determines its not valid. And it also issues a
refreshAccessToken
request. - Request 1 returns successfully and updates the access token and refresh token values.
- Request 2, then gets a
401
response from the api as the refresh token which it provided has already been used. My application then thinks that there is an error with the refreshToken and logs the user out.
This is obviously incorrect and I would like to avoid it. I have in the mean time, done a double check in the refreshAccessToken
onFailed()
method. To see if the accessToken is maybe valid again. However this is inefficient as I am still sedning two requests over the air, and my API has to handle the failed refresh attempt.
Question: Now my issue is that i cant use any locks or synchronization as you cannot block the main UI thread in android. And Android Async Http Library handles all of the different threads etc.