android-cookie-store icon indicating copy to clipboard operation
android-cookie-store copied to clipboard

Fatal exception: concurrent modification exception

Open hbj508 opened this issue 2 years ago • 11 comments

java.util.ArrayList$Itr.next (ArrayList.java:860) net.gotev.cookiestore.SharedPreferencesCookieStore.add (SharedPreferencesCookieStore.kt:107) java.net.CookieManager.put (CookieManager.java:360) net.gotev.cookiestore.WebKitSyncCookieManager.put (WebKitSyncCookieManager.kt:37)

hbj508 avatar Nov 16 '23 10:11 hbj508

could you please give more context and steps to reproduce?

gotev avatar Nov 16 '23 11:11 gotev

Happens in prod after upgrade to okhttp5.0-alpha10. not able to reproduce locally

hbj508 avatar Nov 16 '23 11:11 hbj508

please keep using latest okhttp 4.x. OkHttp 5 is not supported yet.

gotev avatar Nov 16 '23 11:11 gotev

Any plans to support it? We had to use fastfallback feature which is available in okhttp 5.0

hbj508 avatar Nov 16 '23 11:11 hbj508

you're welcome to contribute with a PR

gotev avatar Nov 16 '23 12:11 gotev

Hey this happens in older okhttp versions as well. Can you please provide a fix for concurrent modification exception

hbj508 avatar Nov 17 '23 17:11 hbj508

What version are you using? Never experienced this personally, so as I asked initially, more context and steps to reproduce are needed to debug this, which needs to be replicated first. A test would be awesome and you're welcome to open a PR. As a quick check, ensure your code is not reading and accessing cookies from more than one thread and if so, consider synchronizing access. From what I see, the exception gets thrown from java.net.CookieManager which is JVM standard library.

gotev avatar Nov 18 '23 17:11 gotev

We are using 4.9.1. We are not able to reproduce it locally. Was thinking will it help if we switch to concurrent hashmap like below?

 internal val uriIndex = ConcurrentHashMap<URI, MutableList<HttpCookie>>()

hbj508 avatar Nov 24 '23 09:11 hbj508

Hey @gotev can we replace linkedhasmap to concurrentHashmap? It seems be thread safe

hbj508 avatar Feb 20 '24 08:02 hbj508

So, are you using 4.9.1 with cookie store 1.5.0?

From what I see, that snippet you posted is from the InMemoryCookieStore, but the posted stack trace at the beginning of the thread is not referring to it, but to java.net.CookieManager so I'm not getting the correlation between the two without further evidence, given also that InMemoryCookieStore has locked reads and writes.

gotev avatar Feb 20 '24 12:02 gotev