Skip to content

fix: error check on state#864

Open
afjcjsbx wants to merge 1 commit intosipeed:mainfrom
afjcjsbx:fix/state-error-check
Open

fix: error check on state#864
afjcjsbx wants to merge 1 commit intosipeed:mainfrom
afjcjsbx:fix/state-error-check

Conversation

@afjcjsbx
Copy link
Contributor

@afjcjsbx afjcjsbx commented Feb 27, 2026

📝 Description

Added proper error handling and logging for filesystem operations in the state manager (pkg/state/state.go).

Previously, directory creation (os.MkdirAll), state saving during migration (sm.saveAtomic), and state loading (sm.load) were executed without checking for returned errors. This PR wraps these calls in error checks and adds [WARN] logs to ensure that any filesystem issues (like permission errors or disk space issues) are properly surfaced instead of failing silently.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

📚 Technical Context (Skip for Docs)

  • Reference URL: N/A
  • Reasoning: Ignoring I/O errors during state initialization, migration, or loading can lead to inconsistent application states or silent data loss. Adding if err := ...; err != nil checks ensures operators are immediately notified of underlying environment issues via logs without necessarily crashing the application.

🧪 Test Environment

  • Hardware: PC (Local testing)
  • OS: Any supported OS
  • Model/Provider: N/A
  • Channels: N/A

📸 Evidence (Optional)

Click to view Logs/Screenshots

(Expected log output if a failure occurs)
[WARN] state: failed to create state directory: permission denied

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

Copy link
Contributor

@PixelTux PixelTux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean fix. These three error paths (os.MkdirAll, saveAtomic during migration, load) were silently discarding errors, which can mask permission problems or disk-full scenarios. Logging at WARN level without crashing is the right call since state initialization is best-effort during startup.

Two minor observations (non-blocking):

  1. In the migration path, sm.saveAtomic() failure is logged but the code still proceeds to log [INFO] state: migrated state from .... The migration technically failed at that point. Consider gating the INFO log:

    if err := sm.saveAtomic(); err != nil {
        log.Printf("[WARN] state: failed to save migrated state: %v", err)
    } else {
        log.Printf("[INFO] state: migrated state from %s to %s", oldStateFile, stateFile)
    }
  2. The load() failure log says "failed to load state" but load() may return an error for "file not found" on a fresh install, which is not really a warning. Consider checking os.IsNotExist(err) before logging at WARN.

Neither blocks merging. LGTM.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants