Skip to content

fix(realtime): cast presence ids to string in onClose cleanup - #13551

Merged
ArnabChatterjee20k merged 7 commits into
mainfrom
fix-sentry-REALTIME-1G
Sep 9, 2026
Merged

ArnabChatterjee20k merged 7 commits into
mainfrom
fix-sentry-REALTIME-1G

Conversation

@ArnabChatterjee20k

Copy link
Copy Markdown
Member

What

Fixes an Invalid query: Query value is invalid for attribute "$id" error thrown from the realtime onClose presence-cleanup path (Sentry: realtimeOnClosePresenceDeletion / realtime.close.presenceCleanup).

Root cause

A presence id is a valid UID, and a UID may be purely numeric (0-9 are allowed). The presenceId param is validated only as Text(36), so a client can publish a presence with an id like "12345".

The realtime worker tracks live presences as a set keyed by id:

$realtime->connections[$connectionId]['presences'][$presence->getId()] = true;

PHP coerces any canonical-integer string key to an int. On disconnect the cleanup does:

$presenceIds = \array_keys($presencesById);        // numeric ids come back as int
$dbForProject->deleteDocuments('presenceLogs', [Query::equal('$id', $presenceIds)]);

So $id (a string column) is queried with an integer:

  • the query validator rejects it → the observed exception, and
  • even where it passes, an int never matches the string id on strict adapters (Postgres/Mongo), so those presence rows leak instead of being cleaned up.

Note: user.id/project.id reading n/a in the Sentry tags is a span-tagging artifact (the cleanup span only sets realtime.projectId), not evidence the user was unauthenticated — a presence cannot be created without an authenticated user.

Fix

Cast the ids back to strings right before the delete query:

$presenceIds = \array_map('strval', \array_keys($presencesById));

Diagnostics

To make any future occurrence self-explanatory, the cleanup now records user.id on the span and adds userId + the concrete presenceIds to the realtimeOnClosePresenceDeletion error tags (previously only a count). Ids are attached on the failure path only, to avoid span cardinality blowup.

Tests

Adds testPresenceCloseWithNumericIdEmitsDeleteEvent (e2e, PresenceRealtimeClientTest): publishes a presence with a numeric id over realtime, closes the connection, and asserts the presences.<id>.delete event fires and GET /presences/<id> returns 404. Without the fix the numeric id is queried as an int, the row is not deleted, and both assertions fail.

app/realtime.php is a process entrypoint with no unit-testable seam for this glue, so the regression is covered through the real realtime API per the repo's E2E conventions.

Verification

docker compose up -d --force-recreate --build   # realtime container must reload app/realtime.php
docker compose exec appwrite test tests/e2e/Services/Presences/PresenceRealtimeClientTest --filter=testPresenceCloseWithNumericIdEmitsDeleteEvent

🤖 Generated with Claude Code

Numeric-only presence ids are valid UIDs, but PHP coerces them to
integer keys when stored in the connection map. array_keys then yields
ints, so Query::equal('$id', ...) queries the string $id column with an
integer - rejected by the query validator and unmatched on strict
adapters, leaking presence rows. Cast the ids back to strings before the
delete query.
… ids

Record user.id on the cleanup span and add userId plus the concrete
presenceIds to the realtimeOnClosePresenceDeletion error tags, so a
future failure carries the offending ids instead of only a count.
Publish a presence with a numeric id over realtime, close the
connection, and assert the delete event fires and the row is gone
(404) - guards the coerced-key $id cleanup regression.
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the numeric-ID cleanup is corrected and the regression test validates the externally observable deletion behavior.

Summary

  • Normalizes tracked presence IDs before database deletion.
  • Records user context and a bounded sample of IDs when cleanup fails.
  • Verifies that disconnect emits a delete event and removes the presence through public APIs.

Comment thread app/realtime.php Resolved
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-sentry-REALTIME-1G (after).

Metric Before After Change
🚀 Requests/sec 190.56 184 ⚪ -3.4%
⏱️ Latency P50 89.86 ms 93.97 ms ⚪ +4.6%
⏱️ Latency P95 216.23 ms 224.52 ms ⚪ +3.8%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 93.97 224.52 11,457 184 +8.29
Account 190.05 365.97 603 9.98 +23.51
TablesDB 90.03 161.39 6,231 101.7 -1.92
Storage 87.91 191.83 3,015 51.47 +6.42
Functions 141.72 280.14 1,608 28.33 +13.6

Top API waits (after)

API request Max wait (ms)
functions.delete 535.1
account.prefs.update 524.34
functions.variables.create 523.74
account.name.update 518.61
functions.variables.update 479.82

Sample at most 10 ids in the error tag; the total stays available via
the separate presences count, so a busy connection cannot overflow
telemetry tag length limits.
Guard the publisher against a double close and tear it down even if an
earlier assertion throws before the mid-body close.
Comment thread app/realtime.php Resolved
Comment thread app/realtime.php Resolved
@ArnabChatterjee20k
ArnabChatterjee20k merged commit 02b75d8 into main Sep 9, 2026
45 of 46 checks passed
@ArnabChatterjee20k
ArnabChatterjee20k deleted the fix-sentry-REALTIME-1G branch September 9, 2026 06:09
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 tasks
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