What is the safest way to shut down a Mac which is stuck at the screensaver?
A couple potential ideas:

1. Try to avoid the Screen Saver Bug: Turn off the screen saver (set it to “Never”) and just adjust the Energy Saver to turn the screen off to prevent burn-in. You can still lock the screen with ⌃⌘Q

2. Alternate Login Methods: If allowed, turn on Screen Sharing and/or Remote Login in System Preferences » Sharing and see if you can login with your phone using ssh or vnc (I recommend Prompt and Screens, respectively).

3. Sleep Instead Of Lock: Enable a “Hot Corner” to put the Mac to sleep instead of just locking it with the screen saver. Not as convenient as just locking it and walking away, as it will take a little longer to wake from sleep, but if it doesn't trigger the bug, it would be worth it, perhaps?