openclaw - 💡(How to fix) Fix [Bug] sessions.json can become 0 bytes (empty), breaking session aggregation with no recovery mechanism [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#77783Fetched 2026-05-06 06:21:32
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
2
Timeline (top)
commented ×1cross-referenced ×1

Error Message

  • No user-facing error message logger.warn('sessions.json is empty, rebuilding...');

Root Cause

Root Cause Hypothesis

Fix Action

Workaround

# Manual recovery
echo "[]" > ~/.openclaw/state/sessions.json

# Then restart gateway
systemctl --user restart openclaw-gateway

Code Example

$ ls -la ~/.openclaw/state/sessions.json
-rw-rw-r-- 1 moltbot moltbot 0 May 5 16:58 sessions.json

$ wc -c ~/.openclaw/state/sessions.json
0 /home/moltbot/.openclaw/state/sessions.json

---

# Manual recovery
echo "[]" > ~/.openclaw/state/sessions.json

# Then restart gateway
systemctl --user restart openclaw-gateway

---

// Pseudo-code for atomic session.json update
const tmpFile = `${sessionFile}.tmp.${Date.now()}`;
fs.writeFileSync(tmpFile, JSON.stringify(sessions));
fs.renameSync(tmpFile, sessionFile); // Atomic on POSIX

---

const data = fs.readFileSync(sessionFile, 'utf8');
if (!data || data.length === 0) {
  logger.warn('sessions.json is empty, rebuilding...');
  return rebuildFromSessionDir();
}

---

function rebuildFromSessionDir() {
  const sessionFiles = fs.readdirSync(sessionsDir)
    .filter(f => f.endsWith('.jsonl'))
    .map(f => parseSessionFile(f));
  return sessionFiles;
}

---

[FAIL] sessions.json is 0 bytes (corrupted)
  Fix: Run `openclaw sessions rebuild-index`
RAW_BUFFERClick to expand / collapse

Bug Description

sessions.json can become corrupted (0 bytes), causing silent session aggregation failures. When this happens, OpenClaw cannot list or manage sessions properly, and there's no automatic recovery mechanism.

Environment

  • OpenClaw version: 2026.5.3-1
  • OS: Linux 6.8.0-110-generic (x64)
  • Node: v22.22.2
  • Gateway: systemd user service
  • Session store: ~/.openclaw/agents/main/sessions/sessions.json

Observed State

$ ls -la ~/.openclaw/state/sessions.json
-rw-rw-r-- 1 moltbot moltbot 0 May 5 16:58 sessions.json

$ wc -c ~/.openclaw/state/sessions.json
0 /home/moltbot/.openclaw/state/sessions.json

Session directory: 7,806 jsonl files (404MB total)

Symptoms

  1. Session operations fail silently

    • sessions.list may timeout or return empty
    • Session aggregation cannot complete
    • No user-facing error message
  2. SessionWriteLockTimeoutError cascades

    • Lock held for 60s (timeout)
    • Event loop starvation (ELD P99=7,772ms)
    • Discord slash commands fail ("Application did not respond")
  3. No automatic recovery

    • File remains 0 bytes indefinitely
    • Manual intervention required: echo "[]" > sessions.json

Root Cause Hypothesis

Likely caused by:

  • Concurrent write during session compaction
  • Gateway restart mid-write
  • Session file lock contention during heavy load

Impact

  • Complete session management failure - Cannot list, archive, or prune sessions
  • Cascading failures - Event loop starvation affects all channels
  • Data loss risk - If sessions.json is the source of truth for active sessions

Expected Behavior

  1. Atomic writes - Use temp file + rename pattern for sessions.json updates
  2. Corruption detection - Validate JSON on read, trigger rebuild if invalid
  3. Automatic recovery - If sessions.json is empty/invalid, rebuild from session directory scan
  4. Health check - openclaw doctor should flag empty/corrupted sessions.json

Reproduction Steps

  1. Have high session file count (~7,000+ files)
  2. Trigger session compaction or heavy concurrent operations
  3. Force gateway restart during active session write (SIGTERM)
  4. Check ~/.openclaw/state/sessions.json - may be 0 bytes

Workaround

# Manual recovery
echo "[]" > ~/.openclaw/state/sessions.json

# Then restart gateway
systemctl --user restart openclaw-gateway

Related Issues

  • #75839 - pi-trajectory-flush timeout with high session count
  • #13744 - Make session write lock configurable
  • #77716 - Discord AbortError during event loop saturation
  • #76421 - sessions.list timeout after event loop stall

Suggested Fix

  1. Write atomicity:
// Pseudo-code for atomic session.json update
const tmpFile = `${sessionFile}.tmp.${Date.now()}`;
fs.writeFileSync(tmpFile, JSON.stringify(sessions));
fs.renameSync(tmpFile, sessionFile); // Atomic on POSIX
  1. Validation on read:
const data = fs.readFileSync(sessionFile, 'utf8');
if (!data || data.length === 0) {
  logger.warn('sessions.json is empty, rebuilding...');
  return rebuildFromSessionDir();
}
  1. Rebuild function:
function rebuildFromSessionDir() {
  const sessionFiles = fs.readdirSync(sessionsDir)
    .filter(f => f.endsWith('.jsonl'))
    .map(f => parseSessionFile(f));
  return sessionFiles;
}
  1. Health check: Add to openclaw doctor:
[FAIL] sessions.json is 0 bytes (corrupted)
  Fix: Run `openclaw sessions rebuild-index`

Evidence from 2026-05-05 Incident

  • sessions.json: 0 bytes (corrupted)
  • Session files: 7,806 jsonl files
  • SessionWriteLockTimeoutError at 17:35:58 and 18:23:26
  • Event loop utilization: 95.8%
  • Discord slash commands: "Application did not respond"
  • Fixed by: echo "[]" > sessions.json + gateway restart

extent analysis

TL;DR

Implement atomic writes for sessions.json and add corruption detection to prevent silent session aggregation failures.

Guidance

  1. Atomic writes: Use a temporary file and rename pattern for sessions.json updates to prevent data corruption during concurrent writes or gateway restarts.
  2. Corruption detection: Validate JSON on read and trigger a rebuild if the file is invalid or empty to ensure data integrity.
  3. Automatic recovery: Rebuild sessions.json from the session directory scan if the file is empty or invalid to prevent complete session management failure.
  4. Health check: Add a check to openclaw doctor to flag empty or corrupted sessions.json files and provide a fix command.

Example

// Pseudo-code for atomic session.json update
const tmpFile = `${sessionFile}.tmp.${Date.now()}`;
fs.writeFileSync(tmpFile, JSON.stringify(sessions));
fs.renameSync(tmpFile, sessionFile); // Atomic on POSIX

Notes

The provided pseudo-code and suggested fixes are based on the information given in the issue and may require modifications to fit the exact implementation details of OpenClaw.

Recommendation

Apply the workaround by manually recovering sessions.json and restarting the gateway, and then implement the suggested fixes for atomic writes, corruption detection, and automatic recovery to prevent future occurrences.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug] sessions.json can become 0 bytes (empty), breaking session aggregation with no recovery mechanism [1 comments, 2 participants]