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,6 +36,7 @@ object SSLConfig {
serverHost: String?, serverHost: String?,
clientCertHash: Int clientCertHash: Int
) { ) {
synchronized(this) {
val newHash = computeHash(allowSelfSigned, serverHost, clientCertHash) val newHash = computeHash(allowSelfSigned, serverHost, clientCertHash)
val newRequiresCustomSSL = allowSelfSigned || keyManagers != null val newRequiresCustomSSL = allowSelfSigned || keyManagers != null
if (newHash == configHash && sslSocketFactory != null && requiresCustomSSL == newRequiresCustomSSL) { if (newHash == configHash && sslSocketFactory != null && requiresCustomSSL == newRequiresCustomSSL) {
@@ -51,6 +52,7 @@ object SSLConfig {
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 {
var result = allowSelfSigned.hashCode() var result = allowSelfSigned.hashCode()
@@ -120,6 +120,7 @@ private object ImageFetcherManager {
} }
private fun invalidate() { private fun invalidate() {
synchronized(this) {
val oldFetcher = fetcher val oldFetcher = fetcher
if (oldFetcher is OkHttpImageFetcher && SSLConfig.requiresCustomSSL) { if (oldFetcher is OkHttpImageFetcher && SSLConfig.requiresCustomSSL) {
fetcher = oldFetcher.reconfigure(SSLConfig.sslSocketFactory, SSLConfig.trustManager) fetcher = oldFetcher.reconfigure(SSLConfig.sslSocketFactory, SSLConfig.trustManager)
@@ -128,6 +129,7 @@ private object ImageFetcherManager {
fetcher = build() fetcher = build()
oldFetcher.drain() oldFetcher.drain()
} }
}
private fun build(): ImageFetcher { private fun build(): ImageFetcher {
return if (SSLConfig.requiresCustomSSL) { return if (SSLConfig.requiresCustomSSL) {
@@ -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
} }