Skip to content

Fix terminate sessions - #24

Merged
Ataba29 merged 3 commits into
mainfrom
fixTerminate_Sessions
Jul 27, 2026
Merged

Fix terminate sessions#24
Ataba29 merged 3 commits into
mainfrom
fixTerminate_Sessions

Conversation

@Ataba29

@Ataba29 Ataba29 commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Related to issue #21

Description

Idle-timeout cleanup closed client sockets directly from UserSession::terminate_session(), on the background cleanup thread, without ever notifying Server. This left Server::connections and the event loop holding stale entries for sockets that were already closed elsewhere. If the OS reassigned that fd number to a new client before Server noticed, 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() called remove_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. UserSessionManager now takes an on_session_expired callback, invoked from cleanup_expired_sessions() before a session is erased, so Server learns about the expiry. Since that callback fires from the background cleanup thread and connections/eventLoop are only ever safely touched from the event-loop thread, the callback just queues the socket (Server::onSessionExpired, guarded by expiredMutex) rather than closing anything directly. runEventLoop() drains that queue each iteration and calls closeConnection() for each one, keeping all connections/eventLoop mutation on a single thread.

stop() also changed: since UserSession no longer self-closes, close_all_sessions() at shutdown no longer closes any fds, so stop() now explicitly closes every live connection before clearing connections and tearing down sessions.

Changes

UserSession.h/UserSession.cpp - added on_session_expired callback member and setter, added get_socket(), terminate_session() no longer closes the socket, cleanup_expired_sessions() invokes the callback before erase.

Server.h/Server.cpp - added expiredMutex/expiredSockets, added onSessionExpired(), 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, onSessionExpired queued the socket, and the event-loop thread's drain picked it up and called closeConnection on 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.

@Ataba29 Ataba29 added bug Something isn't working enhancement New feature or request labels Jul 26, 2026
@Ataba29 Ataba29 mentioned this pull request Jul 26, 2026
10 tasks
@Ataba29

Ataba29 commented Jul 27, 2026

Copy link
Copy Markdown
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

@razimograbi razimograbi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job

@Ataba29
Ataba29 merged commit 4e26e86 into main Jul 27, 2026
@Ataba29
Ataba29 deleted the fixTerminate_Sessions branch July 27, 2026 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants