Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Recovery Patterns

This page shows how to handle each family of errors that can occur when resuming a read

Outdated State

Rust Error::Mismatch with Mismatch::Path, Size, Mtime, or Missing Python StateOutdatedError

The file changed in a way that is typically benign: it moved, grew, or was touched. Resync the state and retry:

Rust

use preader::{Error, Mismatch};

match reader.lines(file).state("job-1").build() {
    Err(
        error @ Error::Mismatch {
            kind: Mismatch::Path | Mismatch::Size | Mismatch::Mtime | Mismatch::Missing,
            ..
        },
    ) => {
        eprintln!("{error}, resyncing");
        reader.states().resync("job-1", file)?;
        reader.lines(file).state("job-1").build()
    }
    result => result,
}

Python

from preader import StateOutdatedError

try:
    lines = reader.lines(file, state="job-1")
except StateOutdatedError as error:
    print(f"{error}, resyncing")
    reader.states.resync(error.name, file)
    lines = reader.lines(file, state="job-1")

See Resync for the full pattern including file moves and growth

Replaced File

Rust Error::Mismatch with Mismatch::Identity Python StateMismatchError

The file content has been replaced. The first 4096 bytes no longer match the saved fingerprint, so the saved position is meaningless. Delete the state and start over:

Rust

use preader::{Error, Mismatch};

match reader.lines(file).state("job-1").build() {
    Err(Error::Mismatch { kind: Mismatch::Identity, .. }) => {
        reader.states().delete("job-1")?;
        reader.lines(file).state("job-1").build()
    }
    result => result,
}

Python

from preader import StateMismatchError, StateOutdatedError

try:
    lines = reader.lines(file, state="job-1")
except StateOutdatedError:
    raise  # handle separately, see Outdated State above
except StateMismatchError:
    reader.states.delete("job-1")
    lines = reader.lines(file, state="job-1")

Corrupt State

Rust Error::Corrupt Python StateCorruptError

The state file was manually edited or is damaged. Delete it and start over:

Rust

use preader::Error;

match reader.lines(file).state("job-1").build() {
    Err(Error::Corrupt { .. }) => {
        reader.states().delete("job-1")?;
        reader.lines(file).state("job-1").build()
    }
    result => result,
}

Python

from preader import StateCorruptError

try:
    lines = reader.lines(file, state="job-1")
except StateCorruptError:
    reader.states.delete("job-1")
    lines = reader.lines(file, state="job-1")

Stale State

Rust Error::Mismatch with Mismatch::Stale Python StateConflictError

Another process or iterator saved the state since you last loaded it. Your save would overwrite those changes:

Rust

use preader::{Error, Mismatch};

match lines.save() {
    Err(Error::Mismatch { kind: Mismatch::Stale, .. }) => {
        let fresh = reader.states().load("job-1")?;

        eprintln!("conflict: state was updated to byte {}", fresh.position);
    }
    result => { result?; }
}

Python

from preader import StateConflictError

try:
    lines.save()
except StateConflictError:
    fresh = reader.states.load("job-1")
    print(f"conflict: state was updated to byte {fresh.position}")

StateConflictError is raised by save(), not by iterator creation. It means your save would overwrite changes made by another writer