Skip to content

Conversation

@LeekJay
Copy link

@LeekJay LeekJay commented Jan 16, 2026

Summary

When a session JSON file is empty or contains invalid JSON, the entire session list fails to load with Unexpected end of JSON input error. This causes all sessions to appear missing even though valid session files exist.

Problem

  • A single corrupted/empty session file causes Storage.read() to throw an exception
  • The exception propagates up and causes the entire list() generator to fail
  • Users see "No results found" when running /sessions even though they have many valid sessions
  • This is a data loss UX issue - users think their sessions are gone

Root Cause

The list() and children() functions in packages/opencode/src/session/index.ts don't handle JSON parsing errors:

export async function* list() {
  const project = Instance.project
  for (const item of await Storage.list(["session", project.id])) {
    yield Storage.read<Info>(item)  // No error handling
  }
}

Solution

Add try-catch blocks to gracefully skip corrupted session files:

  • Log a warning for each skipped file (helps users identify corrupted files)
  • Continue processing remaining valid sessions
  • Validate that the parsed session has required fields (id)

Testing

Manually tested by:

  1. Creating an empty session JSON file in the storage directory
  2. Verifying that /sessions now shows valid sessions instead of failing
  3. Confirming warning is logged for the corrupted file

Related

This addresses the scenario described in logs like:

ERROR service=server error=Unexpected end of JSON input failed

When a session JSON file is empty or contains invalid JSON, the entire
session list fails to load with 'Unexpected end of JSON input' error.
This causes all sessions to appear missing even though valid session
files exist.

This fix adds try-catch blocks to the list() and children() functions
to gracefully skip corrupted session files instead of failing the
entire operation. A warning is logged for each skipped file to help
users identify and clean up corrupted files.

Fixes the issue where a single corrupted session file prevents all
sessions from being displayed.
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search results, I found several related PRs that address similar JSON parsing and error handling issues:

Potential Related PRs:

  1. fix(storage): handle empty JSON files gracefully #7888 - fix(storage): handle empty JSON files gracefully

  2. fix(sdk): improve JSON parsing error handling with contextual messages #7618 - fix(sdk): improve JSON parsing error handling with contextual messages

    • Related: addresses JSON parsing error handling, though in SDK context
  3. fix: handle empty JSON responses in sdk client #8027 - fix: handle empty JSON responses in sdk client

    • Related: handles empty JSON responses, which could overlap with empty session file handling
  4. fix(tui): handle JSON parse error on double-escape abort #7959 - fix(tui): handle JSON parse error on double-escape abort

    • Tangentially related: addresses JSON parse errors in a different context
  5. fix: add error handling to RPC system to prevent crashes on session abort #7939 - fix: add error handling to RPC system to prevent crashes on session abort

    • Tangentially related: error handling to prevent crashes from session issues

Note: PR #7888 is particularly relevant as it appears to address the exact same issue (empty JSON files) at the storage layer, which may overlap with the scope of PR #8825.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant