@bom-pi/core 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bom-pi/core",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Core pairing machinery for Bom Pi: ceremonies, transports, durable stores",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -111,32 +111,32 @@ async function syncDirectory(directory: string): Promise<void> {
111
111
  }
112
112
  }
113
113
 
114
- /** A finished lease names its holder: `pid:uuid`. A lease that is malformed, legacy
115
- * (pre-pid build) or names a provably dead holder never guarded a completed mutation — every
116
- * mutation requires a finished lease and re-validates the lease inode first (assertLock), and
117
- * the app's single-instance lock rules out a second live daemon on one data directory — so
118
- * such leases are crash leftovers and are reclaimed instead of blocking the next boot. */
119
- function leaseHeldByLiveWriter(content: string): boolean {
114
+ /** The pid of a live lease holder, or null for a lease that is malformed, legacy (pre-pid
115
+ * build) or names a provably dead holder. Such leases never guarded a completed mutation —
116
+ * every mutation requires a finished lease and re-validates the lease inode first (assertLock),
117
+ * and the app's single-instance lock rules out a second live daemon on one data directory — so
118
+ * they are crash leftovers and get reclaimed instead of blocking the next boot. */
119
+ function liveLeaseHolderPid(content: string): number | null {
120
120
  const trimmed = content.trim();
121
121
 
122
122
  if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(trimmed)) {
123
- return false; // legacy bare-uuid build: the lease cannot name a live holder
123
+ return null; // legacy bare-uuid build: the lease cannot name a live holder
124
124
  }
125
125
 
126
126
  const separator = trimmed.indexOf(":");
127
127
  const pid = Number.parseInt(separator > 0 ? trimmed.slice(0, separator) : "", 10);
128
128
 
129
129
  if (!Number.isSafeInteger(pid) || pid <= 0) {
130
- return false; // the writer died before finishing its own identity
130
+ return null; // the writer died before finishing its own identity
131
131
  }
132
132
 
133
133
  try {
134
134
  process.kill(pid, 0);
135
135
 
136
- return true;
136
+ return pid;
137
137
  } catch (error) {
138
138
  // EPERM names a live process under another user; anything else is dead or invalid.
139
- return (error as NodeJS.ErrnoException).code !== "EPERM";
139
+ return (error as NodeJS.ErrnoException).code === "EPERM" ? pid : null;
140
140
  }
141
141
  }
142
142
 
@@ -153,18 +153,20 @@ async function acquireLock(directory: string, lockFile: string): Promise<LockLea
153
153
 
154
154
  // An EEXIST lease nobody live holds is a crash leftover: reclaim it once, fail-closed.
155
155
  const existing = await readFile(path, "utf8").catch(() => "");
156
+ const holder = liveLeaseHolderPid(existing);
156
157
 
157
- if (!leaseHeldByLiveWriter(existing)) {
158
- await rm(path, { force: true });
158
+ if (holder !== null) {
159
+ fail(
160
+ `writer lock exists; a live writer (pid ${holder}) holds this store ` +
161
+ "(stop it, or verify and recover explicitly)",
162
+ );
163
+ }
159
164
 
160
- try {
161
- handle = await open(path, "wx", 0o600);
162
- } catch (race) {
163
- fail(
164
- "writer lock exists; a live writer holds this store (explicit recovery required)",
165
- );
166
- }
167
- } else {
165
+ await rm(path, { force: true });
166
+
167
+ try {
168
+ handle = await open(path, "wx", 0o600);
169
+ } catch (race) {
168
170
  fail("writer lock exists; a live writer holds this store (explicit recovery required)");
169
171
  }
170
172
  }