Repository navigation
fix(realtime): keep open connections in step with the user record - #14021
Conversation
Connections now remember the session they were opened with, and every `users.*` event re-resolves that user's open connections the way membership events already do. A connection whose session has ended, or whose user is blocked or deleted, receives that event and is then closed with a 401 error frame; its next attempt authenticates from scratch, as an HTTP request would. Co-Authored-By: Claude Fable 5.1 <[email protected]>
🟢 Tier S · Ready to mergeThe PR refreshes realtime authorization after access-changing user events, tracks session and JWT expiry, and closes connections when access ends. Impersonated connections remain bound to the impersonator’s session, and authentication metadata survives resubscriptions. Unit and end-to-end coverage exercises revocation, expiry, session extension, and impersonator lifecycle behavior. Latest changes: The newest commit adds an E2E test that verifies ping succeeds before JWT expiry, then receives a 401 error and socket closure after expiry without any outbound event triggering the close.
📂 Walkthrough · 12
✅ Fixed since the last review · 2
Reviewed the commits since |
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Connections are looked up from connection state instead of the subscription tree, so a connection opened without channels, or that dropped its last subscription, is re-resolved too. The rebuild now carries owned presences across, and the e2e helper catches the timeout subclass before its parent. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…nator An impersonated connection carries the target user's roles but was opened on the impersonator's session, so it now records who opened it (and on which session) and is re-resolved on that user's events: it ends when that session ends or that user is blocked or deleted, while the target's own events keep refreshing its roles. `subscribe()` keeps the impersonation keys across resubscribes, so the in-band authentication guard holds after a later subscribe message. Co-Authored-By: Claude Fable 5.1 <[email protected]>
The user resource authenticates x-appwrite-key + x-appwrite-user against the platform database on any project, but the impersonator binding used the connection's project unless in admin mode or on the console. A console impersonator using an account key on a custom project was bound to that project: their console block, delete or session end never closed the socket, while events for a project user with the same ID could close it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ge no access Every users.* event was flagged permissionsChanged, so a prefs or name update re-fetched the user and rebuilt roles for all of their open connections. User events that neither shape roles nor end a session are now listed in USER_EVENTS_WITHOUT_ACCESS_CHANGE and skipped; anything not listed still re-resolves, so new user events are safe by default. update.password and recovery.*.update stay re-resolving: with invalidateSessions they delete sessions without emitting sessions.*.delete. update.email and update.phone reset verification. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Creating a session can end others without emitting sessions.*.delete: the session limit hook and ID-token sign-in delete older sessions directly. Magic URL, email/phone OTP and OAuth sign-in can also set emailVerification or phoneVerification, which changes roles. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Expiry fires no event, so a connection outlived the session or JWT it was opened with until some other user event re-checked it, while the HTTP API refuses that session's next request. Connections now record when their session (`expire`) and JWT (`jwtExpire`) end; delivery skips expired connections and closes them with a 401 frame, and a per-worker tick closes expired connections that receive nothing. Extending a session moves its expiry, so `sessions.*.update` re-resolves the user's connections again and refreshes `expire`. Re-authenticating with a session cookie replaces any JWT the connection was opened with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the 60s sweep. Expiry is checked before every send instead: event delivery, the console stats broadcast and the live-tail flush. An idle expired connection stays open but receives nothing, and is closed at its next send. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The user resource verifies the JWT, then the session and jwtExpire resources decode it again. If it expired in between, they returned null and the connection lost both its session and its JWT expiry. The signature is checked before expiry, so on an expiry error their claims are read unverified and the connection is closed at its first send. Adds an e2e regression for a console live tail opened with a JWT: after expiry it delivers no tail frames and is closed with 401. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g is removed The HTTP API re-checks the impersonator flag on every request, but an impersonated socket kept the target's roles until the impersonator's session ended. users.*.update.impersonator now closes the connections that user opened while impersonating. The impersonator's events also refresh the expiry of those connections, so extending the impersonator's session extends them instead of closing them at the expiry they were opened with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…once extended Runs on its own project with the 60s minimum session duration, extends the impersonator's session, waits until an unextended session from the same moment is refused, then checks the impersonated socket still gets the target's events. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oked fix(realtime): end impersonated connections when the impersonator flag is removed
fix(realtime): close connections when their session or JWT expires
…ount-key fix(realtime): bind account-key impersonators to the console project
fix(realtime): skip re-resolving connections on user events that change no access
fix(realtime): keep impersonated connections in step with the impersonator
Expiry was checked before every delivery, but onMessage still dispatched requests from an expired connection, so a socket without subscriptions could keep writing presences and getting responses. Inbound messages now close an expired connection with the 401 frame, except `authentication`, which replaces the expired credentials with a session's. The 401 frame is built once at file scope and shared. The impersonation e2e now updates the target's labels instead of its name: name updates no longer rebuild the connection, so the step did not prove the impersonator binding survives a rebuild. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Opens a socket with a 2s JWT and no channels, so no delivery can close it, checks ping is answered, waits until the HTTP API refuses the JWT, then asserts the next ping gets a 401 frame and the socket closes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
Realtime re-resolves a user's open connections on team-membership events, but not on changes to the user record itself. This PR closes that gap so a connection reflects the same state the HTTP API would resolve for its next request.
users.*events now flagpermissionsChanged, like membership events already did, so roles derived from the user document (verification, labels, status, sessions) are refreshed on that user's open connections.store,proofForTokenandsessionresources mirroring the HTTP ones. The session a connection was opened with (cookie,x-appwrite-session, fallback cookies, or a session-bound JWT) is recorded on the connection entry and kept across resubscribes and in-bandauthentication. Impersonated connections run on the impersonator's session, so they are not bound to one of the target user's.errorframe and closed. SDKs reconnect and authenticate from their current cookie state.Realtime::subscribe()now preservesimpersonatedUserIdandsessionIdalongsideauthorizationacross resubscribes.Test Plan
tests/unit/Messaging/MessagingTest.php:users.*events flagpermissionsChanged;subscribe()keeps connection authorization state.tests/e2e/Services/Realtime/RealtimeCustomClientTest.php::testConnectionEndsWithSession: deleting another session leaves the connection open and working; deleting its own session (account API, users API, delete-all, session-bound JWT), blocking the user, or deleting the user delivers the event and then closes the socket with a 401 error frame.testChannelAccountnow expects the close that follows the password recovery, which invalidates every session of the user by default.RealtimeBase::receiveUntilClosed()reads frames until the server closes the socket, replacing sleep-and-close checks.Pint, PHPStan and Rector pass on the touched files; the Messaging unit suite passes locally. E2E runs in CI.
Related PRs and Issues
app/realtime.php.Checklist
🤖 Generated with Claude Code