422a82958764

clear a stale folder lock only when its writer is gone

time
2026-11-17 01:56 UTC · 1 week ago
machine
Junes-MacBook-Pro.local
agent
deepseek/deepseek-flash
session
review findings
parent
4ab979d1a1e4
4 files src/remote/folder.v+19 −3src/remote/folder_test.v+20 −0vendor/shim/koh_process.c+14 −0vendor/shim/koh_process.h+15 −0
src/remote/folder.v+19 −3
@@ -16,9 +16,12 @@1616 1717 #flag -I@VMODROOT/vendor/shim1818 #flag @VMODROOT/vendor/shim/koh_create.c19+#flag @VMODROOT/vendor/shim/koh_process.c1920 #include "koh_create.h"21+#include "koh_process.h"2022 2123 fn C.koh_create_exclusive(path &char, data voidptr, len usize) int24+fn C.koh_pid_alive(pid int) int2225 2326 pub struct Folder {2427 pub:@@ -187,12 +190,25 @@187190 188191 // clear_stale_lock removes a lock left behind by a writer that died mid-swap.189192 // A lock a minute old is not a swap in progress; it is debris, and leaving it190-// would block this remote for ever.193+// would block this remote for ever. The pid inside it is checked first: deleting194+// a lock whose writer is still alive would let a second writer in.191195 fn clear_stale_lock(path string) {192196 	info := os.stat(path) or { return }193-	if time.now().unix() - info.mtime > 60 {194-		os.rm(path) or {}197+	if time.now().unix() - info.mtime <= 60 {198+		return195199 	}200+	body := os.read_file(path) or { return }201+	pid := body.trim_space().int()202+	if pid > 0 && C.koh_pid_alive(pid) != 0 {203+		return204+	}205+	// The file may have been replaced between the two reads; only the exact206+	// stale lock that was examined is removed.207+	again := os.stat(path) or { return }208+	if again.mtime != info.mtime {209+		return210+	}211+	os.rm(path) or {}196212 }197213 198214 pub fn (mut f Folder) delete(key string) ! {

view file

src/remote/folder_test.v+20 −0
@@ -1,6 +1,7 @@11 module remote22 33 import os4+import time45 56 fn scratch(name string) !string {67 	dir := os.join_path(os.temp_dir(), 'koh3_remote_${os.getpid()}_${name}')@@ -124,4 +125,23 @@124125 	release_lock(guard)125126 	assert take_lock(guard)!126127 	release_lock(guard)128+}129+130+fn test_a_stale_lock_is_cleared_only_when_its_writer_is_gone() {131+	base := scratch('stale_lock')!132+	defer {133+		os.rmdir_all(base) or {}134+	}135+	guard := os.join_path(base, 'key.lock')136+	old := time.now().unix() - 120137+	// Our own pid is alive, so an old lock is still a writer's.138+	os.write_file(guard, '${os.getpid()}')!139+	os.utime(guard, old, old)!140+	clear_stale_lock(guard)141+	assert os.exists(guard)142+	// A pid nothing holds is debris, and clears.143+	os.write_file(guard, '2000000')!144+	os.utime(guard, old, old)!145+	clear_stale_lock(guard)146+	assert !os.exists(guard)127147 }

view file

vendor/shim/koh_process.c+14 −0
@@ -0,0 +1,14 @@1+#include "koh_process.h"2+3+#include <errno.h>4+#include <signal.h>5+6+int koh_pid_alive(int pid) {7+	if (pid <= 0) {8+		return 0;9+	}10+	if (kill(pid, 0) == 0) {11+		return 1;12+	}13+	return errno == EPERM ? 1 : 0;14+}

view file

vendor/shim/koh_process.h+15 −0
@@ -0,0 +1,15 @@1+/* Process liveness for a stale lock.2+ *3+ * The folder backend clears a lock only when the writer that took it is gone.4+ * The machine's own pid namespace is enough for that: a lock holds the pid that5+ * wrote it, and `kill(pid, 0)` answers whether that process still exists.6+ * EPERM means it exists but belongs to another user, which still counts.7+ */8+9+#ifndef KOH_PROCESS_H10+#define KOH_PROCESS_H11+12+/* 1 when the process exists, 0 when it does not (or the pid is nonsense). */13+int koh_pid_alive(int pid);14+15+#endif

view file