welcome to the forum!
I’m afraid you are correlating three unrelated things. First of all, the fact that you have to enter your password to unlock the screen after you’ve left the keyboard unattended for a while is not a login. You are still logged in, but it is simply a screen locker.
Secondly, the phenomenon that follows later is not the system logging you out either, but instead it is your X11 session that’s crashing for some reason, and by default, the system will then try restarting the X11 server. Of course, that does mean that…
- you are forcefully logged out (because your GUI session has committed suicide); and
- you will have lost whatever it is you were working on.
So what we have to figure out here is what causes your desktop to crash. And that could be any number of things, including but not limited to third-party applications ─ including games.
Alas, given that our crystal ball is currently undergoing maintenance, only you know what it is that you were doing and what may have triggered that crash.
If you want us to help you fix the problem ─ if it is fixable ─ then you’re going to have to provide us with more information. For starters, you haven’t even mentioned what desktop environment you’re using.