Skip to content

Gateway shutdown/restart does not fire session_end for active sessions #57790

@GigiTiti-Kai

Description

@GigiTiti-Kai

Summary

When the OpenClaw gateway process is terminated (SIGTERM/SIGINT/kill) or restarted, session_end hooks are never fired for currently active sessions. This leaves sessions in an incomplete state in any downstream system that relies on session lifecycle hooks (e.g., claude-mem plugin).

Root Cause

session_end is only fired when a new session starts and the previous session ID differs (in auth-profiles module, around line 193458-193468):

if (previousSessionEntry?.sessionId && previousSessionEntry.sessionId !== effectiveSessionId) {
  hookRunner.runSessionEnd(payload.event, payload.context).catch(() => {});
}

When the gateway process is killed:

  1. No graceful shutdown handler iterates over active sessions
  2. No session_end hooks fire
  3. On restart, the gateway has no memory of previous sessions
  4. New sessions are created without closing the old ones

Impact

  • Downstream systems (e.g., claude-mem) accumulate ghost sessions in active state
  • Gateway restarts during development/debugging compound the problem (each restart adds more orphaned sessions)
  • In our case: 11 orphaned sessions caused agent pool exhaustion with 63 timeout errors in a single day

Expected Behavior

On gateway shutdown (SIGTERM/SIGINT), the process should:

  1. Iterate over all active sessions
  2. Fire session_end for each
  3. Then exit

Or alternatively, provide a mechanism for plugins to register shutdown handlers.

Workaround

We've added a cron-based cleanup that marks stale active sessions as completed in the database. This is a band-aid, not a fix.

Environment

  • OpenClaw version: latest stable (installed via npm)
  • Platform: WSL2 (Linux)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions