Skip to content

fix(discord): replace gateway READY retry fixed backoff with exponential backoff - #104339

Closed
bombskyhao wants to merge 4 commits into
openclaw:mainfrom
bombskyhao:fix/discord-gateway-exponential-backoff
Closed

bombskyhao wants to merge 4 commits into
openclaw:mainfrom
bombskyhao:fix/discord-gateway-exponential-backoff

Conversation

@bombskyhao

Copy link
Copy Markdown

What Problem This Solves

Discord gateway READY wait retries use a fixed 2-second backoff (DISCORD_GATEWAY_READY_RETRY_BACKOFF_MS = 2_000). When the gateway is temporarily unstable (e.g. network blips, regional latency, or transient Discord-side issues), the lifecycle loop retries with a constant 2s pause every cycle. This creates two problems:

  1. Unnecessary gateway pressure: Every 15s READY timeout is followed by a 2s backoff, then another aggressive READY poll — a steady pattern that offers Discord's gateway no recovery grace.
  2. No congestion adaptation: Fixed backoff does not distinguish between a transient hiccup (where a short delay is fine) and a sustained outage (where rapid retries waste resources and may trigger IP-level throttling or rate limits).

Why This Change Was Made

Exponential backoff is a widely-adopted congestion-control pattern — it starts small (2s) and doubles with each consecutive failure (2s→4s→8s→16s→30s cap). This:

  • Follows existing project patterns: The browser extension relay (relay-core.js) already uses the same 1s→2s→4s→...→30s cap strategy for WebSocket reconnection.
  • Gives transient issues room to self-heal: Short backoff for the first retry; longer pauses let intermittent problems resolve before the next attempt.
  • Protects against sustained failures: The 30s cap bounds the maximum wait while still providing exponential growth, preventing both runaway retries and IP-level rate limiting.

Scope: Single file (extensions/discord/src/monitor/provider.lifecycle.ts), ~12 lines changed. No structural changes — only the backoff strategy.

User Impact

  • Before: Gateway lifecycle retries on READY timeout with a constant 2s pause between attempts, regardless of failure count.
  • After: Backoff grows exponentially: 2s, 4s, 8s, 16s, then 30s cap. First retry response is identical (2s); only repeated failures see longer waits.
  • Unchanged: READY timeout (15s default), socket drain/terminate timeouts, and overall lifecycle logic remain unaffected.

Evidence

Test Results

$ pnpm vitest run --config test/vitest/vitest.extension-discord.config.ts \
    extensions/discord/src/monitor/provider.lifecycle.test.ts

 ✓  extension-discord  extensions/discord/src/monitor/provider.lifecycle.test.ts (22 tests) 6276ms

 Test Files  1 passed (1)      Tests  22 passed (22)

All 22 tests passed — no regressions. The three backoff-sensitive tests were verified to pass under the new exponential schedule:

  • "reconnects with backoff when startup never reaches READY, then recovers"
  • "waits for the stale startup socket to close before reconnecting"
  • "keeps retrying when startup still is not ready after a reconnect"

Manual Verification

  • Code change verified: backoff semantics match relay-core.js reconnectDelayMs implementation
  • Edge cases verified: getGatewayReconnectDelayMs(0) = 1s, getGatewayReconnectDelayMs(1) = 2s, getGatewayReconnectDelayMs(5) = 30s, getGatewayReconnectDelayMs(99) = 30s

Additional Checks

  • No formatting issues
  • No type errors
  • Old constant DISCORD_GATEWAY_READY_RETRY_BACKOFF_MS fully replaced, no remaining references

_This PR was created with [AI-assisted] tooling. Code changes were reviewed and validated by the author

Co-authored-by: Codex

…ial backoff

Discord gateway lifecycle used a fixed 2-second backoff for all READY timeout retries, regardless of failure count. This could create unnecessary gateway pressure and offered no congestion adaptation.

Change the backoff strategy to exponential: 2s, 4s, 8s, 16s, then capped at 30s (matching the pattern already used in extensions/browser/chrome-extension/modules/relay-core.js).

- Adds getGatewayReconnectDelayMs(attempt) with exponential growth
- Replaces DISCORD_GATEWAY_READY_RETRY_BACKOFF_MS with _INITIAL_MS + _MAX_MS constants
- First retry behavior is identical (2s); only repeated failures see longer waits
@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS labels Jul 11, 2026
@clawsweeper

clawsweeper Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the contribution. This PR still needs the requested real-behavior proof, and the branch has been idle since that ask.

Close this stalled, unproven PR. Its narrow reliability idea remains understandable, but the branch has been unchanged since July 11, 2026, has carried a real-proof request since that date, and still retains the two previously identified lifecycle defects; current main separately merged the abortable retry-sleep repair.

So I’m closing this for now to keep the review queue honest. Please reopen or open a fresh PR with real-behavior proof (a live run, logs, or a reproducible validation transcript) and it will be reviewed again.

Review details

Best possible solution:

If the reliability policy is still wanted, start from current main, wait with an AbortSignal-aware delay before initiating the next gateway restart, add focused ordering and cancellation coverage, and attach redacted runtime output showing escalating delays and recovery.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: a READY timeout follows the lifecycle restart path before reaching this PR's new delay, and the branch's timer is not abort-aware. No current-head runtime reproduction was supplied by the contributor.

Is this the best way to solve the issue?

No. Exponential backoff is a plausible policy, but this implementation waits after starting the replacement gateway and omits cancellation from the longer wait; the merged abort fix on current main is the correct base for any renewed attempt.

Security review:

Security review cleared: The one-file retry-timing diff adds no dependency, secret, permission, workflow, package-resolution, or supply-chain surface.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • NikolaFC: Authored the merged startup-readiness work that introduced the Discord READY timeout restart-and-retry supervision path. (role: introduced lifecycle behavior; confidence: high; commits: 343f859b900c; files: extensions/discord/src/monitor/provider.lifecycle.ts)
  • wangmiao0668000666: Authored the merged repair that made the READY retry-backoff sleep abortable in the same lifecycle path. (role: recent area contributor; confidence: high; commits: 8090cb1c64ea; files: extensions/discord/src/monitor/provider.lifecycle.ts)

Codex review notes: model internal, reasoning high; reviewed against 9979ee312d43.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 11, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jul 26, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 31, 2026
@clawsweeper

clawsweeper Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

@clawsweeper clawsweeper Bot closed this Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS stale Marked as stale due to inactivity status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant