Fix worktree scan generation cache leak (#7675)
This commit is contained in:
@@ -23,6 +23,8 @@ import { clearGitCapabilityStateForTests } from './git-capability-state'
|
||||
import {
|
||||
addSparseWorktree,
|
||||
addWorktree,
|
||||
_getWorktreeScanCacheSizesForTests,
|
||||
_resetWorktreeScanCacheForTests,
|
||||
listWorktreeGraph,
|
||||
listWorktrees,
|
||||
moveWorktree,
|
||||
@@ -36,7 +38,13 @@ beforeEach(() => {
|
||||
})
|
||||
|
||||
describe('listWorktrees in-flight sharing', () => {
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
_resetWorktreeScanCacheForTests()
|
||||
// Why: capability discovery serializes same-host callers; warming it lets
|
||||
// these tests isolate the scan-generation overlap they are exercising.
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '' })
|
||||
await listWorktrees('/capability-warmup')
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
})
|
||||
|
||||
@@ -87,6 +95,126 @@ describe('listWorktrees in-flight sharing', () => {
|
||||
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('does not retain scan generations after mutations without active scans', async () => {
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '' })
|
||||
|
||||
await Promise.all(
|
||||
Array.from({ length: 128 }, (_, index) =>
|
||||
moveWorktree(`/repo-${index}`, `/repo-${index}-old`, `/repo-${index}-new`)
|
||||
)
|
||||
)
|
||||
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 0, generations: 0 })
|
||||
})
|
||||
|
||||
it('cleans mutation generations after stale scans settle and supports repo reuse', async () => {
|
||||
const scanResolvers: ((stdout: string) => void)[] = []
|
||||
let listCalls = 0
|
||||
gitExecFileAsyncMock.mockImplementation((args: string[]) => {
|
||||
if (args[0] === 'worktree' && args[1] === 'list') {
|
||||
listCalls += 1
|
||||
return new Promise((resolve) => {
|
||||
scanResolvers.push((stdout) => resolve({ stdout }))
|
||||
})
|
||||
}
|
||||
return Promise.resolve({ stdout: '' })
|
||||
})
|
||||
|
||||
const staleScan = listWorktrees('/repo')
|
||||
expect(scanResolvers).toHaveLength(1)
|
||||
|
||||
await moveWorktree('/repo', '/repo-old', '/repo-new')
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 1, generations: 1 })
|
||||
|
||||
const freshScan = listWorktrees('/repo')
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 2, generations: 1 })
|
||||
|
||||
scanResolvers[1]?.('worktree /repo-new\nHEAD fresh\nbranch refs/heads/main\n')
|
||||
expect((await freshScan)[0]?.path).toBe('/repo-new')
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 1, generations: 1 })
|
||||
|
||||
scanResolvers[0]?.('worktree /repo\nHEAD stale\nbranch refs/heads/main\n')
|
||||
expect((await staleScan)[0]?.path).toBe('/repo')
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 0, generations: 0 })
|
||||
|
||||
const firstReuse = listWorktrees('/repo')
|
||||
const secondReuse = listWorktrees('/repo')
|
||||
expect(listCalls).toBe(3)
|
||||
|
||||
scanResolvers[2]?.('worktree /repo-reused\nHEAD reused\nbranch refs/heads/main\n')
|
||||
const [firstResult, secondResult] = await Promise.all([firstReuse, secondReuse])
|
||||
expect(firstResult).toEqual(secondResult)
|
||||
expect(firstResult[0]?.path).toBe('/repo-reused')
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 0, generations: 0 })
|
||||
})
|
||||
|
||||
it('retires every overlapping scan generation across repeated mutations', async () => {
|
||||
const scanResolvers: (() => void)[] = []
|
||||
let listCalls = 0
|
||||
gitExecFileAsyncMock.mockImplementation((args: string[]) => {
|
||||
if (args[0] === 'worktree' && args[1] === 'list') {
|
||||
listCalls += 1
|
||||
return new Promise((resolve) => {
|
||||
scanResolvers.push(() => resolve({ stdout: '' }))
|
||||
})
|
||||
}
|
||||
return Promise.resolve({ stdout: '' })
|
||||
})
|
||||
|
||||
const oldestScan = listWorktrees('/repo')
|
||||
await moveWorktree('/repo', '/old-0', '/new-0')
|
||||
const middleScan = listWorktrees('/repo')
|
||||
await moveWorktree('/repo', '/old-1', '/new-1')
|
||||
const newestScan = listWorktrees('/repo')
|
||||
|
||||
expect(listCalls).toBe(3)
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 3, generations: 1 })
|
||||
|
||||
scanResolvers[0]?.()
|
||||
scanResolvers[2]?.()
|
||||
scanResolvers[1]?.()
|
||||
await Promise.all([oldestScan, middleScan, newestScan])
|
||||
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 0, generations: 0 })
|
||||
})
|
||||
|
||||
it('keeps WSL scans distinct while retiring both after a mutation', async () => {
|
||||
const scanResolvers: (() => void)[] = []
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: '' })
|
||||
await Promise.all([
|
||||
listWorktrees('/capability-warmup', { wslDistro: 'Ubuntu' }),
|
||||
listWorktrees('/capability-warmup', { wslDistro: 'Debian' })
|
||||
])
|
||||
gitExecFileAsyncMock.mockReset()
|
||||
gitExecFileAsyncMock.mockImplementation((args: string[]) => {
|
||||
if (args[0] === 'worktree' && args[1] === 'list') {
|
||||
return new Promise((resolve) => {
|
||||
scanResolvers.push(() => resolve({ stdout: '' }))
|
||||
})
|
||||
}
|
||||
return Promise.resolve({ stdout: '' })
|
||||
})
|
||||
|
||||
const staleUbuntu = listWorktrees('/repo', { wslDistro: 'Ubuntu' })
|
||||
const staleDebian = listWorktrees('/repo', { wslDistro: 'Debian' })
|
||||
expect(scanResolvers).toHaveLength(2)
|
||||
|
||||
await removeWorktree('/repo', '/worktree', false, {
|
||||
knownRemovedWorktree: { branch: '', head: '' },
|
||||
wslDistro: 'Ubuntu'
|
||||
})
|
||||
const freshUbuntu = listWorktrees('/repo', { wslDistro: 'Ubuntu' })
|
||||
const freshDebian = listWorktrees('/repo', { wslDistro: 'Debian' })
|
||||
expect(scanResolvers).toHaveLength(4)
|
||||
|
||||
for (const resolve of scanResolvers) {
|
||||
resolve()
|
||||
}
|
||||
await Promise.all([staleUbuntu, staleDebian, freshUbuntu, freshDebian])
|
||||
|
||||
expect(_getWorktreeScanCacheSizesForTests()).toEqual({ inFlight: 0, generations: 0 })
|
||||
})
|
||||
|
||||
it('does not share scans for callers with an AbortSignal', async () => {
|
||||
const scanOutput = 'worktree /repo\nHEAD abc123\nbranch refs/heads/main\n'
|
||||
gitExecFileAsyncMock.mockResolvedValue({ stdout: scanOutput })
|
||||
|
||||
@@ -650,10 +650,48 @@ const inFlightWorktreeScans = new Map<string, Promise<GitWorktreeInfo[]>>()
|
||||
// settle for their original callers).
|
||||
const worktreeScanGenerations = new Map<string, number>()
|
||||
|
||||
function hasInFlightWorktreeScanForRepo(repoPath: string): boolean {
|
||||
const keyPrefix = `${repoPath}\0`
|
||||
for (const key of inFlightWorktreeScans.keys()) {
|
||||
if (key.startsWith(keyPrefix)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function bumpWorktreeScanGeneration(repoPath: string): void {
|
||||
// Why: generations only prevent joining a pre-mutation scan. Without an
|
||||
// active scan, retaining the repo path just leaks completed mutation keys.
|
||||
if (!hasInFlightWorktreeScanForRepo(repoPath)) {
|
||||
return
|
||||
}
|
||||
worktreeScanGenerations.set(repoPath, (worktreeScanGenerations.get(repoPath) ?? 0) + 1)
|
||||
}
|
||||
|
||||
function pruneWorktreeScanGeneration(repoPath: string): void {
|
||||
// Why: ordinary scan settlement should stay O(1); only repos invalidated
|
||||
// during an active scan need the cross-generation in-flight check.
|
||||
if (!worktreeScanGenerations.has(repoPath)) {
|
||||
return
|
||||
}
|
||||
if (!hasInFlightWorktreeScanForRepo(repoPath)) {
|
||||
worktreeScanGenerations.delete(repoPath)
|
||||
}
|
||||
}
|
||||
|
||||
export function _getWorktreeScanCacheSizesForTests(): { inFlight: number; generations: number } {
|
||||
return {
|
||||
inFlight: inFlightWorktreeScans.size,
|
||||
generations: worktreeScanGenerations.size
|
||||
}
|
||||
}
|
||||
|
||||
export function _resetWorktreeScanCacheForTests(): void {
|
||||
inFlightWorktreeScans.clear()
|
||||
worktreeScanGenerations.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* List all worktrees for a git repo at the given path. Concurrent calls for
|
||||
* the same repo share one scan (unless the caller passes an AbortSignal,
|
||||
@@ -676,6 +714,7 @@ export function listWorktrees(
|
||||
if (inFlightWorktreeScans.get(key) === scan) {
|
||||
inFlightWorktreeScans.delete(key)
|
||||
}
|
||||
pruneWorktreeScanGeneration(repoPath)
|
||||
})
|
||||
inFlightWorktreeScans.set(key, scan)
|
||||
return scan
|
||||
|
||||
Reference in New Issue
Block a user