From 3f8f26bab43893bad710a61ce9966ad1c61a4b5b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 25 Aug 2026 11:35:06 +0530 Subject: [PATCH] fix(realtime): send connected handshake before channel subscribe (#13329) * fix(realtime): send connected handshake before channel subscribe Presence E2E flakes on the shared console project because onOpen registered URL-channel subscriptions (and then yielded in updateStats on DB/queue I/O) before writing the connected frame. A concurrent presences event from another paratest worker could then be the client's first websocket message. Send connected first, then subscribe. Tests wait for the handshake frame instead of assuming it is first, and expiry waits on the clock instead of a fixed sleep. Co-authored-by: Chirag Aggarwal * fix(tests): stamp presence expiry immediately before PATCH ExpiresAt was computed before upsert/create, so a slow request could make the timestamp past-dated and fail DatetimeValidator (requireDateInFuture). Co-authored-by: Chirag Aggarwal * test(presences): assert first realtime frame is the handshake Keep regression coverage for the console URL-channel race: an event arriving before connected must fail the test, not be skipped. Co-authored-by: Chirag Aggarwal --------- Co-authored-by: Cursor Agent --- app/realtime.php | 57 ++++++++++++------- .../Presences/PresenceConsoleClientTest.php | 35 ++++++++++-- .../Services/Presences/PresenceExpiryTest.php | 28 +++++---- .../Presences/PresenceRealtimeClientTest.php | 29 +++++++++- 4 files changed, 111 insertions(+), 38 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 9013d1072e..2c5894bdf2 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -1035,12 +1035,17 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, ] ]); + // Send `connected` before subscribe()/updateStats(). Those steps put the + // connection in the in-memory delivery tree and then hit the platform DB + // / usage queue (coroutine yields). A pub/sub event delivered in that + // window would otherwise become the client's first websocket frame. + $server->send([$connection], $connectedPayloadJson); + $outboundBytes += \strlen($connectedPayloadJson); + $realtime->subscribe($project->getId(), $connection, '', $roles, [], [], $targetUser->getId()); $realtime->connections[$connection]['authorization'] = $authorization; $realtime->connections[$connection]['impersonatedUserId'] = $impersonatorUser->isEmpty() ? null : $targetUser->getId(); $updateStats($project->getId(), $project->getAttribute('teamId')); - $server->send([$connection], $connectedPayloadJson); - $outboundBytes += \strlen($connectedPayloadJson); triggerStats([ METRIC_REALTIME_OUTBOUND => \strlen($connectedPayloadJson), ], $project->getId()); @@ -1064,29 +1069,11 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, $sanitizedUser = empty($targetUser->getId()) ? null : $response->output($targetUser, Response::MODEL_ACCOUNT); $mapping = []; + $prepared = []; foreach ($subscriptions as $index => $subscription) { $subscriptionId = ID::unique(); - - $realtime->subscribe( - $project->getId(), - $connection, - $subscriptionId, - $roles, - $subscription['channels'], - $subscription['queries'], - $targetUser->getId() - ); - $mapping[$index] = $subscriptionId; - } - - $realtime->connections[$connection]['authorization'] = $authorization; - $realtime->connections[$connection]['impersonatedUserId'] = $impersonatorUser->isEmpty() ? null : $targetUser->getId(); - $updateStats($project->getId(), $project->getAttribute('teamId')); - - $subscriptionCount = \count($subscriptions); - if (!empty($subscriptions)) { - $register->get('telemetry.workerSubscriptionCounter')->add(\count($subscriptions), $register->get('telemetry.workerAttributes')); + $prepared[] = [$subscriptionId, $subscription]; } $connectedPayloadJson = json_encode([ @@ -1098,8 +1085,34 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, ] ]); + // Handshake first: URL channels subscribe the connection into the delivery + // tree before `connected` is sent, and updateStats() yields on DB/queue I/O. + // Concurrent events (especially on the shared console `presences` channel) + // can then race ahead of the handshake frame. $server->send([$connection], $connectedPayloadJson); $outboundBytes += \strlen($connectedPayloadJson); + + foreach ($prepared as [$subscriptionId, $subscription]) { + $realtime->subscribe( + $project->getId(), + $connection, + $subscriptionId, + $roles, + $subscription['channels'], + $subscription['queries'], + $targetUser->getId() + ); + } + + $realtime->connections[$connection]['authorization'] = $authorization; + $realtime->connections[$connection]['impersonatedUserId'] = $impersonatorUser->isEmpty() ? null : $targetUser->getId(); + $updateStats($project->getId(), $project->getAttribute('teamId')); + + $subscriptionCount = \count($subscriptions); + if (!empty($subscriptions)) { + $register->get('telemetry.workerSubscriptionCounter')->add(\count($subscriptions), $register->get('telemetry.workerAttributes')); + } + triggerStats([ METRIC_REALTIME_OUTBOUND => \strlen($connectedPayloadJson), ], $project->getId()); diff --git a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php index 0848275640..1a57d16e76 100644 --- a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php +++ b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php @@ -119,8 +119,6 @@ final class PresenceConsoleClientTest extends Scope public function testExpiredConsolePresenceDeletedByMaintenance(): void { $presenceId = ID::unique(); - // Set a near-future expiry to satisfy validation, then wait until it is in the past. - $expiresAt = DateTime::format((new \DateTime())->modify('+2 seconds')); $upsert = $this->client->call( Client::METHOD_PUT, @@ -141,6 +139,9 @@ final class PresenceConsoleClientTest extends Scope ); $this->assertSame(200, $upsert['headers']['status-code']); + // Compute expiry immediately before PATCH. The API requires a future + // datetime; stamping it before upsert can already be in the past under load. + $expiresAt = DateTime::format((new \DateTime())->modify('+2 seconds')); $expire = $this->client->call( Client::METHOD_PATCH, '/presences/' . $presenceId, @@ -199,12 +200,38 @@ final class PresenceConsoleClientTest extends Scope ] ); - $connected = \json_decode($client->receive(), true); - $this->assertSame('connected', $connected['type'] ?? null); + $this->receiveConnected($client); return $client; } + private function receiveConnected(WebSocketClient $client, int $timeoutMs = 3000): void + { + $deadline = \microtime(true) + ($timeoutMs / 1000); + + while (\microtime(true) < $deadline) { + try { + $raw = $client->receive(); + } catch (TimeoutException) { + continue; + } + + $frame = \json_decode($raw, true); + if (!\is_array($frame)) { + continue; + } + + $this->assertSame( + 'connected', + $frame['type'] ?? null, + 'First realtime frame must be the handshake, got: ' . \json_encode($frame) + ); + return; + } + + $this->fail('Timed out waiting for connected websocket frame on console.'); + } + private function receivePresenceFrame( WebSocketClient $client, string $presenceId, diff --git a/tests/e2e/Services/Presences/PresenceExpiryTest.php b/tests/e2e/Services/Presences/PresenceExpiryTest.php index 866c4e7d12..29c2a2db4c 100644 --- a/tests/e2e/Services/Presences/PresenceExpiryTest.php +++ b/tests/e2e/Services/Presences/PresenceExpiryTest.php @@ -39,8 +39,6 @@ final class PresenceExpiryTest extends Scope { $projectId = $this->getProject()['$id']; $userId = $this->getUser()['$id']; - // Set a near-future expiry to satisfy validation, then wait until it is in the past. - $expiresAt = DateTime::format((new \DateTime())->modify('+2 seconds')); $createServer = $this->client->call( Client::METHOD_PUT, @@ -60,6 +58,9 @@ final class PresenceExpiryTest extends Scope $this->assertEquals(200, $createServer['headers']['status-code']); $presenceIdServer = $createServer['body']['$id']; + // Compute expiry immediately before PATCH. The API requires a future + // datetime; stamping it before create can already be in the past under load. + $expiresAt = DateTime::format((new \DateTime())->modify('+2 seconds')); $expireServer = $this->client->call( Client::METHOD_PATCH, '/presences/' . $presenceIdServer, @@ -80,23 +81,30 @@ final class PresenceExpiryTest extends Scope (new \DateTime($expireServer['body']['expiresAt']))->getTimestamp() ); - \sleep(3); + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-key' => $this->getPresenceApiKey(), + ]; + + // Wait until expiresAt is in the past, then run maintenance and observe cleanup. + $this->assertEventually(function () use ($expiresAt) { + $this->assertGreaterThan( + (new \DateTime($expiresAt))->getTimestamp(), + (new \DateTime())->getTimestamp() + ); + }, 30000, 50); $stdout = ''; $stderr = ''; $code = Console::execute('docker exec appwrite maintenance --type=trigger', '', $stdout, $stderr); $this->assertSame(0, $code, "Maintenance command failed with code $code: $stderr ($stdout)"); - // Maintenance + delete workers are asynchronous; give extra time to observe cleanup. - $this->assertEventually(function () use ($presenceIdServer, $projectId) { + $this->assertEventually(function () use ($presenceIdServer, $headers) { $getServer = $this->client->call( Client::METHOD_GET, '/presences/' . $presenceIdServer, - [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - 'x-appwrite-key' => $this->getPresenceApiKey(), - ] + $headers ); $this->assertEquals(404, $getServer['headers']['status-code']); diff --git a/tests/e2e/Services/Presences/PresenceRealtimeClientTest.php b/tests/e2e/Services/Presences/PresenceRealtimeClientTest.php index 0d2a6b0970..aae87ec28d 100644 --- a/tests/e2e/Services/Presences/PresenceRealtimeClientTest.php +++ b/tests/e2e/Services/Presences/PresenceRealtimeClientTest.php @@ -81,8 +81,7 @@ final class PresenceRealtimeClientTest extends Scope ] ); - $connected = \json_decode($client->receive(), true); - $this->assertSame('connected', $connected['type'] ?? null); + $this->receiveConnected($client, \max(800, $timeout * 1000)); if (empty($channels)) { return $client; @@ -104,6 +103,32 @@ final class PresenceRealtimeClientTest extends Scope return $client; } + private function receiveConnected(WebSocketClient $client, int $timeoutMs = 800): void + { + $deadline = \microtime(true) + ($timeoutMs / 1000); + + while (\microtime(true) < $deadline) { + try { + $message = \json_decode($client->receive(), true); + } catch (TimeoutException) { + continue; + } + + if (!\is_array($message)) { + continue; + } + + $this->assertSame( + 'connected', + $message['type'] ?? null, + 'First realtime frame must be the handshake, got: ' . \json_encode($message) + ); + return; + } + + $this->fail('Timed out waiting for connected websocket handshake.'); + } + private function receiveUntil( WebSocketClient $client, callable $match,