Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syncthing becomes disconnected while android is sleeping #327

Closed
crocket opened this issue Feb 18, 2019 · 40 comments
Closed

Syncthing becomes disconnected while android is sleeping #327

crocket opened this issue Feb 18, 2019 · 40 comments
Assignees

Comments

@crocket
Copy link

crocket commented Feb 18, 2019

Description of the issue

I just disabled battery optimization for syncthing-fork. While android is sleeping, syncthing-android is disconnected from my desktop computer.

Reproducer

Version Information

App Version: 1.0.1.3
Syncthing Version: v1.0.1
Android Version: LineageOS 14.1
Device manufacturer: Google
Device model: Google Nexus 5

Android Log

@Catfriend1
Copy link
Owner

Does the adb command in the article help?

https://github.com/Catfriend1/syncthing-android/wiki/Android-TV-preparations

@crocket
Copy link
Author

crocket commented Feb 18, 2019

I don't need adb to disable battery optimization for syncthing-fork.
I disabled battery optimization for syncthing-fork in settings.

screenshot_20190219-074353

It seems syncthing-fork is disconnected from my desktop computer whenever there are changes in folder settings or whenever I add folders.

I suspect sycnthing-fork crashes on certain conditions.

syncthing-fork.log

Is syncthing-fork going to sleep anyway after android sleeps for a while?

@Catfriend1
Copy link
Owner

Thanks for the log. Ok there's a crash in it... Maybe that's already fixed in master I recall this fixed a few days ago but unsure if the fix works. I'll compile you a test APK during the next dev session and upload it here so you can check if it fixes the problem.

@Catfriend1
Copy link
Owner

Ref: #322

@crocket
Copy link
Author

crocket commented Feb 19, 2019

The crash is a separate issue.

The issue I want to solve is that I cannot predict or control when syncthing-fork wakes up while android is dozing.

Does syncthing-fork wake up when periodic scanning happens for each directory?
Does syncthing-fork wake up during maintenance windows of Doze?
Does disabling battery optimization have any effect on wake-up schedule on LineageOS 14.1?

@Catfriend1
Copy link
Owner

If the activity crashes the doze exemption will not work... Need to check this

@crocket
Copy link
Author

crocket commented Feb 19, 2019

If the activity doesn't crash, when does syncthing-fork wake up while android is dozing?

@Catfriend1
Copy link
Owner

It stays awake (doze exemption) or it never wakes up. There's no maintenance window the app subscribes to.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Perhaps, should I keep syncthing-fork in recent app list in order to allow syncthing-fork to run in the background? I have a habit of closing every app in recent app list very often.

Or, is the Go process unaffected by Doze or Doze exemption?

@Catfriend1
Copy link
Owner

@crocket : Yes, you shouldn't swipe it away as some Android's handle this as force-close and abort the service.

LGE Nexus 5 (hammerhead), Android 7.1
java.lang.IllegalStateException: 
  at android.support.v4.app.FragmentManagerImpl.addFragment (FragmentManager.java:1916)
  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:765)
  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2625)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2411)
  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2366)
  at android.support.v4.app.FragmentManagerImpl.execSingleAction (FragmentManager.java:2243)
  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss (BackStackRecord.java:654)
  at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate (FragmentStatePagerAdapter.java:168)
  at android.support.v4.view.ViewPager.populate (ViewPager.java:1244)
  at android.support.v4.view.ViewPager.populate (ViewPager.java:1092)
  at android.support.v4.view.ViewPager.setOffscreenPageLimit (ViewPager.java:856)
  at com.nutomic.syncthingandroid.activities.MainActivity.updateViewPager (MainActivity.java:297)
  at com.nutomic.syncthingandroid.activities.MainActivity.onServiceStateChange (MainActivity.java:117)
  at com.nutomic.syncthingandroid.service.SyncthingService.registerOnServiceStateChangeListener (SyncthingService.java:704)
  at com.nutomic.syncthingandroid.activities.MainActivity.onServiceConnected (MainActivity.java:337)
  at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:1453)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:1481)
  at android.os.Handler.handleCallback (Handler.java:751)
  at android.os.Handler.dispatchMessage (Handler.java:95)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6186)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Is it possible to separate the Go service from syncthing-fork app?
What would it take to prevent syncthing-fork from sleeping?

@Catfriend1
Copy link
Owner

This is found on the forum as it's often been discussed. Search for sdcard issue.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

https://github.com/syncthing/syncthing-android/wiki/Frequently-Asked-Questions#what-about-sd-card-support doesn't seem relevant to my issue. Or, were you referring to https://forum.syncthing.net/t/when-does-syncthing-synchronize-while-android-is-sleeping/12862 ?

I discovered that my nexus 5 connects to my desktop computer on random intervals while android is dozing.

@Catfriend1 Catfriend1 self-assigned this Feb 19, 2019
@Catfriend1 Catfriend1 reopened this Feb 19, 2019
@Catfriend1
Copy link
Owner

https://forum.syncthing.net/t/syncthing-can-not-write-to-sdcard/12773/9
syncthing/syncthing-android#1008
syncthing/syncthing-android#29

Roadmap, to avoid spending my dev time in discussing.

  1. There is a new release 1.0.1.4 (beta) on Gplay and Github. Please test and report back if the "IllegalStateException" has gone away. If not, please post a new log.

  2. The sleeping/Doze issues are a separate thing. I've already noted to look into it when it's time.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Here's my incomplete conclusion from my investigation. I might be wrong because I don't write android apps.

According to https://developer.android.com/training/monitoring-device-state/doze-standby#restrictions
wake locks are ignored during doze.

According to https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases
if battery optimization is disabled for your app, your app can use network and obtain partial wakelock during doze. Partial wakelock enables an app to use CPU. Can an app use network without CPU? My guess is that if you want your app to be always running, you should disable battery optimization for your app and obtain partial wakelock for it.

From https://developer.android.com/training/monitoring-device-state/doze-standby#understand_doze
I can infer that during doze maintenance windows, every app is given a chance to do its work even if the app doesn't know anything about doze. I guess you don't have to rewrite syncthing in java in order to utilize doze maintenance windows.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

I installed com.github.catfriend1.syncthingandroid_gplay_v1.0.1.4_cdcdc3c1.apk and tested it.

Connection was severed temporarily after adding, removing, or sharing a folder on my computer or nexus 5. But, there was no crash. I don't know what caused the crash in the first place, though.

@Catfriend1
Copy link
Owner

Good observation! Matches my knowledge about what I've already researched. thanks for writing the summary :).
Were you able to stay connected now after the initial temporary disconnect what's normal in syncthing ?
If not, try to add tcp4://phone_ip on the computer's side.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Connections are restored soon after temporary disconnections caused by adding, removing, or sharing folders.

Partial wakelock option is permanently disabled in syncthing-fork, but it can be enabled in syncthing.

When partial wakelock is enabled in syncthing-fork, should it prompt users to disable battery optimization for syncthing-fork, too? It seems to me that you need to disable battery optimization to utilize partial wakelock for continuous sync. Or, is it enough to disable battery optimization and uncheck "Respect Android battery saving setting"?

https://developer.zebra.com/community/home/blog/2018/10/26/keeping-your-application-running-when-the-device-wants-to-sleep says

Note that wake locks will be automatically released when the device enters doze mode unless battery optimization is disabled for the application which acquired the lock (see later)

WiFi locks are only applicable to Android Nougat devices and below (Marshmallow, Lollipop etc). From Android Oreo onward the WiFi will always be on when the device is sleeping.

@Catfriend1
Copy link
Owner

The wakelock thingy in the official app is outdated and only works before android 6.x . That's why it was disabled in the fork permanently on higher versions. You can't bypass doze except whitelisting the app from doze which is what syncthing fork enforces during the welcome wizard.

"Respect Android battery saving setting" is unrelated to the sleep topic as its the power save mode the phone enters e.g. when the battery percentage gets below 20%.

From what I reaf between the lines: your issue is solved. Temporary disconnects after changing the config (folders, devicwles etc.) are expected.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

My conclusion is that if you are not going to use partial wakelock, disabling battery optimization doesn't change anything.

My suggestion

  1. Stop the welcome wizard from prompting users to disable battery optimization
  2. When partial wakelock is enabled, prompt users to disable battery optimization.
  3. Add an option to acquire wifi lock.

@Catfriend1
Copy link
Owner

Catfriend1 commented Feb 19, 2019

@crocket I believe what you're saying but other tickets say this isn't correct according to user reports. Wifi lock for example has no impact when android 7 is dozing. On 7.1 it may have but there, disabling battery optimization solves the issue. I use the app myself quite heavily and - may this be manufacturer or ROM specific - it worked without disconnects on my phones >= 7.1. If you'd like to help and contribute code, we can start digging deeper into the can of worms testing with your specific device. Partial wakelocks were ignored on my tests in the past which leaves me unsure.

Testing here myself, disabling power optimization definitely was a requirement for syncthing to keep connected.
See also : #192 User olav described there what happens if you don't use power optimization .

@Catfriend1
Copy link
Owner

"Stop the welcome wizard from prompting users to disable battery optimization"

I wont do this unless we have a better working code in place. Most users of official got used to and thought they can reduce battery usage by not exempting from doze which was optionally - and it led to a bunch of failing to connect syncthing instances. I'm not opposed to improve it but thinking of all that different Android's out there I guess it's a move from 70% working devices to 30% devices. Maybe others reading this can chime in and give their device setup and details if they have the permanently disconnected observation. For now I'll close this but still read reports, comments or suggestions which are welcome to get a better overview how devices behave.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Syncthing-fork runs a foreground service which is exempt from Doze from android nougat onward according to https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/ and https://www.linkedin.com/learning/android-development-building-battery-conscious-apps/overriding-doze-mode-using-a-foreground-service.

It seems a foreground service doesn't need to acquire partial wakelock or disable battery optimization. Weirdly, disabling or enabling battery optimization of syncthing-fork doesn't affect how often syncthing-fork is disconnected.

  1. Is the Go process totally independent of the foreground service?
  2. Is the Go process totally independent of battery optimization and partial wakelock?
  3. Perhaps, the fact that all my syncthing instances rely only on local discovery is the reason that I experience frequent disconnections that usually last 5~13 minutes? Or, is it just the intervals between doze maintenance windows?

@Catfriend1
Copy link
Owner

  1. Its no java, so independent.
  2. It needs adapters to stay awake (lan, wifi)
  3. Try entering the tcp4://ip on the computer as Android may block broadcasting which local discovery relies on to find the ip automatically.

@crocket
Copy link
Author

crocket commented Feb 19, 2019

Is the Go process automatically exempt from doze? The Go process may not qualify as an app which is subject to Doze.

I don't understand how disabling battery optimization for syncthing-fork disables battery optimization for syncthing Go process. Why would a foreground service need to disable battery optimization in the first place?

@Catfriend1
Copy link
Owner

Well I don't know it that deep but from trying and comparing with other apps like bubbleupnp, linphone ... It's the same observation. A foreground service alone isn't enough as this for example helps rhe app not being killed by Android's algorithms when it's backgrounded (nothing to do with doze here). I'm no Android OS expert in the deep so my observation is , the Android os creates a VM procesa group where the go proc is a child. If the java proc is alive during doze, the whole proc group is alive.

@Catfriend1
Copy link
Owner

Conclusion: will readd the partial wake lock option. From what I heard a minority of users will need this as for example my own hardware works fine without the partial lock but needs exemption from doze instead to keep connected while sleeping.

@Catfriend1 Catfriend1 reopened this Feb 21, 2019
@crocket
Copy link
Author

crocket commented Feb 21, 2019

What is your hardware and operating system?

@Catfriend1
Copy link
Owner

Catfriend1 commented Feb 21, 2019

I've tested:

  • Samsung I9100 Android 8.1 lineage
  • LG G4 h815 - Android 7.1.2 lineage
  • Xiaomi mi8 - Android 8.1 miui 10
  • Huawei p10 - Android 8.0
  • Motorola X Play lux XT1652 - Android 7.1.1 had the same problems like you reported - updated it to 8.1 and problems have gone away.

Got user report by o-l-a-v that Android TV need exempt from doze or the app is useless.

@Catfriend1
Copy link
Owner

Maybe also wifi sleep policy is also relevant as I've checked the setting always scan for wifi in android settings.

@crocket
Copy link
Author

crocket commented Feb 21, 2019

Android 8.1 removed Keep Wi-Fi on during sleep option.

@Catfriend1
Copy link
Owner

Set it via adb put secure setting.
Manufacturers unfortunately have a great impact why Androids are not the same if the version is the same. See this:
screenshot_2019-02-21-12-29-36-506_com android settings

@Catfriend1
Copy link
Owner

Plus, in the location settings there is also an advanced option in Android to scan WiFi and Bluetooth. It's a whole mess, so I change the app if enough people confirm a change makes it better. This has nothing to do with you personally - I need to clarify first that it will help more people than destroying things on other phones where it operated fine already.

@crocket
Copy link
Author

crocket commented Feb 21, 2019

I give up on fiddling with Wi-Fi settings.

@Catfriend1
Copy link
Owner

In summary, the forked version doesn't work for you?

@crocket
Copy link
Author

crocket commented Feb 22, 2019

It works. But, disabling battery optimization doesn't have any visible effect on connection reliability on LineageOS 14.1 and LineageOS 15.1

I suspect android TV is different from mobile devices because it dosen't operate on battery.

I think it would be better to turn syncthing into a library and call the library from the android app.

@Catfriend1
Copy link
Owner

The library idea is already discussed in upstream. But upstream needs a contributor to do so as a clean solution.

@bt90
Copy link

bt90 commented Feb 22, 2019

https://dontkillmyapp.com/ (maybe worth adding to the first-run slideshow)

Locking in the recent apps view might help aswell.

@Catfriend1
Copy link
Owner

Catfriend1 commented Feb 22, 2019

Thank you both for the pointers. Will read through this weekend.

@Catfriend1
Copy link
Owner

@Catfriend1 Catfriend1 changed the title syncthing-android becomes disconnected while android is sleeping. Syncthing becomes disconnected while android is sleeping Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants