Honor broker document holds across service writes

This commit is contained in:
jason.woltje committed 2026-11-07 15:00:09 -06:00
1 parent cb79be0440
commit a55310559b
3 files changed
+28 -6

No files matched your search

+9 -2
View File
@@ -59,6 +59,8 @@ export function withRegistryWriter(configDir, fn) {
const lock = join(configDir, 'org-runtime.lock'); let lockFd;
try { lockFd = openSync(lock, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
catch (e) { if (e.code === 'EEXIST') fail('org-locked', 'registry lock is held', lock); throw e; }
const release = () => { closeSync(lockFd); unlinkSync(lock); };
let pending = false;
try {
const dir = lstatSync(configDir);
if (!dir.isDirectory() || dir.isSymbolicLink() || dir.uid !== process.getuid() || (dir.mode & 0o077)) fail('org-mismatch', 'unsafe private directory', configDir);
@@ -70,8 +72,13 @@ export function withRegistryWriter(configDir, fn) {
try { writeFileSync(temp, JSON.stringify(registry, null, 2) + '\n', { mode: 0o600, flag: 'wx' }); renameSync(temp, file); }
finally { try { unlinkSync(temp); } catch (e) { if (e.code !== 'ENOENT') throw e; } }
};
return fn(registry, publish);
} finally { closeSync(lockFd); unlinkSync(lock); }
const result = fn(registry, publish);
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
pending = true;
return Promise.resolve(result).finally(release);
}
return result;
} finally { if (!pending) release(); }
}
export async function reserveOrg(ctx, id) {
const root = orgRoot(ctx.dataRoot, id);
+4 -4
View File
@@ -372,7 +372,7 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
// duration, and an awaited call would leave a half-checked world to whoever finds the lock gone.
function hold(id, fn) {
requireId(id, "organization id");
return withLock(id, () => {
return withRegistryWriter(dir, () => withLock(id, () => {
recover(id);
const { document, revision } = read(id);
const out = fn({ document, revision });
@@ -381,17 +381,17 @@ export function organizationWriter({ dir, rolesDir, barrier = () => {} }) {
fail("organization-hold-sync", "a held check must be synchronous");
}
return out;
});
}));
}
// Like hold, but the lock stays on disk until an asynchronous effect settles, including on failure.
// The effect must not call an organization writer: it would find this same lock and refuse busy.
function holdAsync(id, fn) {
requireId(id, "organization id");
return withLock(id, () => {
return withRegistryWriter(dir, () => withLock(id, () => {
recover(id);
return fn(read(id));
});
}));
}
// The committed addition of this request and plan, if there is one. A prepared receipt whose
@@ -70,3 +70,18 @@ test('setup never overwrites a broker write committed while git initializes', as
assert.deepEqual((await showOrg(c, 'alpha')).doc.plugins.channel, ['cli']);
} finally { process.env.PATH = oldPath; watcher?.close(); await writeFile(release, 'release'); await pending.catch(() => {}); }
});
test('service updates wait for the broker writer asynchronous document hold', async () => {
const c = context(); await createOrg(c, 'alpha', await templateRender('alpha'));
const current = await showOrg(c, 'alpha');
const writer = organizationWriter({ dir: c.configDir, rolesDir: REPO_ROLES });
let release; const gate = new Promise(resolve => { release = resolve; });
const held = writer.holdAsync('alpha', () => gate);
let settled = false;
const update = updateOrg(c, 'alpha', current.doc, { ifRevision: current.revision }).finally(() => { settled = true; });
try {
await new Promise(resolve => setTimeout(resolve, 100));
assert.equal(settled, false, 'service must not publish during a broker document hold');
} finally { release(); await held; }
await update;
});