-
Notifications
You must be signed in to change notification settings - Fork 6.3k
fix(storage): handle empty JSON files gracefully #7888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
fix(storage): handle empty JSON files gracefully #7888
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate Found: PR #7618: fix(sdk): improve JSON parsing error handling with contextual messages Why it's related: This PR also addresses JSON parsing errors in the SDK. Both PRs are fixing issues with JSON parsing in the SDK client, though PR #7618 appears to focus on error handling with contextual messages while PR #7888 (the current PR) specifically handles empty response bodies. These may be tackling related or overlapping issues in the same codebase area. |
Fixes 'Unexpected end of JSON input' error when server returns empty body.
Root cause: OPTIONS handler at /zen/v1/models returned status 200 with null body,
but SDK only handled empty responses for status 204 or Content-Length: 0.
Changes:
- Server: OPTIONS handler now returns 204 (No Content) instead of 200
- SDK (v1 & v2): JSON parsing now reads text first and handles empty bodies
by returning {} instead of failing on response.json()
2ed9637 to
2b848ef
Compare
|
Created issue #7889 to track this bug. PR description updated with |
After Anthropic OAuth login, sync.data.agent is empty while data loads. This caused crashes when accessing local.agent.current().name before agents were populated. Changes: - Make agentStore.current nullable with optional chaining on init - Add fallback to first agent in current() when not found - Add early return guards in submit(), cycle(), cycleFavorite(), set() - Add null checks in highlight(), spinnerDef() memos - Use optional chaining in JSX for agent name display
|
@coleleavitt the client crash will be fixed upstream hey-api/openapi-ts#3202 |
- Check for empty files before parsing to avoid 'Unexpected end of JSON input' error - Throw NotFoundError instead of JSON parse error for empty files - Apply fix to both read() and update() functions
- Add null byte/control character detection in storage read/update - Wrap JSON.parse in try-catch with descriptive error messages - Make stats command resilient to corrupted files by catching errors - Fixes 'JSON Parse error: Unterminated string' crashes in stats command
Additional Fix: Corrupted File DetectionAdded comprehensive error handling for corrupted JSON files containing null bytes or control characters. New Changes (commit 061652e)Problem Found:
Solution:
Testing:
This prevents the "JSON Parse error: Unterminated string" crash that occurred with corrupted storage files. |
|
@coleleavitt do you think this check also belongs upstream? |
Fixes #7889
Fixes #7715
Summary
Fixed "Unexpected end of JSON input" and "Unterminated string" errors in storage and SDK client.
1. SDK Client (both v1 and v2
client.gen.ts)2. Storage Layer - Empty Files (
storage.ts- commit 23848ed)read()andupdate()functionsNotFoundErrorinstead of JSON parse error3. Storage Layer - Corrupted Files (
storage.ts- commit 061652e)statscommand resilient to corrupted filesRoot Cause (Corrupted Files)
Found 3 production storage files containing 1518 null bytes each instead of valid JSON:
.trim()check only catches whitespace, not null bytesJSON.parse()crashed with "Unterminated string" errorstatscommand failed becausePromise.all()had no error handlingChanges
SDK Client
Storage - Empty File Check
Storage - Corruption Detection (NEW)
Stats Command Resilience (NEW)
Testing
opencode-dev statsworks (processed 4,773 sessions successfully)Files Changed
packages/opencode/src/storage/storage.ts- Empty + corrupted file handlingpackages/opencode/src/cli/cmd/stats.ts- Resilient error handling