In my case, it was because Unity switched off Debug mode. To re-enable debug mode so the editor will attach, you can
In Visual Studio
Debug -> Attach Unity Debugger
(this will open a pop-up; select your Unity instance)
Unity will warn you that you are trying to switch to debug mode, say yes for this session, and now Visual Studio will be able to attach (until you restart Unity. You can set it to all projects, if you want the perf hit to always have the debug mode on).
In Unity
At the bottom left corner, there is this bug-looking icon (the furthest to the left). You can click on that to enable Debug Mode