Skip to content

refactor: one shared connection authority for long-lived connections - #422

Open
hhvrc wants to merge 12 commits into
developfrom
refactor/connection-authority
Open

hhvrc wants to merge 12 commits into
developfrom
refactor/connection-authority

Conversation

@hhvrc

@hhvrc hhvrc commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Why

A long-lived connection is authenticated once and then holds its own copy of what it may do. SignalR hub connections, live control sockets and hub sockets each kept that copy current in their own way: their own Redis handlers, their own periodic check, their own ordering logic and their own copy of the rules. The copies had drifted, and each fix (#407, #416, #417, #421) added another one.

This replaces them with one component.

What

  • Common/ConnectionAuthority: a registry of the connections open on an instance, indexed by what they depend on, and a single GrantResolver that decides whether a connection may go on and what it may do. A connection looks again when it registers, when a change that can affect it is announced, and when time has passed. One look runs per connection at a time and a request during one makes it run once more, so an older read cannot replace a newer one.
  • Live control reads its grant from the registry on every frame. The token subscriber, the revocation subscriber, per-connection revalidation and the generation counters are removed.
  • SignalR hubs go through the same registry. HubConnectionTracker and the API's revocation handlers are removed. The user hub reads a token's pause state and limits from the connection's grant.
  • Hub sockets are now checked after they connect (new behaviour, see below).
  • One Redis channel, authority-changed, replaces session-revoked, user-auth-revoked, api-token-update:* and public-share-access-revoked.
  • Reconcile on reconnect: when the Redis subscription connection is restored, open connections are checked again instead of waiting for their periodic check.
  • One-off control and live control share one query for who may control which shocker. The public share hub and the resolver share one definition of a share a visitor can still reach.

Behaviour changes

  • A hub's socket is closed when the hub is deleted, its token is regenerated or its owner is deactivated. Regenerating a hub token now announces a hub update.
  • Renaming an API token no longer closes its hub connections. A change to its pause state, permissions or limits still does.
  • An idle hub connection whose token expired is closed instead of staying open until its next call.
  • A share or shocker edit no longer delays control commands queued behind it on the hub's channel.
  • Lookups are capped at 32 at a time per instance.
  • The live_not_enabled frame outcome is removed from the gateway metrics. Nothing ever recorded it.

No routes change.

Rollout

During a rolling deploy, instances on the old version publish on the old channels and are not heard by new ones. A connection that misses a change that way finds out at its next periodic check (one minute by default).

Testing

Relation to #421

This branch is built on #421 and contains its commits. Merging this makes #421 redundant.

Summary by CodeRabbit

  • New Features
    • Live-control and hub connections now recheck access when credentials, sessions, shares, or device permissions change, and close when access is no longer valid.
    • Shared control reflects the applicable pause state, permissions, and limits. Connections can remain open when there are no shockers available to control.
  • Bug Fixes
    • Guests can no longer connect to public shares whose owner is deactivated.
    • Device-token regeneration and changes to share or account access are reflected in active connections.

hhvrc added 11 commits December 5, 2026 11:36
A live control socket was authenticated once, when it opened. When its API token changed, the gateway refreshed the pause state and limits but never checked that the token could still control, so a token that lost shockers.use kept working. Expiry was only noticed if some token change happened to be announced, and a logged out session or a deactivated account kept its open socket too.

The gateway now closes a live control socket when its token is deleted, expires, loses shockers.use or its user is deactivated, and when its session is deleted or its account deactivated, including sockets controlling a deactivated owner's hub through a share. Token expiry is enforced on every frame and on the ping. As a fallback for a lost message, each socket re-checks its token or session, the account and its share permissions at most once per revalidation interval, and once right after connecting.

Also adds regression tests for share control while the device owner is deactivated.
The API and Cron integration test processes pull the same images in parallel; one can be told the pull succeeded and then fail container creation with 'No such image'.
Token state and shocker permissions are refreshed by several writers (the token subscriber, hub updates and periodic revalidation) that each read the database and then install the result. A read that started earlier but finished later could overwrite a newer pause or share removal until the next revalidation. Each read now takes a generation before it starts, and a result is installed only if no later read has already installed its own. Permissions are kept as a FrozenDictionary that is swapped whole, so frames read it without a lock.

Revalidation also checks that the owner of a shared hub is not deactivated, so a lost owner revocation message still closes recipients' sockets.

CI: wait on each image pull separately so a failed pull fails the step.
Restores ci-build.yml to its state at the branch point; the change does not belong in the live control revocation fix.
A long-lived connection is authenticated once and then holds its own copy of what it may do. Each kind of connection kept that copy current in its own way, with its own Redis handlers, its own periodic check and its own copy of the rules.

This adds one component for all of them. A registry tracks the connections open on an instance by what they depend on, and a single resolver decides whether a connection may go on and what it may do. A connection looks again when it registers, when a change that can affect it is announced, and when time has passed. Only one look runs per connection at a time and a request during one makes it run once more, so an older read cannot replace a newer one.

Nothing uses it yet. One-off control and the resolver share one query for who may control which shocker.
Live control reads what it may do from its registry handle on every frame. The token subscriber, the revocation subscriber, the per-connection revalidation and the generation counters that ordered their writes are removed, as the registry does all of it.

A hub update now signals the registry instead of refreshing each live control connection in turn, so control commands queued behind it on the hub's channel no longer wait for those database reads.

Also gives a live control connection one connection id instead of a new one on every read.
…hority

The hub filter registers connections with the registry and makes sure a connection was checked recently before a call. The hub connection tracker and the API's own revocation handlers are removed.

A hub connection is still closed when its API token's pause state, permissions or limits change. Renaming a token no longer closes it, and an idle connection whose token expired is now closed instead of staying open until its next call.

The API's Redis subscriber only unsubscribes its own channels on shutdown, as it now shares the Redis connection.
A hub was only checked when it connected. A deleted hub, one whose token was regenerated or one whose owner was deactivated kept its open socket.

The hub socket is now under the shared connection authority and is closed in those cases. Regenerating a hub token announces a hub update so the gateway holding the old connection sees it.
A deleted session, a deactivated account, a changed API token and a public share whose access changed were each announced on a channel of their own, with a handler of their own on every receiver.

They are now one message on one channel that names what changed. A new reason for a connection to look again is one publish call and needs no new subscriber. A message that cannot be read makes every connection look again, as what changed is unknown.

Hub changes stay on the hub's own channel, which the gateway holding the hub already listens on.

During a rolling deploy, instances on the old version publish on the old channels and are not heard by new ones. A connection that misses a change that way finds out at its next periodic check.
Redis pub/sub delivers a message only to who is listening at that moment. An instance whose Redis connection was down heard no changes, and its connections only found out at their next periodic check.

When the subscription connection is restored, live control connections and hub sockets are now checked at once, and hub connections on their next call.
Renames what still carried the names of what it replaced: the hub filter, the account revocation notifier and the test setting for the check interval.

A connection's session id is derived from its session token instead of being passed alongside it. Connections accept any grant unless they say otherwise, and both gateway controllers close a revoked socket through one helper.

The user hub reads a token's pause state and limits from the connection's grant instead of the token it connected with. The public share hub and the resolver share one definition of a share a visitor can still reach.

Removes a live control result that nothing returned, and its metric label.
@hhvrc hhvrc self-assigned this Dec 5, 2026
@hhvrc
hhvrc requested a review from LucHeart as a code owner December 5, 2026 18:42
@hhvrc hhvrc added the feature New feature or request label Dec 5, 2026
@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

Note

CodeRabbit ASCII loading animation

Currently processing new changes in this PR. This may take a few minutes, please wait...

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c62dda9f-9ec7-40ba-9779-b9aec5db41e4
Commits

Reviewing files that changed from the base of the PR and between a3e2097 and b9c0479.

Files selected for processing: 48

  • API.IntegrationTests/Tests/GrantResolverTests.cs
  • API.IntegrationTests/Tests/PublicSharePasswordTests.cs
  • API.IntegrationTests/Tests/ShockerPermissionsTests.cs
  • API.IntegrationTests/Tests/SignalRUserHubTests.cs
  • API.IntegrationTests/WebApplicationFactory.cs
  • API/Controller/Devices/DevicesController.cs
  • API/Controller/Shares/Links/DeletePublicShare.cs
  • API/Controller/Shares/Links/SetPublicSharePassword.cs
  • API/Realtime/RedisSubscriberService.cs
  • API/Services/Account/AccountService.cs
  • API/Services/Token/ApiTokenService.cs
  • Common.Tests/ConnectionAuthority/ConnectionAuthorityRegistryTests.cs
  • Common.Tests/ConnectionAuthority/ConnectionSubjectTests.cs
  • Common.Tests/Hubs/TrackedHubConnectionTests.cs
  • Common/ConnectionAuthority/AuthorityChangeSubscriber.cs
  • Common/ConnectionAuthority/ConnectionAuthorityRegistry.cs
  • Common/ConnectionAuthority/ConnectionAuthoritySweeper.cs
  • Common/ConnectionAuthority/ConnectionHandle.cs
  • Common/ConnectionAuthority/ConnectionSubject.cs
  • Common/ConnectionAuthority/Grant.cs
  • Common/ConnectionAuthority/GrantResolver.cs
  • Common/ConnectionAuthority/IAuthorizedConnection.cs
  • Common/ConnectionAuthority/IGrantResolver.cs
  • Common/ConnectionAuthority/SessionIds.cs
  • Common/DeviceControl/ShockerAccessQueries.cs
  • Common/Hubs/ConnectionAuthorityHubFilter.cs
  • Common/Hubs/HubConnectionAuthority.cs
  • Common/Hubs/HubConnectionTracker.cs
  • Common/Hubs/PublicShareHub.cs
  • Common/Hubs/RevalidateAuthenticationHubFilter.cs
  • Common/Hubs/UserHub.cs
  • Common/OpenShockDb/PublicShareQueries.cs
  • Common/OpenShockServiceHelper.cs
  • Common/Redis/PubSub/AuthorityChangedMessage.cs
  • Common/Services/ControlSender.cs
  • Common/Services/RedisPubSub/IRedisPubService.cs
  • Common/Services/RedisPubSub/RedisChannels.cs
  • Common/Services/RedisPubSub/RedisPubService.cs
  • Common/Services/Session/SessionService.cs
  • Common/Websocket/WebsockBaseController.cs
  • LiveControlGateway/Controllers/HubControllerBase.cs
  • LiveControlGateway/Controllers/LiveControlController.cs
  • LiveControlGateway/LifetimeManager/HubLifetime.cs
  • LiveControlGateway/LifetimeManager/HubLifetimeManager.cs
  • LiveControlGateway/Metrics/GatewayMetrics.cs
  • LiveControlGateway/Models/LiveShockerPermission.cs
  • LiveControlGateway/Program.cs
  • LiveControlGateway/PubSub/ApiTokenUpdateSubscriber.cs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A change to a hub was announced only on the hub's own channel, as a message telling its gateway to load the hub's shockers again. The gateway took every such message to also mean that who may use the hub had changed. Authority had a second path, and a hub socket that was still being set up was not listening on it yet.

Hub changes are now announced on the authority channel like every other kind, and a hub's own channel only carries what the hub is told to do. Regenerating a hub token announces an authority change and no reload.

Deactivating or reactivating an account no longer tells the gateways to reload that account's hubs. The account's own authority change already reaches every connection that depends on them.

Announcing an authority change no longer throws. It happens after the change is saved, the caller can do nothing about a failure, and the affected connections find out at their next periodic check. One call site handled this itself and the others did not.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant