Skip to content
New issue

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

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

Already on GitHub? # to your account

Close unidentified websocket when the app is in background #13337

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

p1gp1g
Copy link

@p1gp1g p1gp1g commented Dec 29, 2023

First time contributor checklist

Contributor checklist

  • Pixel 6A, Android 14
  • My contribution is fully baked and ready to be merged as is
  • I ensure that all the open issues my contribution fixes are mentioned in the commit message of my first commit using the Fixes #1234 syntax > This is related to Excessive battery drain  #12341, but it requires some feedback before considering it fixed.

Description

At present, Signal behaves in the same way in the background as it does when you're using the application : it keeps the 2 websockets connected (unidentified and identified). But only the identified one is necessary. Having the unidentified websocket connected is a useless resource consumption, and if it fails, then it restart the websockets for nothing.

This patch change the behavior of the IncomingMessageObserver to close the unidentified websocket when it goes in background, after a timeout. This will reduce the number of reconnection, and the number of websocket messages, and the resources used by Signal.
When the user needs the unidentified socket, it connects again.


Before, you can see the unidentified websocket in use:

2023-12-29 14:27:07.381  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 951192 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:27:07.381  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:27:36.291  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:27:36.315  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:28:06.291  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:28:06.313  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:28:07.396  3498-4728  IncomingMessageObserver W  Application level read timeout...
2023-12-29 14:28:07.475  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 1011326 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:28:07.476  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:28:36.293  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:28:36.304  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:29:06.295  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:29:06.302  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:29:08.371  3498-4728  IncomingMessageObserver W  Application level read timeout...
2023-12-29 14:29:08.428  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 1072301 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:29:08.428  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:29:36.293  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:29:36.302  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:30:06.297  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:30:06.318  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:30:08.441  3498-4728  IncomingMessageObserver W  Application level read timeout...
2023-12-29 14:30:08.548  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 1132371 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:30:08.548  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:30:36.970  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:30:36.982  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:31:06.971  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:31:06.992  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:31:08.563  3498-4728  IncomingMessageObserver W  Application level read timeout...
2023-12-29 14:31:08.635  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 1192493 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:31:08.635  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:31:36.973  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:31:36.993  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:31:59.827  3498-3498  IncomingMessageObserver D  Background service started.
2023-12-29 14:31:59.827  3498-3498  IncomingMessageObserver D  Background service started.
2023-12-29 14:32:06.974  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:32:06.982  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...
2023-12-29 14:32:08.650  3498-4728  IncomingMessageObserver W  Application level read timeout...
2023-12-29 14:32:08.716  3498-4728  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: true, Time Since Last Interaction: N/A, FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 14:32:08.717  3498-4728  IncomingMessageObserver D  Reading message...
2023-12-29 14:32:36.974  3498-4747  WebSocketConnection     I  [normal:97882913] Sending keep alive...
2023-12-29 14:32:36.989  3498-4747  WebSocketConnection     I  [unidentified:84802253] Sending keep alive...

After, you can see the unidentified websocket being closed. It is open again when I open a conversation:

2023-12-29 15:30:04.568  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 3469 ms (within limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:30:04.568  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:30:04.574  9040-9089  IncomingMessageObserver D  Disconnecting unidentified websocket
2023-12-29 15:30:04.621  9040-9626  WebSocketConnection     I  [unidentified:18212726] onClosing()
2023-12-29 15:30:04.622  9040-9626  WebSocketConnection     I  [unidentified:18212726] onClose()
2023-12-29 15:30:34.577  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:31:04.589  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:31:04.698  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:31:04.720  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 63617 ms (within limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:31:04.721  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:31:04.726  9040-9089  IncomingMessageObserver D  Disconnecting unidentified websocket
2023-12-29 15:31:34.614  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:32:04.631  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:32:04.734  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:32:04.759  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 123653 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:32:04.760  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:32:34.646  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:33:04.657  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:33:04.775  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:33:04.810  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 183695 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:33:04.810  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:33:34.679  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:34:04.696  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:34:04.831  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:34:04.859  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 243750 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:34:04.859  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:34:34.718  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:35:04.737  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:35:04.876  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:35:04.907  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 303796 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:35:04.907  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:35:34.755  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:36:04.776  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:36:04.923  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:36:04.968  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 363843 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:36:04.968  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:36:34.781  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:37:04.807  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...
2023-12-29 15:37:04.992  9040-9089  IncomingMessageObserver W  Application level read timeout...
2023-12-29 15:37:05.023  9040-9089  IncomingMessageObserver D  [Needs Connection] Network: true, Foreground: false, Time Since Last Interaction: 423911 ms (over limit), FCM: false, Stay open requests: [], Registered: true, Proxy: false, Force websocket: truePushAvailable: false
2023-12-29 15:37:05.023  9040-9089  IncomingMessageObserver D  Reading message...
2023-12-29 15:37:34.826  9040-9630  WebSocketConnection     I  [normal:58528127] Sending keep alive...

// Here, I open the app and a conversation

2023-12-29 15:37:37.699  9040-9040  IncomingMessageObserver D  Background service started.
2023-12-29 15:37:40.665  9040-9101  WebSocketConnection     I  [unidentified:166711988] connect()
2023-12-29 15:37:41.270  9040-9761  WebSocketConnection     I  [unidentified:166711988] onOpen() connected

Edit: I've force-pushed a change, the check in mayDisconnectUnidentified wasn't correct

@p1gp1g p1gp1g force-pushed the close_unidentified_ws branch from ce18927 to 20b75db Compare December 30, 2023 08:11
@iasdeoupxe iasdeoupxe mentioned this pull request Dec 31, 2023
4 tasks
@jayb-g
Copy link

jayb-g commented Jan 1, 2024

The issue of battery drain (especially on AOSP) was there since long. But I just noticed today it now uses only 2% battery as opposed to 30+ %. I had last updated the app on 6th December to v6.41.3

Somehow seems that the issue was kind of fixed before it made it to the update. Or maybe the exact behavior of the cause still not fully known.

Issue #12341 seems to be definitely related to this. Ultimate goal should be to minimize battery drain while still not having to miss notifications.

Hopefully this fix will make it more efficient.

@jayb-g
Copy link

jayb-g commented Jan 3, 2024

After going through #12341 entirely, I now realised that it used 2% battery because I didn't leave it open that day. But mostly I do leave it open in the app drawer and only then it uses excessive battery.

@p1gp1g
Copy link
Author

p1gp1g commented Jan 3, 2024

@jayb-g : your feedback better fit in #12341, not this PR. This place is used to review the code and sometime to give feedback when testing the actual patch

@jayb-g
Copy link

jayb-g commented Jan 3, 2024

@p1gp1g Got it. Thanks.

@jayb-g
Copy link

jayb-g commented Jan 5, 2024

How does one know when this PR makes it to the final build ?

@Ronkn
Copy link

Ronkn commented Jan 20, 2024

Maybe we can just close this open issue with the resolution being uninstall signal and install Molly?

@p1gp1g p1gp1g force-pushed the close_unidentified_ws branch from 20b75db to ca93e33 Compare January 28, 2024 09:25
@p1gp1g
Copy link
Author

p1gp1g commented Jan 28, 2024

I've force-pushed a new version (co-authored with @valldrac).

This now works differently:

  • Instead of closing the unidentified websocket, it stops sending keepalives and lets the socket die if there's been no activity for few minutes in the background

As the previous patch:

  • It isn't connecting the socket until necessary. But once it's connected, it sends keepalives while (recently) in foreground
  • If the connection drops, it doesn't bother reconnecting just for sending keepalives

The previous patch can be found here: https://github.com/p1gp1g/Signal-Android/tree/close_unidentified_ws_v1

Copy link

stale bot commented Mar 30, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 30, 2024
@flxai
Copy link

flxai commented Mar 30, 2024

@p1gp1g Can you please elaborate which patch has found its way to the now distributed application?

@stale stale bot removed the wontfix label Mar 30, 2024
@p1gp1g
Copy link
Author

p1gp1g commented Mar 31, 2024

@flxai Can you clarify which distributed application are you talking about ?

@flxai
Copy link

flxai commented Mar 31, 2024

Signal for Android on the Play Store

@p1gp1g
Copy link
Author

p1gp1g commented Mar 31, 2024

Well, the pull request is still open, so nothing has been merged into the app

@jayb-g
Copy link

jayb-g commented May 3, 2024

@p1gp1g
This PR makes the app close websocket connection after some time of inavtivity(around 2min) ie doze mode. So you get no notifications at all until you open the app again. I'm assuming this PR is merged in Molly since molly currently behaves like that on websocket(for notifications) mode. This is probably not the intended solution I think, just a workaround for the time being until underlying issue is identified and resolved.

And for now it seems like using unifiedPush may be the only solution to the battery drain without GMS.

@matchboxbananasynergy
Copy link

@p1gp1g This PR makes the app close websocket connection after some time of inavtivity(around 2min) ie doze mode. So you get no notifications at all until you open the app again.

I use Molly-FOSS with websockets, and this PR is merged, and I am not experiencing any delay in notifications whatsoever.

@jayb-g
Copy link

jayb-g commented May 3, 2024

@matchboxbananasynergy 🤔 you may be right. I may have misconfigured UP which is causing that.

I use Molly-FOSS with websockets, and this PR is merged,

Does it still drain the battery ?

Copy link

stale bot commented Jul 2, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 2, 2024
@iasdeoupxe
Copy link

Most likely still relevant right?

@stale stale bot removed the wontfix label Jul 2, 2024
@Ronkn
Copy link

Ronkn commented Jul 2, 2024

Most likely still relevant right?

I'd say so. I was tagged in some other signal issue related to this so there is still discussion going on. It was a month ago or so. I provided the version of Molly that I'm using. Alternatively, my proposed solution above still stands.

This issue has been on again off again in my experience, for the past couple years. I believe there were related issues to this opened in the past but I can't be certain.

To answer your question; I believe it should stay open since I believe it's still an issue per the other issues that are discussing it. Personally, I've moved onto Molly since they fixed it right away. So this can stay open but I don't know what good it will do...

@jayb-g
Copy link

jayb-g commented Jul 3, 2024

Anyone experiencing more than 10% battery usage by signal/molly in a day, one important consideration to compare battery usage between the two: make sure to disable chat backups under settings in both signal and molly. Molly has an option to schedule it on daily/weekly basis whereas signal does not. If backups are enabled in signal it means it performs backup daily and it uses a huge amount of battery(up to 50%) daily especially if you have a lot of chats and media(I have 20+GB). It would probably consume same amount of battery for chat backups in molly.

After disabling chat backups, I have around/under 10% battery use by signal, which I think is still huge.

Copy link

stale bot commented Sep 1, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 1, 2024
@jayb-g
Copy link

jayb-g commented Sep 1, 2024

Might be still relevant

@stale stale bot removed the wontfix label Sep 1, 2024
Copy link

stale bot commented Nov 4, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 4, 2024
@randomwithnoname
Copy link

Might be still relevant

It is

@stale stale bot removed the wontfix label Nov 11, 2024
@HasBert
Copy link

HasBert commented Nov 19, 2024

Can you please turn off this fucking stale bot? This Issue still persists...

@smokemyshoes
Copy link

@HasBert Or even better, just merge the damn PR and be done with this. I's a joke that this haven't been merged yet.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

9 participants