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

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

@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
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 ?

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

Successfully merging this pull request may close these issues.

None yet

5 participants