Unigram icon indicating copy to clipboard operation
Unigram copied to clipboard

Unigram keeps playing audio, prevents Modern Standby devices from entering DRIPS sleep

Open fefrei opened this issue 3 years ago • 7 comments

Describe the bug In some cases, Unigram appears to keep an audio stream open despite nothing happening, causing Modern-Standby-enabled Windows devices to drain the battery during sleep.

To Reproduce Steps to reproduce the behavior:

  1. Open Unigram on a device that uses Modern Standby.
  2. Use Unigram for a while. At the end, ensure Unigram is not playing audio (e.g. an audio file you received).
  3. Put your device into modern standby.
  4. Wait multiple hours.
  5. Wake your device.

Expected behavior Unigram should prevent the device from entering DRIPS, the most power-saving Modern Standby state, only when the user has triggered continuous audio playback.

Screenshots After a Modern Standby session, powercfg /sleepstudy can be used to create a sleep study report.

The sleep study shows how my device drained 31% of its battery for a 5 hour sleep session, not spending any time in DRIPS:

image

The sleep study also analyzes “offenders”, i.e. reasons why the device was kept from entering DRIPS. For this sleep session, the only relevant offender is the Audio Service activator, which claims it had to keep the device in a high-power state for two reasons: The Shell Experience Host was playing audio for 1 second, and… Unigram played audio throughout the whole sleep session:

image

Version Info

  • Unigram: 8.8 (7438) X64
  • Windows: Windows 11, Version 21H2, OS Build 22000.832

Additional context I am experiencing this problem on a Surface device.

I don't recall intentionally playing any audio in Unigram during that session (no voice messages etc.), so I think the only reason for Unigram to open any audio stream was notification sounds. Unigram was in the background when I entered sleep.

fefrei avatar Aug 09 '22 06:08 fefrei

I don't think this is due to notifications sound... I will investigate this further

FrayxRulez avatar Aug 29 '22 19:08 FrayxRulez

Thank you so much! I've had to stop using Unigram because of this for now – reliable sleep is, in the end, more important than a better Telegram client.

If there's anything I can do to help, let me know. (Maybe I can try to catch a memory dump of Unigram after such an energy-eating sleep session?)

fefrei avatar Aug 30 '22 11:08 fefrei

Could you please explain me the repro steps? Such as:

  • Window was active
  • Window was minimized
  • Actions performed in the app

As far as I can understand you can reproduce this issue quite well.

FrayxRulez avatar Aug 30 '22 11:08 FrayxRulez

Unfortunately, I don't know reliable repro steps. Determining whether the issue is there requires about 15 minutes of sleep, so it is really hard to test systematically. In practice, I usually have Unigram open all the time, often sitting minimized to the taskbar, sometimes just sitting behind other (fullscreen) apps. I occasionally interact with it – just writing and receiving messages and stickers, I typically don't receive video (and audio is an absolute rarity) – but most of the time, it's sitting minimized or in the background. When I sleep my device, it often sleeps absolutely fine (loosing maybe 1% battery a day). When the bug hits me, the Surface churns through battery. Every time I noticed this and checked the sleep study report, Unigram was the activator that kept the device awake.

fefrei avatar Aug 30 '22 11:08 fefrei

#2625 has a comment saying it's the tray icon that causes the problem. Disabling it seems to fix it for me.

trs80 avatar Sep 04 '22 16:09 trs80

Thanks for the comment, @trs80. I guess that disabling the tray icon can be a good workaround. It, in fact, prevents the app from being suspended, and this is by design.

FrayxRulez avatar Sep 04 '22 16:09 FrayxRulez

Thank you! (And sorry that I did not find that other ticket.) I disabled the tray icon and resumed my use of Unigram – I'll report back if I get another battery drain.

fefrei avatar Sep 04 '22 18:09 fefrei

Recently did quite a lot of changes to audio stuff in the app. Does the problem still occur?

FrayxRulez avatar Oct 25 '22 19:10 FrayxRulez

Thank you! I updated to the most recent version of Unigram available in the store and re-enabled the system tray icon. I'll monitor the situation and let you know if another drain occurs.

fefrei avatar Oct 26 '22 08:10 fefrei

Oh, right, sorry, it's about system tray icon. Having that, there's no way that modern standby can possibly work. There's no way around this.

FrayxRulez avatar Oct 26 '22 08:10 FrayxRulez

Huh, weird! I have other apps that show tray icons, can play audio, and do not cause this battery drain. (Looking at the sleep study, it does not look like the tray icon itself is the blocker, but the system thought that Unigram was continuously playing audio.)

fefrei avatar Oct 26 '22 09:10 fefrei

@fefrei Unigram is not a normal app, because UWP apps don't support tray icon. So this is done by an hack, and there's no other way around this.

FrayxRulez avatar Oct 26 '22 09:10 FrayxRulez

That's really unfortunate. Thank you for investigating!

fefrei avatar Oct 26 '22 09:10 fefrei

This just happened to me. Drained my laptop to 0% overnight. image

Prior to this, sleep mode has been fine even with Unigram open.

I don't think this is because of the tray icon. Something is playing audio and not closing the audio context, most likely related to notifications or reactions.

ds84182 avatar May 21 '23 22:05 ds84182

@ds84182 It’s actually the system tray icon, you can disable it and you will see that your laptop will sleep as expected :) (There’s no audio involved with notifications or reactions)

FrayxRulez avatar May 21 '23 22:05 FrayxRulez

@ds84182 I’m saying this with some confidence because UWP apps (UWP is the technology used to build Unigram) are not allowed to run in the background and to have a tray icon, so we do this by using an hack that consists in spawning a classic win32 process that hosts the tray icon and forces the main process to stay open (as is: it launches it again as soon as it gets terminated)

FrayxRulez avatar May 21 '23 22:05 FrayxRulez

No, it's definitely audio related. Older versions of Unigram depended on C# GC to finalize AudioGraphs used for the message send sound, etc. Of course GC finalization is nondeterministic so if you send a telegram message and then put your laptop to sleep, this happens.

ds84182 avatar May 22 '23 02:05 ds84182

@ds84182 But now AudioGraphs are disposed as soon as the stream has been played. Having them as weak references caused a huge amount of crashes, so this had to be changed.

FrayxRulez avatar May 22 '23 06:05 FrayxRulez

@ds84182 But now AudioGraphs are disposed as soon as the stream has been played. Having them as weak references caused a huge amount of crashes, so this had to be changed.

Right. And having them as weak references caused this bug because they weren't being explicitly disposed. I'm not sure if the latest build has the fixes in it but I've just updated Unigram today. Will report back if I encounter this again.

ds84182 avatar May 22 '23 07:05 ds84182

@ds84182 Unigram doesn’t use weak references since a while, since before yesterday for sure 😁

FrayxRulez avatar May 22 '23 07:05 FrayxRulez

@FrayxRulez Would be glad to know that AudioGraph is actually the issue to be honest, but I’m quite confident that this isn’t the case 🥲

FrayxRulez avatar May 22 '23 07:05 FrayxRulez

Reporting back, haven't hit the bug since updating so the AudioGraph refactor fixed this! Also fwiw this isn't typically a problem for UWP apps unless they declare background audio perms, which Unigram does iirc

ds84182 avatar May 29 '23 02:05 ds84182

Closing this

FrayxRulez avatar Jun 05 '23 12:06 FrayxRulez