[HowTo] Avoid random freezing while the audio continues to play

Randomly the video is frozen and the PC does not respond to anything while the audio continues to play. A reset is necessary.

The problem is related to the default I/O scheduler used by kernels to manage the HDDs and its short timeout default value. I have tried different HDDs with the same result, but the system works fine if I unplug them.

You can see the used scheduler and those available for the current kernel with:

cat /sys/block/sd*/queue/scheduler

On kernel 4.14 and earlier:

The solution is to assign "CFQ" scheduler to the rotational disks. For this you have to create/edit the file:

sudo nano /etc/udev/rules.d/60-ioschedulers.rules

And paste in it:

# set scheduler for rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"

Captura de pantalla de 2020-06-17 10-53-19

Then reboot.

* 4.14 is the last kernel that supports "cfq".

On kernel 4.19 and later:

The solution is to assign "BFQ" scheduler to the rotational disks. For this you have to create/edit the file:

sudo nano /etc/udev/rules.d/60-ioschedulers.rules

And paste in it:

# set scheduler for rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="bfq"

Captura de pantalla de 2020-06-17 10-53-19

Then reboot.

With BFQ you also have to raise the "timeout_sync" default value from 124ms to 200, e.g. with:

echo '200' | sudo tee /sys/block/sdX/queue/iosched/timeout_sync

* You can try higher values if the problem continues.

But this setting is lost on reboot, so we have to create a script:

sudo nano /usr/bin/hdd-timeout.sh

With the content:

#!/bin/bash
echo '200' | tee /sys/block/sd[a-z]/queue/iosched/timeout_sync

Captura de pantalla de 2020-06-17 13-37-16

And we make it executable:

sudo chmod +x /usr/bin/hdd-timeout.sh

Now we create a new systemd service for to launch it on every boot:

sudo nano /lib/systemd/system/hdd-timeout.service

With the content:

[Unit]
Description=Tunning BFQ timeout for HDDs.

[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/hdd-timeout.sh

[Install]
WantedBy=multi-user.target

Captura de pantalla de 2020-06-17 10-54-57

We start up and enable it:

sudo systemctl enable --now hdd-timeout

We can check if it works well by consulting the value of "timeout_sync":

cat /sys/block/sdX/queue/iosched/timeout_sync

That is nicely done, except I see one potential problem and that is that drive assignments sometimes change. Using "sdX" in your script may change from the original "sdb" to "sde" if multiple drives are being juggled (as I often do).

Perhaps using something like "sd[a-z]" in your script might be more universally applicable.

I have found juggling schedulers to be necessary in the past as well.

Nicely done, documenting your findings.

1 Like

You're right, and it's something I hadn't thought of.

The problem is that all my drives are "sdX" type including SSD, so I guess the timeout value would be applied to all of them. I don't know if there's any other unambiguous way to identify the unit...

Thank you.

1 Like

Ok, I've checked that none of the other available schedulers have that setting, so I guess it's safe to apply your suggestion.

Thank you very much for your contribution.

1 Like

Forum kindly sponsored by