Skip to content

fix(user_status): make setUserStatus() idempotent for concurrent duplicate requests#59913

Open
miaulalala wants to merge 1 commit into
masterfrom
fix/noid/user-status-phase2b-idempotent-set-status
Open

fix(user_status): make setUserStatus() idempotent for concurrent duplicate requests#59913
miaulalala wants to merge 1 commit into
masterfrom
fix/noid/user-status-phase2b-idempotent-set-status

Conversation

@miaulalala

Copy link
Copy Markdown
Contributor

Summary

When two automated events of the same type fire simultaneously (e.g. a calendar webhook is delivered twice, or two Talk clients both trigger a call status at the same time), both requests race through setUserStatus().

The first request succeeds: it creates a backup of the user's previous status and sets the new automated status. The second request finds the backup slot already occupied (unique constraint on user_id), so backupCurrentStatus() returns false. Previously this caused setUserStatus() to return null, which the caller interpreted as a failure even though the desired status was already correctly set by the first request.

Fix: after backupCurrentStatus() returns false, re-read the current active status row. If it already carries the same messageId that we were trying to set, the operation is effectively complete — return the existing status instead of null. If it carries a different messageId (a higher-priority automated status is active), we still return null to abort as before.

This also covers the meeting-with-call scenario: if a calendar BUSY status is active and a Talk call starts, the call correctly overrides it via the priority shortcut path. If the calendar fires a duplicate webhook while the call is already active, the re-read finds MESSAGE_CALL ≠ MESSAGE_CALENDAR_BUSY and correctly aborts rather than overriding the ongoing call.

AI-Assisted-By: Claude Sonnet 4.6 noreply@anthropic.com

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@miaulalala miaulalala added this to the Nextcloud 34 milestone Apr 25, 2026
@miaulalala miaulalala self-assigned this Apr 25, 2026
@miaulalala miaulalala added the bug label Apr 25, 2026
@miaulalala miaulalala requested a review from a team as a code owner April 25, 2026 15:44
@miaulalala miaulalala requested review from ArtificialOwl, artonge, come-nc and provokateurin and removed request for a team April 25, 2026 15:44
@miaulalala miaulalala force-pushed the fix/noid/user-status-phase2b-idempotent-set-status branch from 6597362 to b3313cd Compare April 25, 2026 15:54
@miaulalala miaulalala force-pushed the fix/noid/user-status-phase2b-idempotent-set-status branch 2 times, most recently from b110271 to 4fc7729 Compare May 5, 2026 10:13
// If the active status already carries the same messageId, this is a
// duplicate/concurrent request for the same event — treat it as success.
try {
$currentStatus = $this->mapper->findByUserId($userId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Don't we already have the user status as $userStatus?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

$userStatus was read at the top of the function, before backupCurrentStatus() ran.

In the concurrent race case, another request has already renamed the original status row to a backup (_alice), and inserted a new active row with the automated messageId.

Our in-memory $userStatus object reflects the DB state from before those writes, so it's stale. The fresh read after backupCurrentStatus() returns false is the only way to reliably see what's currently active — which is what we need to compare against $messageId for the idempotency check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm with artonge here and can not follow this.
If we assume a race condition after both requests have read $userStatus = $this->mapper->findByUserId($userId); in line 245. there is also no guarantee that the other process already finished it's insert yet?
The code from the read to the code point here is 4 boolean comparisons with 1 boolean assignment and 1 debug log in the winning/loosing case and then the one UPDATE query.

…icate requests

When two automated events of the same type fire simultaneously (e.g. a
calendar webhook is delivered twice, or two Talk clients both trigger a
call status at the same time), both requests race through setUserStatus().

The first request succeeds: it creates a backup of the user's previous
status and sets the new automated status. The second request finds the
backup slot already occupied (unique constraint on user_id), so
backupCurrentStatus() returns false. Previously this caused setUserStatus()
to return null, which the caller interpreted as a failure even though the
desired status was already correctly set by the first request.

Fix: after backupCurrentStatus() returns false, re-read the current active
status row. If it already carries the same messageId that we were trying to
set, the operation is effectively complete — return the existing status
instead of null. If it carries a different messageId (a higher-priority
automated status is active), we still return null to abort as before.

This also covers the meeting-with-call scenario: if a calendar BUSY status
is active and a Talk call starts, the call correctly overrides it via the
priority shortcut path. If the calendar fires a duplicate webhook while the
call is already active, the re-read finds MESSAGE_CALL ≠ MESSAGE_CALENDAR_BUSY
and correctly aborts rather than overriding the ongoing call.

AI-Assisted-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Anna Larch <anna@nextcloud.com>
@miaulalala miaulalala force-pushed the fix/noid/user-status-phase2b-idempotent-set-status branch from 4fc7729 to 428fe31 Compare June 24, 2026 11:38
@miaulalala miaulalala requested a review from artonge June 24, 2026 11:38
@miaulalala

Copy link
Copy Markdown
Contributor Author

/backport to stable34

@miaulalala

Copy link
Copy Markdown
Contributor Author

/backport to stable33

@miaulalala

Copy link
Copy Markdown
Contributor Author

/backport to stable32

@nickvergessen nickvergessen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd really like to review this as maintainer give me some more time 🙈

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.

7 participants