OSX Locking then logging out
I have experienced similar problems with my MacBook Pro when I drop my earphones near the left side of my keyboard. I believe the MacBook uses magnets to detect the screen lid is closed, so when the magnetic field of the earphones get near the Macbook case it thinks the screen was closed and locks the system.

I have ever since placed my earphones somewhere else, far from the MacBook case and never had this problem again.

Check if you don't have any strong magnetic fields near your computer, even if a bit far from it.