{"record":{"id":"403035382f99f6c3","repo":"nikivdev/code","slug":"error-reading-log-file","errorCode":null,"errorMessage":"Error reading log file: {}","messagePattern":"Error reading log file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/processes.rs","lineNumber":565,"sourceCode":"    let mut file = File::open(path).context(\"failed to open log file\")?;\n    file.seek(SeekFrom::End(0))?;\n\n    if !quiet {\n        println!(\"\\n--- Following {} (Ctrl+C to stop) ---\", path.display());\n    }\n\n    let mut buf = vec![0u8; 4096];\n    loop {\n        match file.read(&mut buf) {\n            Ok(0) => {\n                // No new data, sleep and retry\n                thread::sleep(Duration::from_millis(100));\n            }\n            Ok(n) => {\n                print!(\"{}\", String::from_utf8_lossy(&buf[..n]));\n            }\n            Err(e) => {\n                bail!(\"Error reading log file: {}\", e);\n            }\n        }\n    }\n}\n\n/// Fetch and display logs for a hub task by ID\nfn show_hub_task_logs(task_id: &str, follow: bool) -> Result<()> {\n    use reqwest::blocking::Client;\n    use serde::Deserialize;\n\n    const HUB_HOST: &str = \"127.0.0.1\";\n    const HUB_PORT: u16 = 9050;\n\n    #[derive(Debug, Deserialize)]\n    struct TaskLog {\n        id: String,\n        name: String,\n        command: String,","sourceCodeStart":547,"sourceCodeEnd":583,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/processes.rs#L547-L583","documentation":"In tail_follow, the loop reading the log file bails if a read(2) on the log file fails (Err(e) from read). This surfaces OS-level read errors — not EOF, which is handled by polling — such as the file being deleted/truncated underneath the tailer or a permission change.","triggerScenarios":"While following a log, the file read returns an error: file deleted (on filesystems where deletion still yields errors via the open handle it usually becomes EOF, but truncation/rotation can error), descriptor closed, or I/O error from the filesystem (EIO, EACCES after chmod).","commonSituations":"Log rotation that truncates or replaces the file mid-follow, another process deleting the log, disk errors, or permissions tightened while the process runs.","solutions":["Check the file still exists and is readable: `ls -l <log_path>`; recreate or restore permissions if needed.","If log rotation caused it, restart `f logs --follow` after rotation completes.","Look at the underlying OS error in the message (e.g. 'No such file or directory', 'Permission denied') and address that cause.","Rerun the logs command to reopen the file once the filesystem state is sane."],"exampleFix":"// before\nErr(e) => {\n    bail!(\"Error reading log file: {}\", e);\n}\n// after\nErr(e) if e.kind() == std::io::ErrorKind::NotFound => {\n    // log rotated/deleted: wait for it to reappear instead of dying\n    thread::sleep(Duration::from_millis(100));\n    continue;\n}\nErr(e) => {\n    bail!(\"Error reading log file: {}\", e);\n}","handlingStrategy":"try-catch","validationCode":"// ensure the log file is readable and stable before tailing\nlet meta = std::fs::metadata(&log_path)?;\nif meta.len() == 0 { /* may still appear; proceed */ }\nlet f = std::fs::File::open(&log_path)?;","typeGuard":null,"tryCatchPattern":"match tail_follow(&log_path) {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"Error reading log file\") => {\n        eprintln!(\"log vanished or I/O error: {e}; reattaching...\");\n        // reopen and retry once\n        tail_follow(&log_path)?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Avoid deleting or rotating the log file while `f logs --follow` is running.","Use copytruncate-safe rotation or restart the follower after rotation.","Keep the log path writable by the user running the tail.","Monitor disk health if EIO errors recur."],"tags":["io","logs","file-read","tail"],"backgroundTag":"file-read-failure","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}