{"record":{"id":"465f286dc5262b05","repo":"neondatabase/neon","slug":"parse-pidfile-content-to-pid","errorCode":null,"errorMessage":"parse pidfile content to PID","messagePattern":"parse pidfile content to PID","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"libs/utils/src/pid_file.rs","lineNumber":161,"sourceCode":"        LockFileRead::LockedByOtherProcess {\n            not_locked_file: _not_locked_file,\n            content,\n        } => {\n            // XXX the read races with the write in claim_pid_file_for_pid().\n            // But pids are smaller than a page, so the kernel page cache will lock for us.\n            // The only problem is that we might get the old contents here.\n            // Can only fix that by implementing some scheme that downgrades the\n            // exclusive lock to shared lock in claim_pid_file_for_pid().\n            PidFileRead::LockedByOtherProcess(parse_pidfile_content(&content)?)\n        }\n    };\n    Ok(ret)\n}\n\nfn parse_pidfile_content(content: &str) -> anyhow::Result<Pid> {\n    let pid: i32 = content\n        .parse()\n        .map_err(|_| anyhow::anyhow!(\"parse pidfile content to PID\"))?;\n    if pid < 1 {\n        anyhow::bail!(\"bad value in pidfile '{pid}'\");\n    }\n    Ok(Pid::from_raw(pid))\n}\n","sourceCodeStart":143,"sourceCodeEnd":167,"githubUrl":"https://github.com/neondatabase/neon/blob/8f60b04da47ffefe0e52bda2440134b42874eb75/libs/utils/src/pid_file.rs#L143-L167","documentation":"pid_file::read parses the content of a lock-holding pidfile via parse_pidfile_content; if the bytes are not a plain decimal integer parseable as i32 (empty file, garbage text, truncated write), this error is returned instead of a PID.","triggerScenarios":"Reading a pidfile whose content is non-numeric: a file truncated by a crash between create and write_content, overwritten by other tooling, or corrupted on disk.","commonSituations":"Power loss mid-startup leaving a zero-length pidfile; humans or scripts accidentally writing to the pidfile; disk corruption.","solutions":["Inspect the file: cat <pidfile>","If no process holds the lock (service is stopped), remove the stale pidfile and restart","Prevent other tooling from writing to the pidfile path"],"exampleFix":"# before\n$ cat /var/lib/neon.pid\nnot-a-pid\n# after: with the service stopped, clear the stale file and start\n# rm /var/lib/neon.pid\nsystemctl start neon","handlingStrategy":"validation","validationCode":"fn pidfile_content_is_valid(path: &std::path::Path) -> bool {\n    std::fs::read_to_string(path)\n        .ok()\n        .and_then(|c| c.trim().parse::<i32>().ok())\n        .is_some_and(|pid| pid >= 1)\n}","typeGuard":"fn is_pidfile_parse_error(err: &anyhow::Error) -> bool {\n    err.to_string().contains(\"parse pidfile content to PID\")\n}","tryCatchPattern":"match pid_file::read(&path) {\n    Err(e) if e.to_string().contains(\"parse pidfile content to PID\") => {\n        // content is garbage; if no process holds the flock, treat as stale and remove\n    }\n    other => other?,\n}","preventionTips":["Never hand-edit or append to pidfiles","Write pidfiles atomically (the library writes under flock)","Treat non-numeric pidfile content as stale-state and clean it while stopped"],"tags":["rust","pidfile","filesystem","parsing"],"backgroundTag":"invalid-pidfile","analyzedSha":"8f60b04da47ffefe0e52bda2440134b42874eb75","analyzedAt":"2026-08-16T23:39:28.135Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}