router-for-me/CLIProxyAPI · error
failed to write token to file: %w
Error message
failed to write token to file: %w
What it means
json.NewEncoder(f).Encode(data) failed writing the serialized token to the opened file — an I/O error during the actual write or flush, not during open. Common causes: disk full (ENOSPC), quota exceeded, or the filesystem being remounted read-only after open. Because the encoder streams directly to the file, a mid-write failure can leave a truncated (invalid) JSON file behind.
Source
Thrown at internal/auth/codex/token.go:81
// Merge metadata using helper
data, errMerge := misc.MergeMetadata(ts, ts.Metadata)
if errMerge != nil {
return fmt.Errorf("failed to merge metadata: %w", errMerge)
}
f, err := os.Create(authFilePath)
if err != nil {
return fmt.Errorf("failed to create token file: %w", err)
}
defer func() {
if errClose := f.Close(); errClose != nil {
log.Errorf("codex token storage: close token file error: %v", errClose)
}
}()
if err = json.NewEncoder(f).Encode(data); err != nil {
return fmt.Errorf("failed to write token to file: %w", err)
}
return nil
}
View on GitHub (pinned to 78f0c4079e)
Solutions
- Check space: `df -h <auth-dir>` and inodes with `df -i <auth-dir>`; free space if full.
- After fixing, verify the auth file is valid JSON (a truncated write leaves a corrupt file) and re-login/refresh if not.
- Raise disk quota / enlarge the container volume.
- Alert on disk usage for the volume hosting auths/ so credential saves never hit ENOSPC.
- Consider a storage backend (Postgres/object store) where writes are transactional if local-disk truncation is a recurring risk.
Example fix
# before: auths on a full rootfs df -h / # 100% # after: free space or move auth-dir to a healthy volume cli-proxy-api --config config.yaml # with auth-dir: /data/auths
Defensive patterns
Strategy: validation
Validate before calling
// Check writable space before persisting credentials
var stat syscall.Statfs_t
if err := syscall.Statfs(filepath.Dir(path), &stat); err == nil {
free := stat.Bavail * uint64(stat.Bsize)
if free < 1<<20 { // < 1 MiB
return errors.New("auth volume nearly full; token save would be truncated")
}
} Try / catch
if err := ts.SaveTokenToFile(path); err != nil {
if strings.Contains(err.Error(), "failed to write token to file") {
// ENOSPC/quota: free space, then re-save; verify file is valid JSON afterwards
}
} Prevention
- Monitor disk space on the volume hosting auths/.
- After any failed save, validate the auth file with json.Valid before reusing it.
- Keep credentials on a volume with headroom or use a database/object-store backend.
When it happens
Trigger: Disk full at the moment of saving credentials; per-user quota hit on the volume; file opened then the underlying device/vanished (ejected volume, dead NFS server); fcntl locks not a factor but VM disk limits are.
Common situations: Small container filesystems filling up with logs; long-running servers whose volume hits quota right when a refreshed token is persisted; network storage blipping mid-write.
Related errors
- failed to create directory: %v
- failed to create token file: %w
- failed to save refreshed auth: %w
- failed to read auth file: %w
- failed to create directory: %v
AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15).
Data as JSON: /api/errors/ac98952f275c7462.
Report an issue: GitHub.