Skip to content

regression: timed status sometimes never reverts after expiring#41047

Merged
dionisio-bot[bot] merged 3 commits into
release-8.6.0from
fix/presence-restore
Jun 24, 2026
Merged

regression: timed status sometimes never reverts after expiring#41047
dionisio-bot[bot] merged 3 commits into
release-8.6.0from
fix/presence-restore

Conversation

@ricardogarim

@ricardogarim ricardogarim commented Jun 22, 2026

Copy link
Copy Markdown
Member

Proposed changes (including videos or screenshots)

A temporary status with an expiration (e.g. Busy "until 14:32") could intermittently never revert — it stayed stuck until the user changed status again or the server restarted.

Status expiry runs off a single in-memory timer rescheduled on every status change. Concurrent reschedules raced over that timer: a slow, stale lookup could resolve last and wipe out a newer call's timer, leaving the expiration unscheduled. A status that then went overdue with no timer was invisible to the scheduler.

Fix (keeps the precise on-time timer):

  • Presence.ts — guard the reschedule with a per-call token: each call stamps a unique Symbol before its lookup and bails if a newer call replaced it, so a stale lookup can't clear the newer timer. Calls stay independent (no serialization). Shutdown invalidates the token so an in-flight reschedule can't re-arm the timer after the service stops.
  • Users.tsfindNextStatusExpiration now returns the soonest expiry including overdue ones ({$exists: true}), so a missed expiry self-heals on the next scheduling pass.

The revert logic (endActive) was already correct — only the scheduling was broken.

Issue(s)

Steps to test or reproduce

  1. Set Busy with a short expiration (with or without a message).
  2. Wait past the expiration → it reverts to online and clears text/expiration.
  3. Set the status repeatedly around the expiry boundary — still reverts reliably.

Further comments

Summary by CodeRabbit

Summary

  • Bug Fixes
    • Improved reliability of user status expiration scheduling by preventing outdated timers from being re-armed during async race conditions.
    • Adjusted how the next upcoming status expiration is selected, ensuring the system consistently finds the correct expiration timestamp and processes expirations more accurately.

@ricardogarim ricardogarim requested a review from a team as a code owner June 22, 2026 22:51
@dionisio-bot

dionisio-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is ready to merge! 🎉
If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b6b68cc

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 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Two small fixes to the status expiration pipeline: the MongoDB query in findNextStatusExpiration is changed from filtering by statusExpiresAt >= now to filtering by statusExpiresAt field existence to include past-due expirations, and setupNextExpiration adds a token-based concurrency guard with proper shutdown cleanup to prevent stale async results from modifying the timer.

Changes

Status Expiration Fix

Layer / File(s) Summary
Expiration query predicate
packages/models/src/models/Users.ts
findNextStatusExpiration filter changes from { $gte: new Date() } to { $exists: true } so candidates include past-due expirations.
Async race guard for expiration scheduling
ee/packages/presence/src/Presence.ts
Adds expirationScheduleToken?: symbol private field. setupNextExpiration now records a token before awaiting the DB lookup and only clears/resets expirationTimeout if the token is unchanged, preventing stale results from corrupting the timer during concurrent reschedules. stopped() invalidates the token before clearing the timeout, ensuring in-flight scheduling cannot affect timers after shutdown.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • sampaiodiego
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main regression being fixed: timed statuses not reverting after expiring. It directly corresponds to the primary issue addressed in the PR objectives and code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • PRES-56: Request failed with status code 401

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ee/packages/presence/src/Presence.ts`:
- Around line 134-138: The setupNextExpiration() method has a race condition
where an older call can resolve after a newer call, clearing the newer timeout
and leaving an expiration unscheduled. Fix this by implementing a monotonic
request token mechanism: create a counter that increments each time
setupNextExpiration() is invoked, store the current token value for each
request, and only proceed with the clearTimeout and timeout assignment if the
stored token still matches the current counter value, ensuring stale requests
cannot overwrite newer timeouts.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c03e4276-9546-47bd-af02-0fd7a2e93d48

📥 Commits

Reviewing files that changed from the base of the PR and between 87cd0e2 and 5d077f6.

📒 Files selected for processing (2)
  • ee/packages/presence/src/Presence.ts
  • packages/models/src/models/Users.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (5)
  • GitHub Check: 📦 Build Packages
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: Hacktron Security Check
  • GitHub Check: CodeQL-Build
  • GitHub Check: CodeQL-Build
🧰 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:

  • packages/models/src/models/Users.ts
  • 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:

  • packages/models/src/models/Users.ts
  • 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:

  • packages/models/src/models/Users.ts
  • 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:

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

1136-1136: LGTM!

Comment thread ee/packages/presence/src/Presence.ts

@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.

1 issue found across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread ee/packages/presence/src/Presence.ts
@ricardogarim ricardogarim marked this pull request as draft June 22, 2026 23:05
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.12%. Comparing base (02b8008) to head (b6b68cc).
⚠️ Report is 34 commits behind head on release-8.6.0.

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##           release-8.6.0   #41047    +/-   ##
===============================================
  Coverage          70.12%   70.12%            
===============================================
  Files               3357     3360     +3     
  Lines             129552   129674   +122     
  Branches           22407    22500    +93     
===============================================
+ Hits               90843    90930    +87     
- Misses             35416    35436    +20     
- Partials            3293     3308    +15     
Flag Coverage Δ
e2e 59.37% <ø> (+0.10%) ⬆️
e2e-api 46.25% <ø> (+0.04%) ⬆️
unit 70.02% <0.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ricardogarim ricardogarim marked this pull request as ready for review June 23, 2026 00:06

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ee/packages/presence/src/Presence.ts`:
- Around line 136-148: The stopped() method clears expirationTimeout but fails
to invalidate expirationScheduleToken. This allows in-flight
setupNextExpiration() calls that are awaiting Users.findNextStatusExpiration()
to pass the token check at line 141 after the await completes, re-arming the
timer even after shutdown. In the stopped() method, invalidate
expirationScheduleToken (by assigning it a new unique value like Symbol() or a
sentinel value) in addition to clearing expirationTimeout so that any pending
setupNextExpiration invocations will fail their token equality check and exit
early without re-arming the timer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 484309b7-43c7-4d8b-874b-474d949badb3

📥 Commits

Reviewing files that changed from the base of the PR and between 5d077f6 and 24b5db1.

📒 Files selected for processing (1)
  • ee/packages/presence/src/Presence.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (4)
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: Hacktron Security Check
  • GitHub Check: 📦 Build Packages
  • GitHub Check: CodeQL-Build
🧰 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)

51-51: LGTM!

Comment thread ee/packages/presence/src/Presence.ts

@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.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="ee/packages/presence/src/Presence.ts">

<violation number="1" location="ee/packages/presence/src/Presence.ts:142">
P2: The token guard prevents reschedule races but doesn't account for the shutdown path. `stopped()` clears `expirationTimeout` but never invalidates `expirationScheduleToken`. If `setupNextExpiration` is awaiting `findNextStatusExpiration()` when `stopped()` is called, the token check will still pass after the await resolves, re-arming the timer on a stopped service. Add `this.expirationScheduleToken = undefined;` (or assign a new Symbol) in `stopped()` so in-flight calls bail out.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread ee/packages/presence/src/Presence.ts
@ricardogarim ricardogarim added this to the 8.6.0 milestone Jun 23, 2026
@ricardogarim ricardogarim added the stat: QA assured Means it has been tested and approved by a company insider label Jun 24, 2026
@dionisio-bot dionisio-bot Bot added the stat: ready to merge PR tested and approved waiting for merge label Jun 24, 2026
@dionisio-bot dionisio-bot Bot merged commit bb11bf4 into release-8.6.0 Jun 24, 2026
83 of 85 checks passed
@dionisio-bot dionisio-bot Bot deleted the fix/presence-restore branch June 24, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stat: QA assured Means it has been tested and approved by a company insider stat: ready to merge PR tested and approved waiting for merge type: bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants