Skip to content

message:sent hook stops firing after WebSocket reconnect (code 1005) #55340

Closed

Description

@aloysiusmartis

Description

The message:sent internal hook (hooks.internal.handlers) stops firing after a Discord WebSocket reconnect event (close code 1005). The hook module is loaded at gateway startup and works correctly until a WebSocket disconnection occurs. After the gateway auto-reconnects, the hook never fires again for the remainder of the process lifetime.

Reproduction

  1. Configure an internal hook handler for message:sent in openclaw.json:
"hooks": {
  "internal": {
    "enabled": true,
    "handlers": [
      {
        "event": "message:sent",
        "module": "hooks/my-hook.mjs"
      }
    ]
  }
}
  1. Add debug logging to the hook to confirm it fires:
import("node:fs").then(fs => {
  fs.appendFileSync("/tmp/openclaw-hook-debug.log",
    `[${new Date().toISOString()}] hook fired\n`
  );
});
  1. Send messages — hook fires correctly ✅
  2. Wait for a Discord WebSocket disconnect (code 1005) — these happen naturally every few hours
  3. After reconnect, send messages — hook never fires again ❌

Evidence

Debug log from production:

# Hook fires normally through 7:45 AM UTC
[2026-03-26T14:45:52.118Z] hook fired: type=message action=sent sessionKey=agent:main:discord:channel:...

# WebSocket crash at 7:34 AM PDT (14:34 UTC):
[openclaw] Uncaught exception: Error: Max reconnect attempts (0) reached after code 1005

# Zero hook fires from 7:45 AM onward despite 5+ hours of active conversation

Gateway log shows 5 WebSocket disconnects (code 1005) between 3:58 AM and 7:34 AM PDT. After the final reconnect, the hook module is effectively dead.

Expected Behavior

Internal hook handlers should survive WebSocket reconnects. Either:

  1. Re-register handlers after reconnection, or
  2. Ensure the event emitter pipeline remains intact across reconnects

Environment

  • OpenClaw version: 2026.3.24
  • Node: v22.22.0
  • OS: macOS 23.6.0 (x64)
  • Channel: Discord (WebSocket-based)

Workaround

Manual observer calls (fire-and-forget HTTP POST) as a fallback — but this is discretionary and defeats the purpose of the forced hook pattern.

Activity

  1. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    Closing this as implemented after Codex review.

    Current main already has the hook-runtime and Discord reconnect cleanup needed for this report: internal hook registrations are now process-global, outbound delivery still dispatches message:sent, and the Discord 1005 reconnect-exhausted path is covered by supervisor/lifecycle regression tests. The shipped changelog also records the hook-registry stability fix, so this looks implemented rather than an open current-main bug.

    What I checked:

    • Internal hook registry is process-global: internal-hooks.ts stores handler registrations in a globalThis singleton so registration and dispatch stay aligned even if bundling creates duplicate module copies. (src/hooks/internal-hooks.ts:189, 95b7a85f0607)
    • Trigger path still dispatches shared hook registry: triggerInternalHook() reads the shared registry and invokes message:sent listeners without any reconnect-specific reset path. (src/hooks/internal-hooks.ts:286, 95b7a85f0607)
    • Outbound sends still emit internal message:sent: Outbound delivery continues to call triggerInternalHook(createInternalHookEvent("message", "sent", ...)) for sent messages. (src/infra/outbound/deliver.ts:648, 95b7a85f0607)
    • Discord reconnect-exhausted cleanup is covered on main: Current Discord gateway supervisor/lifecycle code classifies reconnect exhaustion and regression tests cover close-code 1005 teardown/dispose handling instead of leaving a broken runtime behind. (extensions/discord/src/monitor/gateway-supervisor.test.ts:107, 95b7a85f0607)
    • Shipped changelog entry matches the fix: The changelog explicitly says the internal hook handler registry was moved to a globalThis singleton so hook registration/dispatch remain consistent across duplicate module copies, which matches this symptom pattern. (CHANGELOG.md:3829, a9797214338b)

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review notes: reviewed against 95b7a85f0607; fix evidence: release v2026.4.23, commit a9797214338b.

  2. locked as resolved and limited conversation to collaborators on Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions