Skip to content

[Bug]: no_agent cron sessions not closed — state.db accumulates zombie sessions #41935

Closed as not planned

Description

@r643584644-arch

title: "[Bug]: no_agent cron sessions not closed — state.db accumulates zombie sessions"

[Bug]: no_agent cron sessions not closed — state.db accumulates zombie sessions

Summary

When no_agent=True (script-mode) cron jobs run, the SQLite session row is
never closed: ended_at stays NULL, end_reason stays NULL, and the session
continues to look "active" to every tool that reads state.db.

This accumulates zombie sessions in state.db until external cleanup runs.

Root cause (in hermes 0.16.0, commit b1e399d)

cron/scheduler.py::_run_job_impl (line 1310) has a no_agent short-circuit
that runs the script via _run_job_script and returns early, before the
outer _run_job finally block (lines 1921-1942) gets a chance to call
end_session.

def _run_job_impl(job: dict) -> tuple[bool, str, str, Optional[str]]:
    # ...
    if job.get("no_agent"):
        script_path = job.get("script")
        if not script_path:
            return False, "", "", "no_agent=True but no script is set"
        try:
            ok, output = _run_job_script(script_path)
        finally:
            if _prior_cwd is not None:
                os.chdir(_prior_cwd)
        return ok, output, "", None  # ← early return skips outer finally

The outer finally is the only place that calls end_session. So
no_agent=True jobs never get their session row closed.

Repro

hermes cron add --script /tmp/quick.py --no-agent --schedule "*/5 * * * *"
# let it run 5+ times
sqlite3 ~/.hermes/state.db \
  "SELECT COUNT(*) FROM sessions WHERE source='cron' AND ended_at IS NULL"
# expected: 0   (the row should be closed once the script returns)
# actual:   5   (and growing every tick)

In our installation we saw 8 cron sessions idle 8–16 h, all with ended_at = NULL,
even though every job's script completed in <30 s. This breaks /sessions
counts, hermes cron list active-job tallies, and any external dashboard
that joins sessions on ended_at.

Suggested fix

In _run_job_impl, when no_agent=True finishes, still call end_session
with the script's exit info before returning. Two minimal options:

  1. Move the end_session call (or its body) into the no_agent branch so
    it runs from _run_job_impl's own finally — script's exit code becomes
    end_reason.

  2. Wrap the no_agent block in a try/finally that calls
    session_db.end_session(...) with the captured exit info, then re-raise
    the early return.

We'd prefer option 1 — it keeps the contract ("every cron job ends with a
closed session row") invariant regardless of no_agent mode.

Workaround (until fixed)

We run an external cleanup cron that sets ended_at on zombie rows:

# 1 h idle cron + ended_at NULL → close as cron_stale_cleanup
# 24 h idle human + ended_at NULL → close as stale_human_cleanup
UPDATE sessions SET ended_at = ?, end_reason = 'cron_stale_cleanup'
 WHERE source = 'cron' AND ended_at IS NULL AND started_at < ?

That workaround is what makes this tolerable, but it's not a substitute for
the underlying bug — the session row should close at job exit, not an hour
later.

Environment

  • hermes-agent: 0.16.0 (commit b1e399d)
  • Python: 3.11
  • OS: Windows 11 + WSL2 (both observed)
  • Cron mode: no_agent=True with script set

Activity

  1. added
    type/bugSomething isn't working
    P2Medium — degraded but workaround exists
    comp/cronCron scheduler and job management
    on Jun 8, 2026
  2. liuhao1024 commented on Jun 8, 2026

    @liuhao1024

    This is fixed by #41969 — closes the session row in state.db for no_agent script-mode cron jobs during mark_job_run(), preventing zombie session accumulation.

  3. konsisumer commented on Jul 7, 2026

    @konsisumer

    These issues look like facets of one underlying problem: #41935, #42851, #44080.

    They were grouped by a semantic pass as different symptoms of one underlying problem in comp/cron. A single coordinated fix may resolve them together — flagging for visibility; no action has been taken on the individual issues.

    Automated root-cause grouping. Reply to correct if these are unrelated.

  4. teknium1 commented on Jul 14, 2026

    @teknium1
    Collaborator

    Automated hermes-sweeper review: this zombie-session report no longer reproduces on current main.

    • cron/scheduler.py:2514-2516 intentionally places the no_agent short-circuit before SessionDB initialization; the script-only branch returns at :2608.
    • SessionDB is imported only in the subsequent LLM path at cron/scheduler.py:2616-2623, so a no_agent tick cannot create a source='cron' SessionDB row that could remain unclosed.
    • This behavior was made intentional by 2008f542121462caeefc3559216a129cc39021ae (fix(cron): lazy-import AIAgent + SessionDB so no_agent ticks pay zero).
    • The discussion linked through [Bug]: Desktop GUI shows no execution status/output after manually triggering no_agent cron jobs #44080 identifies missing no-agent run-history/UI records as a separate concern; it is not evidence of zombie SessionDB rows.

    The open #41969 proposal would reintroduce SessionDB work to the script-only path, but the specific accumulation described here is obsolete on main.

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

    P2Medium — degraded but workaround existscomp/cronCron scheduler and job managementsweeper:cannot-reproduceSweeper: could not reproduce on current maintype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions