use crate::paths;
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PidRecord {
#[serde(default = "default_schema_version")]
pub schema_version: u32,
pub pid: u32,
#[serde(default)]
pub start_token: Option<String>,
#[serde(default)]
pub nonce: Option<String>,
}
fn default_schema_version() -> u32 {
1
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedPidFile {
Structured(PidRecord),
LegacyPid(u32),
Empty,
Malformed(String),
}
impl PartialEq for PidRecord {
fn eq(&self, other: &Self) -> bool {
self.schema_version == other.schema_version
&& self.pid == other.pid
&& self.start_token == other.start_token
&& self.nonce == other.nonce
}
}
impl Eq for PidRecord {}
pub fn parse_pid_file(path: &Path) -> ParsedPidFile {
let raw = match fs::read_to_string(path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return ParsedPidFile::Empty;
}
Err(_) => return ParsedPidFile::Empty,
};
let trimmed = raw.trim();
if trimmed.is_empty() {
return ParsedPidFile::Empty;
}
if let Ok(pid) = trimmed.parse::<u32>() {
return ParsedPidFile::LegacyPid(pid);
}
match toml::from_str::<PidRecord>(trimmed) {
Ok(rec) => ParsedPidFile::Structured(rec),
Err(e) => ParsedPidFile::Malformed(e.to_string()),
}
}
pub fn reconcile_pid_under_lock(state_dir: &Path) -> Result<(), String> {
let path = state_dir.join("snip-sync.pid");
if !path.exists() {
return Ok(());
}
let parsed = parse_pid_file(&path);
match parsed {
ParsedPidFile::Structured(rec) => {
if record_still_matches(&rec) {
return Err(format!(
"snip-sync server already running with PID {}. Use 'snip-sync stop' first.",
rec.pid
));
}
tracing::warn!(
"Found stale structured PID file for process {}. It will be replaced.",
rec.pid
);
Ok(())
}
ParsedPidFile::LegacyPid(pid) => {
if is_running(pid) {
#[cfg(unix)]
{
if !validate_process_name(pid) {
return Err(format!(
"Legacy PID file references PID {} but process name cannot be verified as snip-sync. Refusing to start; investigate manually or remove the file.",
pid
));
}
return Err(format!(
"Legacy snip-sync server appears to be running with PID {}. Stop it before starting a new server.",
pid
));
}
#[cfg(not(unix))]
{
return Err(format!(
"Legacy PID file references PID {} but Windows process verification cannot confirm snip-sync. Refusing to start; investigate manually or remove the file.",
pid
));
}
}
tracing::warn!(
"Found legacy PID file for dead process {}. It will be replaced.",
pid
);
Ok(())
}
ParsedPidFile::Empty => {
tracing::warn!("PID file is empty; it will be replaced.");
Ok(())
}
ParsedPidFile::Malformed(msg) => {
tracing::warn!("PID file is malformed ({}); it will be replaced.", msg);
Ok(())
}
}
}
pub struct PidGuard {
path: PathBuf,
record: PidRecord,
}
impl PidGuard {
pub fn pid(&self) -> u32 {
self.record.pid
}
}
impl Drop for PidGuard {
fn drop(&mut self) {
if let Some(current) = read_pid_at(&self.path)
&& current.pid == self.record.pid
&& current.start_token == self.record.start_token
&& current.nonce == self.record.nonce
{
let _ = remove_pid_atomic(&self.path);
}
}
}
pub fn write_pid() -> Result<PidGuard, String> {
let pid = std::process::id();
let path = paths::pid_path();
let nonce = generate_nonce();
let record = PidRecord {
schema_version: 1,
pid,
start_token: get_process_start_token(pid),
nonce: Some(nonce),
};
write_pid_atomic(&path, &record)?;
Ok(PidGuard { path, record })
}
fn write_pid_atomic(path: &Path, record: &PidRecord) -> Result<(), String> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("Failed to create pid dir: {}", e))?;
}
let serialized = toml::to_string_pretty(record)
.map_err(|e| format!("Failed to serialize PID record: {e}"))?;
let temp_path = unique_temp_path(path);
{
let mut options = fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
options.mode(0o600);
}
let mut file = options.open(&temp_path).map_err(|e| {
format!(
"Failed to create temp PID file {}: {}",
temp_path.display(),
e
)
})?;
#[cfg(unix)]
restrict_permissions_unix(&temp_path);
if let Err(e) = file.write_all(serialized.as_bytes()) {
let _ = fs::remove_file(&temp_path);
return Err(format!("Failed to write temp PID file: {e}"));
}
if let Err(e) = file.sync_all() {
let _ = fs::remove_file(&temp_path);
return Err(format!("Failed to sync temp PID file: {e}"));
}
}
if let Err(e) = replace_existing(&temp_path, path) {
let _ = fs::remove_file(&temp_path);
return Err(format!(
"Failed to atomically install PID file {}: {}",
path.display(),
e
));
}
let _ = fs::remove_file(&temp_path);
fsync_parent_dir(path);
Ok(())
}
fn unique_temp_path(final_path: &Path) -> PathBuf {
let parent = final_path.parent().unwrap_or_else(|| Path::new("."));
let nanos = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let stem = final_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("snip-sync");
let ext = final_path
.extension()
.and_then(|s| s.to_str())
.unwrap_or("pid");
parent.join(format!(".{stem}-{}-{nanos}.{ext}.tmp", std::process::id()))
}
#[cfg(unix)]
fn replace_existing(from: &Path, to: &Path) -> Result<(), std::io::Error> {
fs::rename(from, to)
}
#[cfg(windows)]
fn replace_existing(from: &Path, to: &Path) -> Result<(), std::io::Error> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::{
MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW,
};
let from_wide: Vec<u16> = from.as_os_str().encode_wide().chain(Some(0)).collect();
let to_wide: Vec<u16> = to.as_os_str().encode_wide().chain(Some(0)).collect();
let result = unsafe {
MoveFileExW(
from_wide.as_ptr(),
to_wide.as_ptr(),
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH,
)
};
if result == 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}
#[cfg(unix)]
fn restrict_permissions_unix(path: &Path) {
use std::os::unix::fs::PermissionsExt;
if let Ok(meta) = fs::metadata(path) {
let mut perms = meta.permissions();
perms.set_mode(0o600);
let _ = fs::set_permissions(path, perms);
}
}
fn fsync_parent_dir(path: &Path) {
if let Some(parent) = path.parent() {
#[cfg(unix)]
{
if let Ok(f) = fs::OpenOptions::new().read(true).open(parent) {
let _ = f.sync_all();
}
}
#[cfg(not(unix))]
{
let _ = parent;
}
}
}
pub fn read_pid_record() -> Option<PidRecord> {
read_pid_at(&paths::pid_path())
}
#[doc(hidden)]
pub fn remove_pid_if_unchanged(expected: &ParsedPidFile) {
remove_pid_if_unchanged_at(&paths::pid_path(), expected);
}
fn remove_pid_if_unchanged_at(path: &Path, expected: &ParsedPidFile) {
let matches = match (expected, parse_pid_file(path)) {
(ParsedPidFile::LegacyPid(expected_pid), ParsedPidFile::LegacyPid(current_pid)) => {
expected_pid == ¤t_pid
}
(ParsedPidFile::Structured(expected_record), ParsedPidFile::Structured(current_record)) => {
expected_record == ¤t_record
}
_ => false,
};
if matches {
let _ = remove_pid_atomic(path);
}
}
fn read_pid_at(path: &Path) -> Option<PidRecord> {
match parse_pid_file(path) {
ParsedPidFile::Structured(rec) => Some(rec),
_ => None,
}
}
pub fn read_pid() -> Option<u32> {
match parse_pid_file(&paths::pid_path()) {
ParsedPidFile::Structured(rec) => Some(rec.pid),
ParsedPidFile::LegacyPid(pid) => Some(pid),
_ => None,
}
}
fn remove_pid_atomic(path: &Path) -> std::io::Result<()> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}
pub fn remove_pid() {
let path = paths::pid_path();
let _ = remove_pid_atomic(&path);
}
#[cfg(unix)]
pub fn is_running(pid: u32) -> bool {
if pid == 0 {
return true;
}
let rc = unsafe { libc::kill(pid as i32, 0) };
rc == 0 || classify_kill_zero_error(std::io::Error::last_os_error().raw_os_error())
}
#[cfg(unix)]
fn classify_kill_zero_error(errno: Option<i32>) -> bool {
!matches!(errno, Some(libc::ESRCH))
}
#[cfg(windows)]
pub fn is_running(pid: u32) -> bool {
if pid == 0 {
return true;
}
unsafe {
use windows_sys::Win32::Foundation::{CloseHandle, STILL_ACTIVE};
use windows_sys::Win32::System::Threading::{
GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
};
let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if handle.is_null() {
return false;
}
let mut exit_code: u32 = 0;
let ok = GetExitCodeProcess(handle, &mut exit_code);
CloseHandle(handle);
if ok == 0 {
return false;
}
exit_code == STILL_ACTIVE as u32
}
}
#[cfg(not(any(unix, windows)))]
pub fn is_running(_pid: u32) -> bool {
false
}
pub fn record_still_matches(record: &PidRecord) -> bool {
if !is_running(record.pid) {
return false;
}
let recorded = match &record.start_token {
Some(t) => t,
None => return true,
};
let observed = match get_process_start_token(record.pid) {
Some(t) => t,
None => return true, };
recorded == &observed
}
#[cfg(unix)]
pub fn validate_process_name(pid: u32) -> bool {
use std::process::Command;
let output = Command::new("ps")
.args(["-p", &pid.to_string(), "-o", "comm="])
.output();
match output {
Ok(o) => {
let name = String::from_utf8_lossy(&o.stdout);
name.trim().contains("snip-sync")
}
Err(_) => false,
}
}
#[cfg(not(unix))]
pub fn validate_process_name(_pid: u32) -> bool {
false
}
pub fn wait_for_exit(pid: u32, timeout: Duration) -> Result<(), String> {
let deadline = Instant::now() + timeout;
loop {
if !is_running(pid) {
return Ok(());
}
if Instant::now() >= deadline {
return Err(format!(
"Process {} did not exit within {}s",
pid,
timeout.as_secs()
));
}
std::thread::sleep(Duration::from_millis(200));
}
}
pub struct LockGuard {
#[allow(dead_code)]
file: fs::File,
path: PathBuf,
remove_on_drop: bool,
}
impl Drop for LockGuard {
fn drop(&mut self) {
if self.remove_on_drop {
let _ = fs::remove_file(&self.path);
}
}
}
pub fn try_lock(path: &Path) -> Result<Option<LockGuard>, String> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.map_err(|e| format!("Failed to create lock directory: {}", e))?;
}
#[cfg(unix)]
{
use std::os::fd::AsRawFd;
let file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(false)
.open(path)
.map_err(|e| format!("Failed to open lock file {}: {}", path.display(), e))?;
let result = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
if result == 0 {
return Ok(Some(LockGuard {
file,
path: path.to_path_buf(),
remove_on_drop: false,
}));
}
if std::io::Error::last_os_error().kind() == std::io::ErrorKind::WouldBlock {
return Ok(None);
}
Err(format!(
"Failed to lock {}: {}",
path.display(),
std::io::Error::last_os_error()
))
}
#[cfg(not(unix))]
{
match fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
{
Ok(file) => Ok(Some(LockGuard {
file,
path: path.to_path_buf(),
remove_on_drop: true,
})),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(None),
Err(e) => Err(format!("Failed to lock {}: {}", path.display(), e)),
}
}
}
pub(crate) fn generate_nonce() -> String {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let seq = COUNTER.fetch_add(1, Ordering::Relaxed);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
format!("{:x}-{:x}-{:x}", std::process::id(), nanos, seq)
}
#[cfg(target_os = "linux")]
pub fn get_process_start_token(pid: u32) -> Option<String> {
let stat_path = format!("/proc/{pid}/stat");
let content = fs::read_to_string(&stat_path).ok()?;
parse_linux_proc_start_token(&content)
}
#[cfg(target_os = "linux")]
fn parse_linux_proc_start_token(stat: &str) -> Option<String> {
let after_comm = stat.rfind(')')?;
let fields: Vec<&str> = stat.get(after_comm + 2..)?.split_whitespace().collect();
fields.get(19).map(|value| (*value).to_owned())
}
#[cfg(target_os = "macos")]
pub fn get_process_start_token(pid: u32) -> Option<String> {
use libc::{PROC_PIDTBSDINFO, c_int, proc_bsdinfo, proc_pidinfo};
let mut info: proc_bsdinfo = unsafe { std::mem::zeroed() };
let ret = unsafe {
proc_pidinfo(
pid as c_int,
PROC_PIDTBSDINFO,
0,
&mut info as *mut _ as *mut _,
std::mem::size_of::<proc_bsdinfo>() as i32,
)
};
if ret <= 0 {
return None;
}
Some(format!(
"{}.{:06}",
info.pbi_start_tvsec, info.pbi_start_tvusec
))
}
#[cfg(windows)]
pub fn get_process_start_token(pid: u32) -> Option<String> {
use windows_sys::Win32::Foundation::{CloseHandle, FILETIME};
use windows_sys::Win32::System::Threading::{
GetProcessTimes, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
};
unsafe {
let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if handle.is_null() {
return None;
}
let mut creation_time: FILETIME = std::mem::zeroed();
let mut exit_time: FILETIME = std::mem::zeroed();
let mut kernel_time: FILETIME = std::mem::zeroed();
let mut user_time: FILETIME = std::mem::zeroed();
let success = GetProcessTimes(
handle,
&mut creation_time,
&mut exit_time,
&mut kernel_time,
&mut user_time,
);
CloseHandle(handle);
if success == 0 {
return None;
}
let creation =
((creation_time.dwHighDateTime as u64) << 32) | (creation_time.dwLowDateTime as u64);
Some(creation.to_string())
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
pub fn get_process_start_token(_pid: u32) -> Option<String> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_os = "linux")]
#[test]
fn linux_start_token_parser_reads_field_22() {
let stat = "42 (name with ) parens) S f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 FIELD21 START22";
assert_eq!(parse_linux_proc_start_token(stat), Some("START22".into()));
assert_ne!(parse_linux_proc_start_token(stat), Some("FIELD21".into()));
assert_eq!(parse_linux_proc_start_token("1 (short) S f3"), None);
}
#[cfg(unix)]
#[test]
fn kill_zero_error_classification_is_conservative() {
assert!(classify_kill_zero_error(Some(libc::EPERM)));
assert!(!classify_kill_zero_error(Some(libc::ESRCH)));
assert!(classify_kill_zero_error(Some(libc::EINVAL)));
}
#[test]
fn test_parse_pid_file_structured_round_trip() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
let record = PidRecord {
schema_version: 1,
pid: 9999,
start_token: Some("token-abc".to_string()),
nonce: Some("nonce-xyz".to_string()),
};
let serialized = toml::to_string_pretty(&record).unwrap();
std::fs::write(&path, serialized).unwrap();
match parse_pid_file(&path) {
ParsedPidFile::Structured(rec) => {
assert_eq!(rec.pid, 9999);
assert_eq!(rec.start_token.as_deref(), Some("token-abc"));
}
other => panic!("expected Structured, got {other:?}"),
}
}
#[test]
fn test_parse_pid_file_legacy_numeric() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
std::fs::write(&path, "1234\n").unwrap();
match parse_pid_file(&path) {
ParsedPidFile::LegacyPid(pid) => assert_eq!(pid, 1234),
other => panic!("expected LegacyPid, got {other:?}"),
}
}
#[test]
fn test_parse_pid_file_empty() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
std::fs::write(&path, " \n").unwrap();
assert_eq!(parse_pid_file(&path), ParsedPidFile::Empty);
}
#[test]
fn test_parse_pid_file_missing_is_empty() {
let path = PathBuf::from("/nonexistent/snip-sync.pid");
assert_eq!(parse_pid_file(&path), ParsedPidFile::Empty);
}
#[test]
fn test_parse_pid_file_malformed() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
std::fs::write(&path, "this is not valid toml nor numeric").unwrap();
match parse_pid_file(&path) {
ParsedPidFile::Malformed(_) => {}
other => panic!("expected Malformed, got {other:?}"),
}
}
#[test]
fn test_remove_legacy_pid_only_when_unchanged() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
std::fs::write(&path, "1234\n").unwrap();
remove_pid_if_unchanged_at(&path, &ParsedPidFile::LegacyPid(9999));
assert_eq!(std::fs::read_to_string(&path).unwrap(), "1234\n");
remove_pid_if_unchanged_at(&path, &ParsedPidFile::LegacyPid(1234));
assert!(!path.exists());
}
#[test]
fn test_write_pid_does_not_remove_an_existing_pid_file() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
fs::write(&path, "live-pid\n").unwrap();
let record = PidRecord {
schema_version: 1,
pid: 1234,
start_token: None,
nonce: Some("n".to_string()),
};
write_pid_atomic(&path, &record).unwrap();
let parsed = parse_pid_file(&path);
assert!(matches!(parsed, ParsedPidFile::Structured(_)));
}
#[test]
fn test_write_pid_atomic_installs_record() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
let record = PidRecord {
schema_version: 1,
pid: 7777,
start_token: Some("t".to_string()),
nonce: Some("n".to_string()),
};
write_pid_atomic(&path, &record).unwrap();
let parsed = parse_pid_file(&path);
match parsed {
ParsedPidFile::Structured(rec) => {
assert_eq!(rec.pid, 7777);
}
other => panic!("expected Structured, got {other:?}"),
}
}
#[test]
fn test_write_pid_atomic_overwrites_existing() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
let record1 = PidRecord {
schema_version: 1,
pid: 1000,
start_token: Some("t1".to_string()),
nonce: Some("n1".to_string()),
};
write_pid_atomic(&path, &record1).unwrap();
let record2 = PidRecord {
schema_version: 1,
pid: 2000,
start_token: Some("t2".to_string()),
nonce: Some("n2".to_string()),
};
write_pid_atomic(&path, &record2).unwrap();
match parse_pid_file(&path) {
ParsedPidFile::Structured(rec) => {
assert_eq!(rec.pid, 2000);
}
other => panic!("expected Structured, got {other:?}"),
}
}
#[test]
fn test_remove_pid_atomic_removes_file() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
fs::write(&path, "test").unwrap();
assert!(remove_pid_atomic(&path).is_ok());
assert!(!path.exists());
}
#[test]
fn test_remove_pid_atomic_missing_is_ok() {
let path = PathBuf::from("/nonexistent/snip-sync.pid");
assert!(remove_pid_atomic(&path).is_ok());
}
#[test]
fn test_pid_guard_does_not_remove_replacement() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("snip-sync.pid");
let record = PidRecord {
schema_version: 1,
pid: std::process::id(),
start_token: Some("t".to_string()),
nonce: Some("n".to_string()),
};
write_pid_atomic(&path, &record).unwrap();
let guard = PidGuard {
path: path.clone(),
record,
};
let replacement = PidRecord {
schema_version: 1,
pid: 1234,
start_token: Some("replacement-token".to_string()),
nonce: Some("replacement-nonce".to_string()),
};
write_pid_atomic(&path, &replacement).unwrap();
drop(guard);
match parse_pid_file(&path) {
ParsedPidFile::Structured(rec) => assert_eq!(rec.pid, 1234),
other => panic!("expected Structured replacement, got {other:?}"),
}
}
#[test]
fn test_record_still_matches_dead_pid() {
let record = PidRecord {
schema_version: 1,
pid: 99999999,
start_token: Some("anything".to_string()),
nonce: Some("nonce".to_string()),
};
assert!(!record_still_matches(&record));
}
#[test]
fn test_record_still_matches_no_start_token() {
let record = PidRecord {
schema_version: 1,
pid: std::process::id(),
start_token: None,
nonce: None,
};
assert!(record_still_matches(&record));
}
#[test]
#[cfg(unix)]
fn test_record_still_matches_pid_reuse_detected() {
let bogus = "bogus-token-99999";
let record = PidRecord {
schema_version: 1,
pid: std::process::id(),
start_token: Some(bogus.to_string()),
nonce: Some("n".to_string()),
};
assert!(!record_still_matches(&record));
}
#[test]
fn test_generate_nonce_unique() {
let a = generate_nonce();
let b = generate_nonce();
assert_ne!(a, b);
}
#[test]
#[cfg(unix)]
fn test_reconcile_pid_under_lock_dead_legacy_is_ok() {
let temp = tempfile::tempdir().unwrap();
fs::write(temp.path().join("snip-sync.pid"), "99999999").unwrap();
assert!(reconcile_pid_under_lock(temp.path()).is_ok());
}
#[test]
#[cfg(unix)]
fn test_reconcile_pid_under_lock_empty_is_ok() {
let temp = tempfile::tempdir().unwrap();
fs::write(temp.path().join("snip-sync.pid"), "").unwrap();
assert!(reconcile_pid_under_lock(temp.path()).is_ok());
}
#[test]
fn test_reconcile_pid_under_lock_missing_is_ok() {
let temp = tempfile::tempdir().unwrap();
assert!(reconcile_pid_under_lock(temp.path()).is_ok());
}
#[test]
fn test_reconcile_pid_under_lock_malformed_is_ok() {
let temp = tempfile::tempdir().unwrap();
fs::write(
temp.path().join("snip-sync.pid"),
"this is not valid toml nor numeric",
)
.unwrap();
assert!(reconcile_pid_under_lock(temp.path()).is_ok());
}
}