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

Fix SlimeVR server from not working reliably on Quest standalone #1141

Merged
merged 2 commits into from
Aug 28, 2024

Conversation

swax
Copy link
Contributor

@swax swax commented Aug 19, 2024

The SlimeVR server running on Android is at risk of being killed when running in the background and/or when under memory pressure. This is especially bad on the Quest as playing a game requires a lot of memory and it doesn't take long for Android to kill the SlimeVR server if it is running the background.

According to the docs, processes with foreground services are less likely to be killed by Android, for example a music player running in the background. We can start a foreground service using startForegroundService, one of the stipulations being that the service must also show a persistent notification as well. An importance level of 'low' keeps the notification out of the way, only in the alerts tray.

Tested on Quest with server running in background:

  • Verified the current SlimeVR server is killed after a period of time
  • Verified with the new build the server remains running for over an hour, in high pop worlds, with all avatars on (high memory pressure)

Notes:
There isn't a way to shutdown the SlimeVR server from within the GUI afaik. This is an issue on Quest as there isn't a way to manually kill apps. The only way to kill the server on Quest is to restart the device (afaik).

What the alert looks like on Quest, it only shows up in the notifications dialog, not in the game
IMG_8239

…round service to notify the user that the server is running
@swax swax changed the title Prevent Android from unexpectedly killing the server Fix SlimeVR server from not working reliably on Quest standalone Aug 19, 2024
@ButterscotchV ButterscotchV added Priority: High Important feature or blocks something important Type: Enhancement Adds or improves a feature OS: Android Operating system: Android Area: Server Related to the server labels Aug 28, 2024
@ImUrX ImUrX merged commit 5c0d3e9 into SlimeVR:main Aug 28, 2024
8 checks passed
@ButterscotchV
Copy link
Member

I finally got around to getting this on my phone, I don't see the notification show up, does it not have permission? Is there supposed to be a notification on phones? For context, this is on Android 14, I know Quest is on an older version.

@swax
Copy link
Contributor Author

swax commented Sep 21, 2024

Hrm, so the app now starts a foreground task, and foreground tasks are required to have a persistent notification that can't be dismissed. With it set to a low priority notification it should be in the notification manager, but not pop up over any active applications. Is there like a notification tray, or filters on the tray you can double check?

The permissions should be fine, I added the foreground service permission for this change. If we updated the build to Android 14 then we would also want to add the FOREGROUND_SERVICE_CONNECTED_DEVICE permission and call startForeground() with FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE, but for now Android 14 shouldn't care about that if the app is built for Android 12.

I just found this note here: https://developer.android.com/develop/background-work/services/foreground-services
"Note: Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services. On these devices, the system waits 10 seconds before showing the notification associated with a foreground service." So maybe wait a minute and check again?

These persistent notifications are also dismissible in Android 13 and above, so maybe with it being low priority it was auto dismissed somehow? This is the least likely possibility.

@ButterscotchV
Copy link
Member

ButterscotchV commented Sep 22, 2024

For context this is a Pixel 8 running Android 14, it doesn't show up where persistent apps usually do (it makes a section for "Silent" notifications), it shouldn't be dismissed and if I re-open it would show up again. I can see it's running in the background for now but I assume it will be killed eventually.

@swax
Copy link
Contributor Author

swax commented Sep 22, 2024

I pasted the changelog into ChatGPT o1 and asked it for ideas of wtf is going on, and it tipped me off to this permission POST_NOTIFICATIONS that is new in Android 13. Luckily not a hallucination, I read over the docs and there's a bunch of notes in there about how older apps are treated, there's a good chance this is what's preventing the notification from getting through. It even mentions you still need to create a notification for foregrounds services:

Note: Apps don't need to request the POST_NOTIFICATIONS permission in order to launch a foreground service. However, apps must include a notification when they start a foreground service, just as they do on previous versions of Android.

It does say that Android 14 should ask the user for notification permissions the first time an older app tries to create one, but it doesn't sound like that happened for you. Maybe there's an entry in some centralized notification settings somewhere it could be flipped on? Not critical for the notification to show up as that's how it works now, though useful I think for the user to know there's a server running in the background. More importantly though is that the foreground service is running which the notification would help us confirm at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Server Related to the server OS: Android Operating system: Android Priority: High Important feature or blocks something important Type: Enhancement Adds or improves a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants