Address PR review feedback (#3189)

- Serialize initLbugWithDb on withPoolLock so it cannot attach to a Database
  closeOne is still checkpointing.
- Delete pin leases again after the awaited close so a pin acquired during
  teardown cannot survive onto the next init.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Gergo Magyar 2026-09-12 13:20:16 +00:00
parent 1fbe8f661d
commit d1c9a658da
2 changed files with 60 additions and 1 deletions

View file

@ -444,6 +444,11 @@ async function closeOne(repoId: string): Promise<void> {
}
}
// Close yields on native db.close() above. A pinRepo during that await
// would otherwise survive teardown and apply to the next init, contradicting
// the documented lease contract (pins do not outlive closeOne).
pinnedRepos.delete(repoId);
traceRss('close', repoId);
}
@ -937,6 +942,14 @@ export async function initLbugWithDb(
repoId: string,
existingDb: lbug.Database,
dbPath: string,
): Promise<void> {
return withPoolLock(() => initLbugWithDbInner(repoId, existingDb, dbPath));
}
async function initLbugWithDbInner(
repoId: string,
existingDb: lbug.Database,
dbPath: string,
): Promise<void> {
const existing = pool.get(repoId);
if (existing) {

View file

@ -73,8 +73,9 @@ vi.mock('../../src/core/lbug/sidecar-recovery.js', () => ({
statIfExists: vi.fn().mockResolvedValue(null),
}));
const { initLbug, closeLbug, isLbugReady, pinRepo, unpinRepo } =
const { initLbug, initLbugWithDb, closeLbug, isLbugReady, pinRepo, unpinRepo } =
await import('../../src/core/lbug/pool-adapter.js');
const { createLbugDatabase } = await import('../../src/core/lbug/lbug-config.js');
const { initWikiDb, closeWikiDb, pinWikiDb } = await import('../../src/core/wiki/graph-queries.js');
describe('pool-adapter repo pinning (issue #2189)', () => {
@ -251,4 +252,49 @@ describe('pool-adapter repo pinning (issue #2189)', () => {
await init('d-extra2');
expect(isLbugReady('d-shared')).toBe(false);
});
it('a pin acquired while closeOne awaits db.close() does not survive teardown', async () => {
vi.mocked(createLbugDatabase).mockImplementationOnce(() => ({
init: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockImplementation(async () => {
await new Promise((resolve) => setTimeout(resolve, 20));
}),
}));
await init('late-pin');
const closing = closeLbug('late-pin');
await Promise.resolve();
pinRepo('late-pin');
await closing;
expect(isLbugReady('late-pin')).toBe(false);
await init('late-pin');
for (let i = 1; i <= 5; i++) await init(`late-fresh-${i}`);
expect(isLbugReady('late-pin')).toBe(false);
});
it('initLbugWithDb waits for an in-flight closeOne before registering', async () => {
vi.mocked(createLbugDatabase).mockImplementationOnce(() => ({
init: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockImplementation(async () => {
await new Promise((resolve) => setTimeout(resolve, 20));
}),
}));
await init('injected-overlap');
const closing = closeLbug('injected-overlap');
await Promise.resolve();
const injected = {
init: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
};
const injecting = initLbugWithDb(
'injected-overlap',
injected as never,
dbPathFor('injected-overlap'),
);
await closing;
await injecting;
expect(isLbugReady('injected-overlap')).toBe(true);
});
});