Update:
I can consistently reproduce the bug by rapidly turning night light on and off for 30 seconds (tested with Ubuntu 24.04, Wayland, nvidia-driver-555). I use my laptop like a desktop with the laptop screen off and an external monitor attached. I have since had both screens on and it turns out that only the external monitor is affected, while the built-in display still works.
This issue has been reported before: https://forums.developer.nvidia.com/t/external-monitor-freezes-when-using-dedicated-gpu/265406
It also turns out that the “flip event timeout on head 0” does not appear in the dmesg outout directly after the freeze, but after switching virtual terminals to unfreeze. However, if I switch virtual terminals without the screen having first frozen, then I do not get the “flip event timeout on head” message.