Graphical applications go into 'Waiting' state and system can't shutdown on NVIDIA Optimus laptop

I have a Lenovo Ideapad Gaming 3i 15IAH7 laptop with RTX 3050 Mobile as a dGPU and Arch Linux installed. I’ve encountered an issue, when graphical applications (mostly ones that use OpenGL backend) can’t start and go into ‘Waiting’ state, as it’s written in btop. And also system hangs on shutdown, making hard reset the only choice.

There are 2 ways to reproduce this issue:
First way:

  1. Log into the system
  2. Plug in the charger
  3. Unplug the charger
  4. Wait 0,5-1 minute until this message appears in journalctl: kernel: NVRM: Attempting to remove device 0000:01:00.0 with non-zero usage count!
  5. Done, now any graphical application won’t open, and the system gets blocked from shutdown.

As a workaround that I’ve found on Arch Linux Forum add “pci-stub.ids=10de:2291” as a kernel parameter, which will hide the Nvidia audio chip device.
nvidia-bug-report-1.log.gz (106.2 KB)

Second way:

  1. Do the first three steps of the first way.
  2. Do systemctl suspend
  3. Wake up from suspend.
  4. Done, the same kernel message appears right after wake up.

Possible workaround may be hiding the dGPU completely, by adding “pci-stub.ids=10de:25a2,10de:2291” as a kernel parameter.

nvidia-bug-report-2.log.gz (107.2 KB)

Note: this issue can be reproduced without starting X11/Wayland server, everything can be done inside plain TTY, and the issue will appear.