Fix terminate sessions - #24
Merged
Merged
Conversation
Owner
Author
|
@razimograbi I need this reviewed so i can add it, it will fix a bug that i caught in another PR which is #30 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related to issue #21
Description
Idle-timeout cleanup closed client sockets directly from
UserSession::terminate_session(), on the background cleanup thread, without ever notifyingServer. This leftServer::connectionsand the event loop holding stale entries for sockets that were already closed elsewhere. If the OS reassigned that fd number to a new client beforeServernoticed, the stale entry could collide with the new connection, causing traffic to be misattributed to the wrong session.This also uncovered a related double-close:
Server::closeConnection()calledremove_session()(which triggered~UserSession()->terminate_session()->CloseSocket) and then closed the same socket again itself, on every normal disconnect.The fix makes
Server::closeConnection()the sole owner of socket closure.UserSession::terminate_session()no longer closes the socket.UserSessionManagernow takes anon_session_expiredcallback, invoked fromcleanup_expired_sessions()before a session is erased, soServerlearns about the expiry. Since that callback fires from the background cleanup thread andconnections/eventLoopare only ever safely touched from the event-loop thread, the callback just queues the socket (Server::onSessionExpired, guarded byexpiredMutex) rather than closing anything directly.runEventLoop()drains that queue each iteration and callscloseConnection()for each one, keeping allconnections/eventLoopmutation on a single thread.stop()also changed: sinceUserSessionno longer self-closes,close_all_sessions()at shutdown no longer closes any fds, sostop()now explicitly closes every live connection before clearingconnectionsand tearing down sessions.Changes
UserSession.h/UserSession.cpp- addedon_session_expiredcallback member and setter, addedget_socket(),terminate_session()no longer closes the socket,cleanup_expired_sessions()invokes the callback before erase.Server.h/Server.cpp- addedexpiredMutex/expiredSockets, addedonSessionExpired(), wired the callback in the constructor,runEventLoop()drains the queue each iteration,stop()now closes all live connections itself.Testing
Verified manually with an idle client connection over ncat: inserted a key, ran a GET, then left the connection idle past the timeout with no further traffic. Server log confirms the full path: the background worker's periodic idle check found the expired session,
onSessionExpiredqueued the socket, and the event-loop thread's drain picked it up and calledcloseConnectionon it. Client side saw the connection forcibly closed by the server, matching expected behavior.NOTE
The ncat that we are using forcibly closes an idle connection on its own; it's built in, so to check the logic behind the code, I had to set idle time to 30s instead of 240s.