potential race conditions

This commit is contained in:
mertalev
2026-01-20 16:45:24 -05:00
parent d898cba04d
commit e51547cd28
2 changed files with 25 additions and 19 deletions
@@ -36,20 +36,22 @@ object SSLConfig {
serverHost: String?, serverHost: String?,
clientCertHash: Int clientCertHash: Int
) { ) {
val newHash = computeHash(allowSelfSigned, serverHost, clientCertHash) synchronized(this) {
val newRequiresCustomSSL = allowSelfSigned || keyManagers != null val newHash = computeHash(allowSelfSigned, serverHost, clientCertHash)
if (newHash == configHash && sslSocketFactory != null && requiresCustomSSL == newRequiresCustomSSL) { val newRequiresCustomSSL = allowSelfSigned || keyManagers != null
return // Config unchanged, skip if (newHash == configHash && sslSocketFactory != null && requiresCustomSSL == newRequiresCustomSSL) {
} return // Config unchanged, skip
}
val sslContext = SSLContext.getInstance("TLS") val sslContext = SSLContext.getInstance("TLS")
sslContext.init(keyManagers, trustManagers, null) sslContext.init(keyManagers, trustManagers, null)
sslSocketFactory = sslContext.socketFactory sslSocketFactory = sslContext.socketFactory
trustManager = trustManagers?.filterIsInstance<X509TrustManager>()?.firstOrNull() trustManager = trustManagers?.filterIsInstance<X509TrustManager>()?.firstOrNull()
?: getDefaultTrustManager() ?: getDefaultTrustManager()
requiresCustomSSL = newRequiresCustomSSL requiresCustomSSL = newRequiresCustomSSL
configHash = newHash configHash = newHash
notifyListeners() notifyListeners()
}
} }
private fun computeHash(allowSelfSigned: Boolean, serverHost: String?, clientCertHash: Int): Int { private fun computeHash(allowSelfSigned: Boolean, serverHost: String?, clientCertHash: Int): Int {
@@ -120,13 +120,15 @@ private object ImageFetcherManager {
} }
private fun invalidate() { private fun invalidate() {
val oldFetcher = fetcher synchronized(this) {
if (oldFetcher is OkHttpImageFetcher && SSLConfig.requiresCustomSSL) { val oldFetcher = fetcher
fetcher = oldFetcher.reconfigure(SSLConfig.sslSocketFactory, SSLConfig.trustManager) if (oldFetcher is OkHttpImageFetcher && SSLConfig.requiresCustomSSL) {
return fetcher = oldFetcher.reconfigure(SSLConfig.sslSocketFactory, SSLConfig.trustManager)
return
}
fetcher = build()
oldFetcher.drain()
} }
fetcher = build()
oldFetcher.drain()
} }
private fun build(): ImageFetcher { private fun build(): ImageFetcher {
@@ -205,6 +207,7 @@ private class CronetImageFetcher(context: Context, cacheDir: File) : ImageFetche
override fun drain() { override fun drain() {
val shouldShutdown = synchronized(stateLock) { val shouldShutdown = synchronized(stateLock) {
if (draining) return
draining = true draining = true
activeCount == 0 activeCount == 0
} }
@@ -406,6 +409,7 @@ private class OkHttpImageFetcher private constructor(
override fun drain() { override fun drain() {
val shouldClose = synchronized(stateLock) { val shouldClose = synchronized(stateLock) {
if (draining) return
draining = true draining = true
activeCount == 0 activeCount == 0
} }