Hibernation audio issues

I can’t seem to find a single other person online that has had this issue.

my audio buffer doesn’t clear before hibernating. so the last quarter second or so of audio just plays on repeat until it’s done moving things to swap memory and then shuts down.

I have a framework laptop 16 if that matters.
my configuration can be found here: https://github.com/spydragon/ForestDragon-Nixos-Setup/tree/main/modules (hibernation settings are located in the power module folder… or at least I hope all of them are there…)
I have tried to solve this issue twice with AI help (gemini) though I only have one conversation to share: https://share.gemini.google/eTfJRFuDSe62

I just want this issue to be dealt with once and for all.

edit: I forgot to say that I’m on unstable and updated just today. but this issue have been plaguing me for a long time.

Did you look at nixos-hardware ? They usually cover these kinds of issues

There seems to be one for framework laptops:

FWIW i’ve had similar luck as @QuBe with nixos-facter.

real-world example: i run nixos on an old mac x86 laptop, and find that this config sorts a handful of suspend/resume issues that affect restoring the wifi adapter and sudden escape from sleep states that otherwise occur(ed at one point.) HTH.

hmm, i think my laptop actually does this as well, but i never really bothered trying to fix it cause it didn’t really bother me. It does sound pretty goofy though when it happens lol

I use nix hardware, and I do use the framework one from there. Hence why I believe this isn’t a framework specific issue

fascinating. discourse tells me it’s been 4 months since your last post, @MatsuNoKi. that’s a pretty good trick.

in any case, i’m just kind of curious, and trying to better understand the issue you’re experiencing. when you say

audio buffer doesn’t clear … so the last quarter second … of audio just plays on repeat

has the audio playback / monitoring stream been manually paused or stopped at that point, or you’re going for suspend/hibernate with tunes playing or somesuch?

Basically the second.

When media is playing and then hibernate is activated, the last quarter second of audio repeats. If I pause before hibernate then there is no issue.

Because of that, I actually did try to make systemd pause before hibernation but it either didn’t work or I had no idea what I was doing. Admittedly it’s probably the latter.

I still haven’t figured this out.

I hate how often this jump scares me.

i’m sympathetic, yet at the risk of being a bit direct, i might suggest the easy button: muting, pausing, or stopping the audio playback on your machine before asking it to freeze all memory and go into suspend. (advice coming from an audio engineer)

what you’re describing is not a nixos or linux-specific problem. i’ve experienced it on windows and macos as well.

if this is something you’re really inspired to chase, perhaps check out the powerManagement.powerDownCommands option.

For this specific computer, I have never experienced this issue on windows. Not to say you’re wrong, I would just like more evidence than what you provided.

I would also very much like to reiterate that this is on a laptop which I personally decided to make it hibernate when it shuts off from low power, and so the mute suggestion does not work.

I would need to double check this last point but I am very confident that this hibernation issue is something that I did not face with other distros on the same device.

I do not totally get what you have already tried. What about those powerDownCommands?

If you believe it’s not what you are looking for, perhaps try writing a simple systemd unit that runs before and after hibernation exactly, not on regular boot events. I have been using the following for several years now in order to fix the annoying touchpad disappearing issue on my HP after any type of suspension.

# Fix touchpad hang after waking up
  systemd.services.rtouchpad = {
    description = "Unload touchpad module before sleeping";
    enable = true;
    wantedBy = [ "suspend.target" "hibernate.target" "suspend-then-hibernate.target" "hybrid-sleep.target" ];
    before = [ "suspend.target" "hibernate.target" "suspend-then-hibernate.target" "hybrid-sleep.target" ];
    unitConfig = {
      # Tells systemd that this service is related to sleep events
      StopWhenUnneeded = true;
    };
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "${pkgs.bash}/bin/bash -c '${pkgs.util-linux}/bin/logger rtouchpad: suspending, unloading psmouse && ${pkgs.kmod}/bin/modprobe -r psmouse'";
    };
  };
  systemd.services.atouchpad = {
    description = "Inject touchpad module after resuming";
    enable = true;
    wantedBy = [ "suspend.target" "hibernate.target" "suspend-then-hibernate.target" "hybrid-sleep.target" ];
    after = [ "suspend.target" "hibernate.target" "suspend-then-hibernate.target" "hybrid-sleep.target" ];
    unitConfig = {
      # Tells systemd that this service is related to sleep events
      StopWhenUnneeded = true;
    };
    serviceConfig = {
      Type = "oneshot";
      ExecStart = "${pkgs.bash}/bin/bash -c '${pkgs.util-linux}/bin/logger atouchpad: resuming, loading psmouse && ${pkgs.kmod}/bin/modprobe psmouse'";
    };
  };

The workflow is similar. Adjust the commands to match your sound modules (as Gemini pointed out) and try again. That will work in any case exactly before the hibernation, AFAIK.

Have you tried simplifying your audio config to remove the extra pulse configs and keep it simple

I simplified and it fixed a latency problem i had when playing audio on YouTube

services.pipewire = {

enable = true;

pulse.enable = true;

};

Also in relation to hibernation

How much ram do you have vs your actual swap size?

ok, gotcha. that’s the closest you’ve come to explaining what it is you’re trying to accomplish and why. :wink:

@kekneus373 provided additional options and as @stefan-hacks pointed out, using pipewire as your soundserver (instead of a combination of pulse, ALSA, JACK, or other “legacy” frameworks) will help set you up for the present/future of where linux audio is heading, and may also help you mute connected audio device sinks (“outputs”) in a portable fashion via automation.

for example, you may have wireplumber’s wpctl CLI (WP is a Pipewire session/settings manager) run a command such as this in the powerDownCommands option or a systemd service, as suggested before:

bash -c 'wpctl list audio sinks | while read id dev type default ; do wpctl set-mute $id 1 ; done'

it’s still not clear to me exactly what you’re trying to pull off here and what attributes would constitute success.

please be careful, and aware of the fact that while the suggestions given may “mute” the audio signal coming from your audio device via software control of the mixer, in most cases this sort of approach cannot and will not prevent a massive thump (a power transient) coming through any connected amplifier and speakers at the point in time the laptop drops power to the audio interface, unless the audio interface in use contains anti-thump/anti-transient circuitry.

the fact that you’ve described this as “jump scaring you” (sic) leads me to believe it might be happening at high volume or with significant amplification, in which case that power-down thump can be an outboard equipment ender.

HTH.

OK had a look again through your ai Gemini help and power options and I think Gemini firstly is not the solution for you. Alot of its approaches whether your audio or power and other modules are convoluted, soon to be deprecated, out of date.

I suggest since you are using nixos. Start clean i would nuke your repo to be fair. It definitely needs an update or refresh.

If you are working from your home directory that’s great.

If you are working from /etc/nixos then move everything and and regenerate a clean nixos config.

Rebuild and switch to it.

Then maybe post your experience with the generated default and look to implement each feature module one at a time. What I would say is that we are always here before you get lost down the ai wormhole I recommend enabling allowunfree and since you are familiar with flakes enable them as well and build out a highly focused build that actually works at each step. Implement a change test it then move onto the next.

You can search for up to date options and packages using nh tool or directly on nixos.org

No shame in using Ai i did it at first and learnt the hard way like you. Its not the best advice for nixos. But it’s great for one off small troubleshooting your working config.

thank you for reiterating the possible power-down implications, @emilyjackson.

this has been something of a meandering :thread: and I don’t know for a fact that an external amp/speakers are connected to the system in question.

IIUC @MatsuNoKi’s original concerns focused primarily on (what sounds like) the audio playback buffer freezing and looping when suspend/hibernate is triggered during active playback of an audio stream - which may occur as a side-effect of a low-battery condition.

I think we’ve all tried to offer actionable advice to help mitigate that issue, while also noting that:

  • stopping (or muting) playback before suspending (whether manually or via automation) may alleviate audio playback going completely berserk.
  • the described behavior is not (really) a “linux” or “nixos” problem, but a function of hardware, drivers, software, sound server, and state of the system prior to entering a power-saving state.
  • the annoyance of a looping audio buffer may not be the only or most critical “challenge” to consider w.r.t. order of operations when powering down a computer and audio interface that might be connected to external equipment, with risk of equipment damage as a consequence.

i’m hoping all of this gives OP some context to think about their particular situation and choose a way forward that addresses their concerns and protects their equipment.