Screen sharing stops working after disconnecting for about five minutes

I'm running macOS Mojave on a Mac Mini. Built-in VNC server works perfectly when you connect for the first time after reboot, however if you disconnect from it for more than five minutes you can't connect again - it displays the username that was used for the last session and a spinning wheel. Connecting via ssh and killing screensharingagentd doesn't help, only a reboot does. I've verified all the power saving settings are off. There are two virtual machines with macOS High Sierra and Sierra running on this same Mac Mini and I can connect to them without any issues.


My workaround for this problem is to login via ssh and kill loginwindow process:

sudo pkill loginwindow

This will allow to login via VNC then.


With the iMac at work, we had to turn OFF the option under System Preferences - Sharing - Screen Sharing - Computer Settings to allow VNC viewers may control screen with password. Then VNC Viewer prompts for the username & password of the iMac (assuming Only these users: is selected for Allow access for). Multiple connections work fine with that setting disabled. Hope this helps.