Geany is not able to guarantee it will save its session before it is crashed by logout or shutdown.
There is a race between when Geany is signalled to close and when the logout/shutdown crashes Geany. That race is what means it works sometimes, but not every time, it totally depends on Geany access to the CPU, the disk IO etc in the time it has available, and remember those resources are shared with lots of other processes that you don't close, so there is no guarantee that Geany will get the resources it needs to complete saving the session.
Also it will be worse if a modified buffer is open since Geany will ask before saving it, but the user won't have a chance of responding so it just waits. Needs #3733 to remove that issue, then it may be possible to save session ahead of time so nothing needs to be saved at shutdown.
If it does not complete saving the session file may be left broken, so it gets rewritten to defaults some times.
There is no cross platform session management library at the moment despite GTK having an API for it, Gnome uses dbus, dunno what XFCE, KDE and other distros use, X11 has a session management library, and Windows and Macos are different again. At the moment applications are left with the sad choice of either specialising in a particular platform, or being cross platform and not being integrated with any session management, or using a lot of effort to support many platforms.
Geany is cross platform and does not have the effort to support each platform itself, so no session management happens and so the problem.