erickok / transdroid

Manage your torrents from your Android device
GNU General Public License v3.0
1.28k stars 201 forks source link

Initialization sync fixes #599

Closed ImperatorPrime closed 2 years ago

ImperatorPrime commented 2 years ago

I dug into the root cause of #588 and managed to reproduce it. It looks like the issue is because there are two different background threads trying to lazily initialize the HttpClient at the same time. This corruption of state carried over to the CookieStore initialization, as both threads were trying to initialize the CookieStore at the same time. So even though one thread had set a cookie, the other was clearing the cookie with its copy of the cookie store. It seems to occur specifically when we were entering the isAuthenticated method, which I think was pushing the empty cookie store into memory. This results in an empty cookie when we check auth after doing the first auth call, and then falling back to the old auth API, which will fail.

This pull request does two things to rectify this problem. All lazy initializations of the HttpClient are now synchronized, so only one copy can ever be created and assigned to that variable. I've cached the adapters based on their settings. This enables reuse of both the session cookie (if the adapter has one) and the HttpClient, which is thread safe for requests. The cache is keyed to the exact settings used to construct the adaptor - if we attempt to load an adapter with the same id but different settings than are cached, we regenerate the cache entry.

Testing on QBittorent, I saw no slowness and slightly less memory consumption than current master.

Fixes #588, Fixes #534, Fixes #567 and should properly fix #416, which seems to be the same underlying issue.

kevin-stuart commented 2 years ago

Amazing. I have been waiting forever for a fix. If I may be so forward to ask: How are new releases of Transdroid scheduled? Instead of building it myself, I would prefer an officially signed apk.

erickok commented 2 years ago

@kevin-stuart @ImperatorPrime I made a new release https://github.com/erickok/transdroid/releases/tag/v2.5.21