Skip to content

Revision 1 — the current text. The evidence on this page is this revision’s own — it has not been carried forward from, or to, any other revision.

Handling Durable Object WebSocket disconnects on deploy

Covers the expected, documented behaviour that a code update terminates a Durable Object's WebSockets, and gradual deployments extending the window this can happen in. Does not cover WebSocket drops unrelated to deploys (idle timeouts, client network loss, or missing ping/pong handling) — rule those out first if the disconnects don't line up with a deploy.

Unverifiedno reproductions yetWhy this confidence?
Revision 1published by DevYou curation

Symptoms

The diagnostic path

8 steps, exactly as this revision published them.

  1. Step 1 · StartDurable Object WebSocket disconnects on deploy

    Every WebSocket connected to a Durable Object closes when that Durable Object's code is updated, because Cloudflare restarts the object to run the new code.

    What happens next

    • passed step 2, Confirm disconnects line up with deploys
  2. Step 2 · TestConfirm disconnects line up with deploys

    Compare deployment timestamps against WebSocket close events to rule out an unrelated cause (idle timeout, client network loss).

    Read-onlysh
    npx wrangler deployments list

    Expected result

    WebSocket close events cluster within seconds of a deployment timestamp.

    What happens next

    • passed step 3, Check whether gradual deployments are enabled
    • failed step 4, Disconnects don't line up with deploys — escalate
    • unknown step 3, Check whether gradual deployments are enabled
  3. Step 3 · TestCheck whether gradual deployments are enabled

    With gradual deployments, traffic is split across old and new Worker versions over an extended rollout window. A Durable Object can be restarted more than once during that window as traffic shifts, and a request on the new version can reach a Durable Object still momentarily running the old one.

    Read-onlysh
    npx wrangler deployments list

    Expected result

    A deployment showing a percentage rollout (e.g. 10% → 100% over time) rather than an instant 100% cutover.

    What happens next

    • passed step 5, Keep Worker/Durable Object API forward- and backward-compatible during rollout
    • failed step 6, Add automatic client reconnect logic
    • unknown step 6, Add automatic client reconnect logic
  4. Step 4 · EndDisconnects don't line up with deploys — escalate

    If the timing doesn't correlate with deployments, look at idle timeouts, missing ping/pong keep-alive handling, or client-side network conditions instead — this playbook only covers the deploy-triggered reset.

  5. Step 5 · FixKeep Worker/Durable Object API forward- and backward-compatible during rollout

    During a gradual deployment, code changes are released globally in an eventually-consistent manner, so a request can hit the new Worker version while its Durable Object is still on the old one for a short period. Ensure the RPC/API surface between them is compatible in both directions for the duration of the rollout, in addition to adding client reconnect logic.

    What happens next

    • passed step 7, Deploy again and observe reconnect behaviour
  6. Step 6 · FixAdd automatic client reconnect logic

    A code-update-triggered disconnect is expected platform behaviour, not a bug to eliminate. Design the client to detect the close and reconnect automatically (with backoff), and use the Hibernation API (server.accept()) so the Durable Object itself doesn't need to hold the connection object in memory between messages — that reduces unrelated evictions, though it does not prevent a disconnect caused by an actual code update.

    Read-onlyts
    function connect() {
      const ws = new WebSocket("wss://your-worker.example.workers.dev/room");
      ws.addEventListener("close", () => {
        setTimeout(connect, 1000 + Math.random() * 1000);
      });
      return ws;
    }

    What happens next

    • passed step 7, Deploy again and observe reconnect behaviour
  7. Step 7 · Verify the fixDeploy again and observe reconnect behaviour

    Deploy with the reconnect logic in place and confirm connected clients reconnect within your expected window instead of the application breaking.

    Changes statesh

    Changes system or service state. Review before running.

    Deploys the new Worker/Durable Object code to production, which will itself trigger the same reset being tested for.

    npx wrangler deploy

    Expected result

    Clients briefly disconnect and reconnect automatically within a second or two of the deploy, with no application-level errors.

    What happens next

    • passed step 8, Resolved
    • failed step 4, Disconnects don't line up with deploys — escalate
    • unknown step 4, Disconnects don't line up with deploys — escalate
  8. Step 8 · EndResolved

    Clients now recover automatically from the disconnect that deploys inherently cause.

Sources

Why this confidence?

What would strengthen it: 6 more independent reproductions. Reproductions from 3 more distinct environments.

This counts only what was recorded against revision 1 itself. Nothing reported against another revision is included here — see the revision history for why.