Skip to content

fix: add job-level lock to prevent redundant cross-instance reads in …fix: Add job-level lock to prevent redundant cross-instance reads in Presence service - #40987

Closed
ZunedKhan07 wants to merge 3 commits into
RocketChat:developfrom
ZunedKhan07:fix/presence-cross-instance-reads-lock
Closed

fix: add job-level lock to prevent redundant cross-instance reads in …fix: Add job-level lock to prevent redundant cross-instance reads in Presence service#40987
ZunedKhan07 wants to merge 3 commits into
RocketChat:developfrom
ZunedKhan07:fix/presence-cross-instance-reads-lock

Conversation

@ZunedKhan07

@ZunedKhan07 ZunedKhan07 commented Jun 17, 2026

Copy link
Copy Markdown

Closes #40973

Summary

In Microservices (MS) mode, every instance of the presence service was independently executing the removeLostConnections operation inside the started() lifecycle timeout loop. This caused redundant cross-instance reads and unnecessary heavy database/broker operations across multiple running instances.

Changes

  • Wrapped the removeLostConnections lifecycle timeout logic inside a distributed job-level lock using this.api?.lock().
  • Ensured that only one primary instance clears lost connections in distributed or MS mode environments, while other instances safely return.
  • Implemented a proper try-finally block to guarantee the safe release of the lock after execution.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of the presence system's cleanup process by implementing better concurrency handling to prevent multiple simultaneous operations from conflicting with each other.

@dionisio-bot

dionisio-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 862978c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7eaa5bb7-5cba-4135-9f4b-e86ff6d4f989

📥 Commits

Reviewing files that changed from the base of the PR and between 928346f and 52fc22b.

📒 Files selected for processing (1)
  • ee/packages/presence/src/Presence.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • ee/packages/presence/src/Presence.ts
🧠 Learnings (3)
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • ee/packages/presence/src/Presence.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • ee/packages/presence/src/Presence.ts
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • ee/packages/presence/src/Presence.ts
🔇 Additional comments (1)
ee/packages/presence/src/Presence.ts (1)

87-97: LGTM!


Walkthrough

The lostConTimeout callback in Presence.started() now acquires a distributed lock (presence.removeLostConnections, 5000ms timeout) before executing removeLostConnections() and updating user presence. If the lock is unavailable, the callback exits early. The lock is released in a finally block.

Changes

Distributed lock for lost-connection cleanup

Layer / File(s) Summary
Distributed lock around removeLostConnections callback
ee/packages/presence/src/Presence.ts
lostConTimeout callback wraps the removeLostConnections() call and per-user presence updates in an api.lock('presence.removeLostConnections', 5000) acquisition. Returns early if the lock is not obtained; releases the lock in a try/finally block.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly summarizes the main change: adding a distributed lock to prevent redundant cross-instance operations in the Presence service during scheduled cleanup.
Linked Issues check ✅ Passed The code change directly addresses issue #40973 by implementing a distributed lock mechanism via this.api?.lock() to serialize removeLostConnections execution across instances.
Out of Scope Changes check ✅ Passed The changes are narrowly scoped to the Presence.started() method and focus exclusively on adding distributed locking around the removeLostConnections operation as required.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 1 file

Re-trigger cubic

@ricardogarim

Copy link
Copy Markdown
Member

Thanks @ZunedKhan07! Closing this one — the problem it targets (deduping cross-instance presence work in MS mode) is already being solved in #41153, which moves status expiration onto @rocket.chat/cron. Agenda's Mongo job lock guarantees a single instance runs each job, which is the "job-level lock" this was reaching for, and it also clears the TODO #40973 was generated from. Appreciate the contribution! 🙏

@ZunedKhan07

ZunedKhan07 commented Jul 20, 2026 via email

Copy link
Copy Markdown
Author

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

in MS mode every instance runs this independently.

2 participants