akumaigorodski / wallet

Bitcoin wallet for Android
https://sbw.app
Apache License 2.0
240 stars 74 forks source link

app crashes a lot when tor connetions are on #164

Open pony-montana opened 1 year ago

pony-montana commented 1 year ago

Hi, the problem accour only with the "use tor connection" option enable (without setting any custom electrum server).

When I close the app the tor connections terminate, but after 1-2 seconds new connections spawns like if I'm starting the app. They then remain in backgroung for long time, until android kills the process correlated. If I start SBW when these connections are running in background, it will uses them and no connections are killed or spawn during the startup and sync process (2 times over 3 it fails to fetch data and results "offline"). At this point if I close the app, the connections will be closed; after 1-2 seconds new tor connections will spawn and the cicle will goes on. .. If I force-close SBW killing all the prcessess the cicle ends, connections are killed and no connections will start unless I decide to start the app.

When on clearnet (default servers), when I close SBW all the connections stop as expected and nothing strange spawn.

This different behaviour of tor and clearnet make me think that the problem is not related to my specific android-ui/launcher/process-killer, but could be something broken with handling tor connections.

akumaigorodski commented 1 year ago

I've updated tor-android version to a newer one, otherwise have no idea what else I can do here given that something is happening when a wallet app is closed.

pony-montana commented 1 year ago

I checked the last version, the behaviour is identical. I don't know if that could be correlated to the issue -> https://github.com/akumaigorodski/wallet/issues/122 It seems to me a problem with the logic of "when to trigger start/stop tor connections". When the app closes, connections are closed, but then something trigger the connections and make them respawn. Also, when tor connections spawn after the app closes, some data is exchanged; I think is the sync with electrum server because if you reopen the app in the next few seconds you will find the wallet synched. Only if you reopen it after a bit longer time, like some minutes, you will experience a "never ending sync", solved by closing and reopening the app. Is like that when the app is closed it then restarts in background (or, at least, try to do that).

akumaigorodski commented 1 year ago

FWIW #122 fix is about app instructing Tor service to respawn after an app has been reopened, it is not about app trying to do something while in background.

Also technically, if what's meant by closing an app is pressing a back button then all this does is it kills a UI part while stuff like data structures in memory and networking remains for some time. Android does not have a clear concept of closing besides explicit force-closing.

pony-montana commented 1 year ago

The problem is not closing the app; it trigger an expexted kill-all-connections when force-closed from task manager. The problem accour 2 seconds after the app ha been force closed: at this point, it opens in background 3 new connections and exchange around 20kb of data. Then, after at least 10 seconds, if you open the app it will never sync. The way you can obtain fast sync (nearly instant if connection conditions are good) is to be fast: After the app has been force closed, reopen it in less that 2 seconds. Without the network analisys, this is a very pragmatical way to see evident differences in tor sync.

pony-montana commented 1 year ago

Hi, in the 2.5.2 the tor situation is changed: now the app crashes on startup. The issue is correlated to the previous. Now the app crashes, previously "didn't sync", but the root of the problem is the same.

akumaigorodski commented 1 year ago

Any chance you use some kind of non-standard setup? If not, can you let me know your phone model and Android version? I've never experienced tor-related crashes on emulators or phones I've used so can't reproduce this.

pony-montana commented 1 year ago

The problem is present on last lineageos based on android 13 without google playservices, and was also present in previous lineageos based on android 12 without google playservices. Tested also a xiaomi miui phone and the problem here is not present. Something strange happens when I kill the app from the lineageos launcher/task manager. I think the problem could be present also in stock android from google pixel phones because they use a similar launcher under the hood.

edit: foud that the way lineageos "kills" the application from task manager makes it to restart as a process in background memorized in cache.