@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 +1 -1
- package/src/pairing-storage.ts +22 -20
package/package.json
CHANGED
package/src/pairing-storage.ts
CHANGED
|
@@ -111,32 +111,32 @@ async function syncDirectory(directory: string): Promise<void> {
|
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
/**
|
|
115
|
-
*
|
|
116
|
-
* mutation requires a finished lease and re-validates the lease inode first (assertLock),
|
|
117
|
-
* the app's single-instance lock rules out a second live daemon on one data directory — so
|
|
118
|
-
*
|
|
119
|
-
function
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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 (
|
|
158
|
-
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
}
|