fix(sdk): close WS before HTTP disconnect to prevent presence race#105
Merged
khaliqgant merged 2 commits intomainfrom Mar 24, 2026
Merged
fix(sdk): close WS before HTTP disconnect to prevent presence race#105khaliqgant merged 2 commits intomainfrom
khaliqgant merged 2 commits intomainfrom
Conversation
The disconnect() method was calling markOffline() (HTTP) before closing the WebSocket. Server-side, each WS ping triggers a fire-and-forget heartbeat to PresenceDO. If a ping's heartbeat landed at PresenceDO *after* the HTTP disconnect, the agent was re-added as online — causing the e2e smoke test to fail with BackendAgent stuck "online". Fix: close the WebSocket first (stopping the ping timer), pause briefly for any in-flight server-side heartbeat to settle, then send the HTTP disconnect as the authoritative final presence update. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Preview deployed!
This preview shares the staging database and will be cleaned up when the PR is merged or closed. Run E2E testsnpm run e2e -- https://pr105-api.relaycast.dev --ciOpen observer dashboard |
Server: await the heartbeat in the AgentDO ping handler. The DO runtime serializes message handlers and webSocketClose, so awaiting ensures the heartbeat settles at PresenceDO before any subsequent disconnect. Client: close the WebSocket before sending the HTTP disconnect. This stops the ping timer immediately, and the HTTP call serves as the authoritative final presence update. Together these guarantee deterministic ordering at PresenceDO: heartbeat (from last ping) → server disconnect (webSocketClose) → client HTTP disconnect. The agent reliably ends up offline. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
khaliqgant
added a commit
that referenced
this pull request
Mar 24, 2026
- Merge origin/main (includes #103 schema + #105 disconnect fix) - Resolve schema.test.ts conflicts: take main's 24-table version with directory/routing tables - Fix PATCH /v1/agents/:name test: mock getAgentByName so the route's existence check passes before calling updateAgent Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BackendAgentstays "online" afterdisconnect()await. If the heartbeat landed at PresenceDO after the client's HTTP disconnect, the agent was re-added as online.awaitthe heartbeat fetch in AgentDO's ping handler. The DO runtime serializeswebSocketMessageandwebSocketClose, so awaiting guarantees the heartbeat settles before any subsequent disconnect is processed.heartbeat → server disconnect → client HTTP disconnectTest plan
BackendAgentstaying online🤖 Generated with Claude Code