fix(#545): fix gated ws auth loop and ticket refresh (#546)

Fix gated close codes, run ticket refresh on Dispatchers.IO, prevent scope race on startup, and auto-format files.
This commit is contained in:
M57 authored and GitHub committed 2026-07-11 00:21:23 +03:00
1 parent 53b3966931
commit 480d29e481
12 files changed
+118 -24

No files matched your search

@@ -45,7 +45,7 @@ object CookieManager {
synchronized(this) {
if (jar != null) return
val store = EncryptedCookieStore(context.applicationContext, legacyPrefsDeferred)
jar = PersistentCookieJar(store, scope)
jar = PersistentCookieJar(store, scope, initialServerId)
// Eagerly load the initial scope off the caller thread so the
// first REST call is instant without blocking app startup.
scope.launch { jar!!.useStore(initialServerId) }
@@ -33,12 +33,13 @@ import java.util.concurrent.atomic.AtomicReference
class PersistentCookieJar(
private val store: CookieStore,
private val storeScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO),
initialServerId: String = DEFAULT_SERVER_ID,
) : CookieJar {
// serverId -> (host -> cookies)
private val cache = ConcurrentHashMap<String, MutableMap<String, MutableList<Cookie>>>()
private val loadedScopes = ConcurrentHashMap.newKeySet<String>()
@Volatile private var currentServerId = AtomicReference(DEFAULT_SERVER_ID)
@Volatile private var currentServerId = AtomicReference(initialServerId)
private val scopeMutex = Mutex()
@@ -60,6 +61,9 @@ class PersistentCookieJar(
*/
fun getCookie(name: String): Cookie? {
val serverId = currentServerId.get()
if (!loadedScopes.contains(serverId)) {
kotlinx.coroutines.runBlocking { ensureLoaded(serverId) }
}
val hosts = cache[serverId] ?: return null
for (bucket in hosts.values) {
synchronized(bucket) {