Authenticator breaking when refreshing tokens with coroutines runBlocking

Viewed 426

I have this nondeterministic bug in production and I can't pin it down with a minimal example My setup is okhttp, which drives Retrofit, which uses coroutines adapter I use Authenticator to refresh tokens and retry the requests, something like this

fun Authenticator.authenticate() {
    ...
    synchronized(this) {
        runBlocking { retrofitService.suspendingRefreshTokens() }<--- 
        ...
    }
}

What however SOMETIMES happens is that the api call just doesn't finish, as if the coroutines were broken. I searched the internets and there seem to be some folks affected by this as well

Using Coroutine runblock with the Authenticator to handle 401 response from retrofit

Any intuition on what could be happening? I was thinking it was some sort of a deadlock because of the synchronized, however runBlocking seems to switch back to the original context, so it should be the same thread (atleast it appears that way in testing)

This never happened before with rxjava, where I used the same blockingGet() way to keep it blocking inside Authenticator; However rxjava retrofit adapter didn't use the async parameter; which leads me to believe it possibly could be a issue with Okhttp's threadpools somehow; rather than runBlocking? (which I also read is buggy)

0 Answers
Related