@ctapobep Is it a question or you are saying that you saw the effect that you described and the code is broken? 
If it's a question, I'll tr to explain. The trick is in "totalActiveSessions.compareAndSet" - which means that decrement will succeed only if the value has not changed in meantime, guaranteeing the expected result. In combination with the loop and a check that the value didn't go below 1, it ensures the session counter holds correct value. Let me know if you have more questions?