Is there a preferred way of restarting X after getting stuck on the desktop despite not logging in due to disconnecting a monitor?

When I disconnect the external monitor on my Xubuntu 20.04 laptop, I occasionally experience some misbehavior on my main monitor. For example, I recently disconnected my external monitor while the machine was sleeping. Upon waking it, I had a fraction of a second to try to log in before I was sent to my desktop screen with only the ability to move the mouse. pkilling my session with another tty does the trick, as would REISUB, but this seems extreme. Keyboard shortcuts on the main tty, such as Super+L to lock the screen and get me back the the log-in screen, appear to get ignored.

Ultimately, my question is this: If my XFCE4 X session is misbehaving and is ignoring most keyboard inputs, what's the preferred way to restart it? My current means, e.g. pkill seem like I'm turning to the weapons of last resort as a first resort.


Solution 1:

This problem might be related to the bug report Session crashes when monitor is disconnected. This bug report was marked as duplicate of another bug report: Artful (17.10) Session logout after screen turned off, which was closed after the poster solved his problem by moving to Wayland.

As regarding unfreezing an X session, here is a summary from the article
4 Ways to Recover From a Crashed or Frozen X Server on Linux.

1. Ctrl+Alt+Backspace

The Ctrl+Alt+Backspace keyboard shortcut that traditionally restarted the X server is disabled by default. To enable it on a GNOME-based desktop, use the Keyboard Layout utility. Click the Options button and enable the "Control + Alt + Backspace" check-box under "Key sequence to kill the X server".

2. Restart the X server from terminal

To restart the X server, log into a virtual console and run the command:

sudo service lightdm restart

3. SSH into computer

If you can't log into a virtual console, and the SSH server is active, you may be able to enter the above restart command from another computer.

4. The SysRq key

If the X server has taken control of the keyboard, to take it back use the following key combination: Alt+SysRq+R.
Now you could try pressing Ctrl+Alt+F1 to access a virtual console.