Skip to content

fix: prevent watchdog clock reset during reconnect stall cycles - #51252

Closed
yetval wants to merge 8 commits into
openclaw:mainfrom
yetval:fix/discord-reconnect-watchdog-reset
Closed

yetval wants to merge 8 commits into
openclaw:mainfrom
yetval:fix/discord-reconnect-watchdog-reset

Conversation

@yetval

@yetval yetval commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #51190

The reconnect stall watchdog was being disarmed on every "WebSocket connection opened" debug event and re-armed (resetting its clock) on every close/stall. This meant the 5-minute deadline never expired during repeated open-without-READY cycles, leaving the channel stuck in running=true / connected=false indefinitely.

  • Only arm the watchdog on the first disconnect (isArmed() guard)
  • Disarm only when isConnected is confirmed true (READY/RESUMED), not on WS open
  • Same isArmed() guard on the stall-handler arm

@greptile-apps

greptile-apps Bot commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a watchdog clock-reset bug in the Discord gateway reconnect lifecycle. The reconnect stall watchdog (reconnectStallWatchdog) was previously armed on every "WebSocket connection closed" event and disarmed on every "WebSocket connection opened" event — regardless of whether READY/RESUMED had been confirmed. During open-without-READY cycles, this meant the 5-minute deadline was perpetually reset, leaving the channel stuck with running=true / connected=false indefinitely.

The fix introduces two isArmed() guards so that:

  • The watchdog is armed only once (on the first disconnect after a confirmed-connected state), and subsequent close events during a stall cycle leave the existing clock intact.
  • The watchdog is disarmed only when gateway.isConnected is confirmed true (i.e., after READY/RESUMED), not on bare WS open.

A new regression test directly exercises the 8-open-without-READY scenario and asserts the watchdog fires after the expected 5-minute deadline.

Key changes:

  • "WebSocket connection closed" handler: disarm + re-arm only when gateway.isConnected was true (clean reconnect); otherwise skip re-arm if already armed.
  • "WebSocket connection opened" handler: remove the unconditional disarm() call; only disarm inside the if (sawConnected) branch.
  • Hello-timeout stall handler: wrap arm() with isArmed() guard.
  • New test: "force-stops via watchdog when WS opens repeatedly but READY never arrives".

Confidence Score: 5/5

  • This PR is safe to merge — it correctly fixes the watchdog reset bug without introducing new edge cases.
  • The logic change is minimal and precise: the three isArmed() guards and the moved disarm() call together correctly preserve the watchdog deadline across repeated open-without-READY cycles. All existing test cases continue to exercise the disarm-on-READY and force-stop-on-timeout paths, and the new test directly covers the reported bug. No unguarded state mutation, no new timer allocation, and cleanup in the finally block is untouched.
  • No files require special attention.

Last reviewed commit: "Merge branch 'main' ..."

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b28ffc24a9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +195 to +196
if (!reconnectStallWatchdog.isArmed()) {
reconnectStallWatchdog.arm(at);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reset the watchdog after brief READY reconnects

When a reconnect briefly reaches READY/RESUMED and then drops again before HELLO_CONNECTED_POLL_MS elapses, this close handler still sees gateway.isConnected === true (per the comment above) but now leaves the watchdog armed at the original disconnect timestamp. In that flappy-connection case, several successful reconnects can still accumulate toward the 5-minute cap and eventually raise reconnect watchdog timeout, which is a regression from the previous arm(at) behavior that effectively reset the clock on each successful reconnect cycle.

Useful? React with 👍 / 👎.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: XS labels Mar 20, 2026
@yetval yetval changed the title fix(discord): prevent watchdog clock reset during reconnect stall cycles fix: prevent watchdog clock reset during reconnect stall cycles Mar 20, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 05235acd4f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@yetval

yetval commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai recheck my code

@yetval
yetval force-pushed the fix/discord-reconnect-watchdog-reset branch from 05235ac to 057ebf9 Compare March 29, 2026 22:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 057ebf9925

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +328 to +330
let sawConnected = params.gateway?.isConnected === true;
if (sawConnected) {
reconnectStallWatchdog.disarm();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Disarm watchdog as soon as socket opens

Fresh evidence from this patch: the unconditional disarm on "WebSocket connection opened" was removed and disarm now waits for sawConnected/the 250ms poll. In reconnects that recover just before the 5-minute deadline (READY flips gateway.isConnected shortly after open), reconnectStallWatchdog can remain armed long enough for its 5s checker (src/channels/transport/stall-watchdog.ts) to fire first and force-stop a connection that already recovered, because this branch keeps the old disconnect timestamp until the next poll tick.

Useful? React with 👍 / 👎.

@yetval

yetval commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Friendly bump — still open for review!

yetval and others added 4 commits April 3, 2026 11:55
Resolves conflicts from the carbon-native reconnect refactor (openclaw#59019):
- Remove provider.lifecycle.reconnect.ts (deleted in refactor)
- Accept rewritten provider.lifecycle.test.ts (watchdog tests no longer apply)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c598af5249

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +270 to +272
if (params.gateway?.isConnected) {
reconnectStallWatchdog.arm(at);
} else if (!reconnectStallWatchdog.isArmed()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid resetting reconnect watchdog on every close event

The new close-handler logic still resets the watchdog clock whenever gateway.isConnected is truthy (reconnectStallWatchdog.arm(at)), even if the watchdog is already armed. In reconnect flapping where close debug events keep arriving with isConnected === true (the test suite already models close events while isConnected remains true), each close refreshes lastActivityAt, so the 5-minute watchdog may never expire and the monitor can stay disconnected indefinitely instead of force-stopping.

Useful? React with 👍 / 👎.

markr9805 pushed a commit to markr9805/openclaw that referenced this pull request Sep 6, 2026
…122950

Add a disconnection watchdog to createGatewayStatusObserver in
provider.lifecycle.ts — the existing lifecycle owner. When the gateway
closes, the watchdog starts polling isConnected. If the gateway stays
disconnected longer than runtimeReadyTimeoutMs, it calls triggerForceStop,
which goes through the existing lifecycle cleanup path (waitForDiscordGatewayStop
→ finishReject → proper disconnect and listener cleanup).

The watchdog self-clears when the gateway reconnects (isConnected becomes
true) or when a new socket opens (Gateway websocket opened debug event).

This moves recovery from a parallel timer in the lower-level gateway module
to the existing provider lifecycle owner, as ClawSweeper requested. The
existing startReadyWatch already handles 'opened but no READY' — this
extends coverage to 'closed and reconnect stalled'.

Removed the previous GatewayConnectionWatchdog from gateway.ts and
gateway-lifecycle.ts (net -223 lines of old approach).

Related: openclaw#51190 (same symptom, fixed in lifecycle owner by openclaw#51252),
openclaw#120222 (event-loop stall root cause), openclaw#112698 (Codex app-server stall).

Closes openclaw#122950
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Discord channel can show running=true while connected=false, and new DMs stop reaching the agent until gateway restart

1 participant