From d0293d6602f8f4e53bce208564210575bbb1782d Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 19 Nov 2026 09:51:39 -0600 Subject: [PATCH] fix(bus): review round: proxy init creates the lock before it installs the client, so a lock that refuses leaves no client; a default-instance bus test proves the catalogue refreshes at once on pickup and on drop (#1873, lane proxy-pickup) --- packages/bus/src/runtime.mjs | 7 +++--- packages/bus/tests/proxy-pickup.test.mjs | 30 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/bus/src/runtime.mjs b/packages/bus/src/runtime.mjs index 2d38eb7f9..e6e10a8dd 100644 --- a/packages/bus/src/runtime.mjs +++ b/packages/bus/src/runtime.mjs @@ -1403,12 +1403,13 @@ export async function startBroker({ }, proxyInit: organization ? async args => { const settings = await initProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args }); - modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt }); - proxyPort = settings.port; + const client = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt }); // #1873: an org bus that initializes the shared proxy also holds its write lock from now on. - if (org && !proxyLock) proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }); + // proxy-pickup: the lock comes first, so a lock that refuses leaves no client behind. + const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock; // proxy-pickup: a client this bus did not boot with is checked again like a picked-up one. if (!proxy) { lateProxy = true; proxyCause = null; } + modelProxyClient = client; proxyPort = settings.port; proxyLock = lock; configureProxyPage(); modelCatalogues?.invalidate?.(); return settings; } : null, diff --git a/packages/bus/tests/proxy-pickup.test.mjs b/packages/bus/tests/proxy-pickup.test.mjs index 1feaae9d2..e7ebabd04 100644 --- a/packages/bus/tests/proxy-pickup.test.mjs +++ b/packages/bus/tests/proxy-pickup.test.mjs @@ -293,6 +293,36 @@ test('the bus that ran init also drops its client when the key stops loading', a assert.equal(a.bus.modelProxy, null); }); +test('an init whose lock refuses leaves the init bus holding no client: no ready catalogue without the lock', async t => { + const f = instance(t); + // The real init, then the proxy state dir is left readable by the group, which the lock refuses. + const initProxy = async args => { const settings = await initializeProxy({ ...args, pin: f.pin }); + fs.mkdirSync(join(f.dataRoot, 'proxy'), { recursive: true }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o750); return settings; }; + const a = await orgBus(t, f, 'alpha', { binding: { initProxy } }); + await assert.rejects(a.ask('proxy.init', { key: f.key })); + await fakeProxy(t, f.key, JSON.parse(fs.readFileSync(join(f.configDir, 'proxy.json'), 'utf8')).port); + assert.equal(a.bus.modelProxy, null); + const view = await a.ask('routes'); + assert.equal(view.catalogue.status, 'unavailable'); + assert.match(view.catalogue.notices[0], /proxy-lock-invalid/); + assert.equal(a.bus.modelProxy, null); +}); + +test('a default-instance bus (no lock generation) refreshes its catalogue at once on pickup and on drop', async t => { + const f = instance(t); + const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null }); + // An unavailable catalogue read before init must not be served after the pickup. + assert.equal((await d.ask('routes')).catalogue.status, 'unavailable'); + await initThroughA(t, f, a); + assert.equal((await d.ask('routes')).catalogue.status, 'ready'); + // A ready catalogue read through the picked-up client must not be served after the drop. + fs.rmSync(join(f.configDir, 'proxy.json')); + const gone = await d.ask('routes'); + assert.equal(gone.catalogue.status, 'unavailable'); + assert.equal(gone.catalogue.reason, 'proxy-not-configured'); + assert.equal(d.bus.modelProxy, null); +}); + test('a picked-up bus clears a proxy fence through the same client and lock', async t => { const f = instance(t); const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');