diff --git a/packages/business/src/org-registry.mjs b/packages/business/src/org-registry.mjs index a9ea3671b..f396dc25b 100644 --- a/packages/business/src/org-registry.mjs +++ b/packages/business/src/org-registry.mjs @@ -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); diff --git a/packages/business/src/organization.mjs b/packages/business/src/organization.mjs index 4ea1f9951..770dde6cc 100644 --- a/packages/business/src/organization.mjs +++ b/packages/business/src/organization.mjs @@ -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 diff --git a/packages/business/tests/org-service.test.mjs b/packages/business/tests/org-service.test.mjs index 6d9bde0b6..c2743647b 100644 --- a/packages/business/tests/org-service.test.mjs +++ b/packages/business/tests/org-service.test.mjs @@ -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; +});