Skip to content

Guard Matrix client lifecycle starts#43

Merged
Just-Insane merged 1 commit into
integrationfrom
codex/fix-matrix-client-lifecycle
Jun 13, 2026
Merged

Guard Matrix client lifecycle starts#43
Just-Insane merged 1 commit into
integrationfrom
codex/fix-matrix-client-lifecycle

Conversation

@Just-Insane

Copy link
Copy Markdown
Collaborator

Description

Fixes #

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

AI disclosure:

  • Partially AI assisted (clarify which code was AI assisted and briefly explain what it does).
  • Fully AI generated (explain what all the generated code does in moderate detail).

Copilot AI review requested due to automatic review settings June 13, 2026 15:20
@Just-Insane Just-Insane merged commit 471f8bc into integration Jun 13, 2026
10 of 11 checks passed
@Just-Insane Just-Insane deleted the codex/fix-matrix-client-lifecycle branch June 13, 2026 15:20
@github-actions

Copy link
Copy Markdown

⚠️ Missing changeset

This pull request does not include a changeset. Please add one before requesting review so the change is properly documented and included in the release notes.

How to add a changeset:

  1. Run pnpm run document-change (interactive) and commit the generated file, or
  2. Manually create .changeset/<descriptive-name>.md:
---
default: patch
---

Short user-facing summary of the change.

Replace patch with major, minor, patch, docs, or note as appropriate.

📖 Read more in CONTRIBUTING.md.

If this PR is internal/maintenance with no user-facing impact, a maintainer can add the internal label to skip this check.

@github-actions

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

Status Preview URL Commit Alias Updated (UTC)
✅ Deployment successful! https://pr-43-sable.justin-tech.workers.dev 7da21a2 pr-43 Sat, 13 Jun 2026 15:22:29 GMT

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds lifecycle guards around Matrix client startup/shutdown so the “app” Matrix client behaves like a singleton (preventing duplicate/concurrent starts), while allowing background notification clients to start independently. It also updates call sites to await/chain client shutdown where needed and adds regression tests for the new gating behavior.

Changes:

  • Introduces an app-scoped startClient gate to reuse in-flight starts and stop/replace any previously-active app client.
  • Changes stopClient to return a Promise<void> (with a short settlement delay) and updates callers to await/void it appropriately.
  • Adds clientScope: 'background' for background notification clients and tests covering the new app start gating.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/client/initMatrix.ts Adds app-scope start gating, background scope opt-out, and makes stopClient async-like for sequencing.
src/client/initMatrix.test.ts Adds tests for app singleton start behavior; expands Sentry mocks.
src/app/pages/client/sidebar/AccountSwitcherTab.tsx Ensures navigation to “add account” happens after client stop completes (via finally).
src/app/pages/client/ClientRoot.tsx Awaits/voids stopClient in lifecycle paths; avoids triggering redundant start while already loading.
src/app/pages/client/BackgroundNotifications.tsx Marks background clients with clientScope: 'background' and prevents duplicate background starts per user.

Comment thread src/client/initMatrix.ts
Comment on lines +1074 to +1094
if (activeAppClient && activeAppClient !== mx) {
debugLog.warn('sync', 'Stopping previous app Matrix client before starting replacement', {
previousUserId: activeAppClient.getUserId(),
nextUserId: mx.getUserId(),
previousSyncState: activeAppClient.getSyncState(),
previousRunning: activeAppClient.clientRunning,
});
Sentry.addBreadcrumb({
category: 'sync.lifecycle',
message: 'Stopping previous app Matrix client before replacement start',
level: 'warning',
data: {
previousUserId: activeAppClient.getUserId(),
nextUserId: mx.getUserId(),
previousSyncState: activeAppClient.getSyncState(),
previousRunning: activeAppClient.clientRunning,
},
});
Sentry.metrics.count('sable.sync.previous_app_client_stopped', 1);
await stopClient(activeAppClient);
}
Comment on lines +63 to +65
const startClassicClient = async (mx: MockMatrixClient): Promise<void> => {
startedClients.push(mx);
await startClient(mx, {
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.

2 participants