Skip to content

fix(desktop): keep the relay client on the selected community after a switch - #8283

Closed
ddfourtwo wants to merge 2 commits into
block:mainfrom
ddfourtwo:fix/desktop-admin-publish-community
Closed

ddfourtwo wants to merge 2 commits into
block:mainfrom
ddfourtwo:fix/desktop-admin-publish-community

Conversation

@ddfourtwo

Copy link
Copy Markdown

Summary

Symptom. With a hosted community selected, Settings → Communities → Invites → "Invite to community" added the key to a different community, a self-hosted one the user had switched away from. The self-hosted relay recorded the kind:9030 add, and nothing reached the hosted relay. It happened three times in about ten minutes.

Cause. The relay invite chain never names a community. publishRelayAdminEvent signs the admin event and publishes it on the app-wide relayClient. That client reads its URL from the backend's process-global relay override and caches the first answer. A switch runs relayClient.disconnect() and then awaits more work before apply_workspace installs the new override. If any relay call lands in that gap, connect() reads the old override, caches it, and keeps the socket. The attempt counter was bumped before the call, so nothing marks the socket stale. From then on, publishes go to the previous community, relay admin events included. The member list and role queries share that socket, so Settings can show the old community's roster under the new community's name.

Fix.

  • connect() caches the URL it looked up only if the attempt is still current. A lookup that outlives a switch can no longer write back the old relay.
  • After applyCommunity succeeds, useCommunityInit pins the client to that community's relay (relayClient.bindRelayUrl). The call tears down any socket or in-flight attempt on another relay. First mount and same-relay re-applies don't disconnect, so the existing no-disconnect guarantees in useCommunityInit.test.mjs still hold.
  • Relay admin events (add member, remove member, change role) now take the selected community's relay from the mutation hooks. They refuse to sign or publish when the connection is to a different relay, or to none.
  • The invite and add-member dialogs name the community they act on.
  • To stay within the 1200-line file-size ratchet for relayClientSession.ts, the inline huddle-event and typing-indicator filters moved to relayChannelFilters.ts as buildHuddleEventsFilter and buildTypingIndicatorFilter. The filter values are unchanged.

Not changed. Tauri commands that use the backend override directly (composer REST send, channel membership, get_channels) are the separate scoping gap tracked in #7518. Upstream already skips applyCommunity for a cancelled switch. A huddle window runs its own useCommunityInit and can re-apply the backend override, but it has its own relay client, so it can't move the main window's socket.

Related issue

Related to #7518 and #7799, both the same class of community-switch race. This PR closes the relay-socket side for relay admin events. The unscoped relayMembersQueryKey roster cache from #7799 is not changed here.

Testing

  • New src/features/communities/communitySwitchRelaySocket.test.mjs drives the real useCommunityInit and relayClient against a fake backend whose relay override changes only when apply_workspace finishes.
    • A relay call between the disconnect and the apply opens a socket to the previous relay. On main, the client ends the switch still targeting that relay with the socket live. Fails on main, passes here.
    • A URL lookup that resolves after the switch must not overwrite the new relay. It fails with the connect() guard removed.
  • relayMembers.test.mjs: add, remove and change-role all reject before anything is signed when the connection is to another relay or none. It fails with the guard removed.
  • pnpm check, pnpm typecheck, pnpm check:file-sizes, and pnpm test (6832 + 160 pass).
  • playwright --project=smoke for invites-settings-screenshots.spec.ts and community-rail.spec.ts: 32 passed. The invite-dialog text assertion was updated for the community name.
  • Not yet done: a manual run in the native app.

… switch

The relay client read its URL from the backend's process-global override and
cached the first answer. A relay call landing after `disconnect()` but before
`apply_workspace` installed the new override connected to the previous
community's relay and survived as the current connection, so later publishes,
relay admin events included, went to the old community.

- `connect()` caches the looked-up URL only if the attempt is still current.
- `useCommunityInit` pins the client to the applied community's relay;
  `bindRelayUrl` tears down any socket or attempt on another relay.
- Relay admin events (add, remove, change role) take the selected community's
  relay and refuse to sign or publish when the connection is elsewhere.
- The invite and add-member dialogs name the community they act on.

Signed-off-by: ddfourtwo <35066307+ddfourtwo@users.noreply.github.com>
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

🔐 Codex Security Review

Status: review required for the current range.

The current range is 799cfd70504c78ebb1958279c08110ce925acd27...f52050da601a069c9c4a0c2c3504fe1b8d66dff1.
A new review must complete for this exact range. When manual authorization
is required, a user with write access must comment exactly
@buzz-security-review f52050da601a069c9c4a0c2c3504fe1b8d66dff1 to authorize a new review.
Any previous review applies only to its recorded range.

Review follow-up: pass the community check to publishEvent's isCurrent so a
switch that lands while the event is being signed cancels the send, and cover
the accepted path and test-state cleanup in relayMembers.test.mjs.

Signed-off-by: ddfourtwo <35066307+ddfourtwo@users.noreply.github.com>
@ddfourtwo

Copy link
Copy Markdown
Author

Our private fork carried this patch, and it has now synced past the part that landed upstream, so closing this as superseded.

What is covered upstream: #9033 added RelayEntryRequirements.connect, which resolves the relay URL, throws Relay connection attempt was superseded. before and after the websocket connect, and closes a stale socket with "stale connection attempt". That is the community-switch behaviour this PR was filed for — an in-flight connect can no longer bind the previous community's relay. We verified it by replaying this PR's regression tests against upstream's implementation (relayMembers.test.mjs + communitySwitchRelaySocket.test.mjs, 12 passing), with our duplicate guard removed.

What is still not upstream, and stays in our fork: RelayClient.getRelayUrl() / bindRelayUrl(); the relay-identity guards on relay admin member events in relayMembers.ts, which refuse to publish when the socket is connected to a different community than the one the change is meant for; the huddle/typing filter extraction in relayChannelFilters.ts; and the surrounding integration and e2e coverage. If that narrower half is useful here, I'm glad to open a PR for just it.

Thanks for the review.

@ddfourtwo ddfourtwo closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant