I downgraded the kernel using the following command:
sudo rpi-update 8bd0d75837d6a99138c7cbbc24888601b220405a
Now I am running:
Linux raspi5 6.6.77-v8-16k+ #1851 SMP PREEMPT Thu Feb 13 13:02:57 GMT 2025 aarch64 GNU/Linux
With this kernel version, the script is running stable again.