Fix tracked upstream generation cache leak (#7674)

This commit is contained in:
Neil
2026-07-11 13:43:17 -07:00
committed by GitHub
parent 884781a552
commit b5cdab3a26
2 changed files with 159 additions and 5 deletions
+114
View File
@@ -118,6 +118,7 @@ import {
updatePRState,
updatePRTitle,
_getMergeQueueCacheSizeForTests,
_getTrackedUpstreamBranchCacheSizesForTests,
_resetOwnerRepoCache,
_resetMergeQueueCacheForTests,
__resetTrackedUpstreamBranchCacheForTests
@@ -1786,6 +1787,119 @@ describe('getPRForBranch', () => {
expect(trackedUpstreamCalls).toHaveLength(1)
})
it('releases tracked-upstream probe generations across runtime identities', async () => {
const sshGitProvider = {
exec: vi.fn().mockResolvedValue({ stdout: 'feature\0\n', stderr: '' })
}
getSshGitProviderMock.mockReturnValue(sshGitProvider)
getOwnerRepoMock.mockResolvedValue({ owner: 'acme', repo: 'widgets' })
ghExecFileAsyncMock.mockResolvedValue({ stdout: JSON.stringify([]) })
gitExecFileAsyncMock.mockResolvedValue({ stdout: 'feature\0\n', stderr: '' })
await getPRForBranch('/repo-root', 'feature')
await getPRForBranch('/repo-root', 'feature', null, null, null, {
localGitExecOptions: { wslDistro: 'Ubuntu' }
})
await getPRForBranch('/repo-root', 'feature', null, 'ssh-1')
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 3,
inFlight: 0,
generations: 0
})
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(2)
expect(sshGitProvider.exec).toHaveBeenCalledTimes(1)
})
it('bounds unique tracked-upstream snapshots and sweeps expired identities', async () => {
vi.useFakeTimers()
try {
getOwnerRepoMock.mockResolvedValue({ owner: 'acme', repo: 'widgets' })
ghExecFileAsyncMock.mockResolvedValue({ stdout: JSON.stringify([]) })
gitExecFileAsyncMock.mockResolvedValue({ stdout: 'feature\0\n', stderr: '' })
for (let index = 0; index < 513; index += 1) {
await getPRForBranch(`/repo-root-${index}`, 'feature')
}
await getPRForBranch('/repo-root-512', 'feature')
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 512,
inFlight: 0,
generations: 0
})
expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(513)
await vi.advanceTimersByTimeAsync(30_001)
await getPRForBranch('/repo-root-fresh', 'feature')
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 1,
inFlight: 0,
generations: 0
})
} finally {
vi.useRealTimers()
}
})
it('keeps stale probe cleanup from releasing a replacement generation', async () => {
let resolveOldProbe: (value: { stdout: string; stderr: string }) => void
let resolveCurrentProbe: (value: { stdout: string; stderr: string }) => void
getOwnerRepoMock.mockResolvedValue({ owner: 'acme', repo: 'widgets' })
ghExecFileAsyncMock.mockResolvedValue({ stdout: JSON.stringify([]) })
gitExecFileAsyncMock
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveOldProbe = resolve
})
)
.mockResolvedValueOnce({ stdout: 'replacement\0\n', stderr: '' })
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveCurrentProbe = resolve
})
)
const oldLookup = getPRForBranch('/repo-root', 'old')
await vi.waitFor(() => expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(1))
__resetTrackedUpstreamBranchCacheForTests()
await getPRForBranch('/repo-root', 'replacement')
const currentLookup = getPRForBranch('/repo-root', 'current')
await vi.waitFor(() => expect(gitExecFileAsyncMock).toHaveBeenCalledTimes(3))
resolveOldProbe!({ stdout: 'old\0\n', stderr: '' })
await oldLookup
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 0,
inFlight: 1,
generations: 1
})
resolveCurrentProbe!({ stdout: 'current\0\n', stderr: '' })
await currentLookup
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 1,
inFlight: 0,
generations: 0
})
})
it('releases tracked-upstream probe state when setup rejects', async () => {
getOwnerRepoMock.mockResolvedValue({ owner: 'acme', repo: 'widgets' })
ghExecFileAsyncMock.mockResolvedValue({ stdout: JSON.stringify([]) })
readLocalGitConfigSignatureMock.mockRejectedValue(new Error('git config unavailable'))
await expect(getPRForBranch('/repo-root', 'feature')).resolves.toBeNull()
expect(_getTrackedUpstreamBranchCacheSizesForTests()).toEqual({
snapshots: 0,
inFlight: 0,
generations: 0
})
})
it('does not fan out tracked-upstream probes after a transient for-each-ref failure', async () => {
resolvePRRepositoryCandidatesMock.mockResolvedValue({
candidates: [{ owner: 'acme', repo: 'widgets' }],
+45 -5
View File
@@ -2392,6 +2392,7 @@ type TrackedUpstreamBranch = {
}
const TRACKED_UPSTREAM_SNAPSHOT_CACHE_TTL_MS = 30_000
const TRACKED_UPSTREAM_SNAPSHOT_CACHE_MAX_ENTRIES = 512
type TrackedUpstreamSnapshotCacheEntry = {
expiresAt: number
@@ -2411,12 +2412,49 @@ const trackedUpstreamSnapshotInFlight = new Map<
string,
Promise<TrackedUpstreamSnapshotProbeResult>
>()
const trackedUpstreamSnapshotGenerations = new Map<string, number>()
const trackedUpstreamSnapshotGenerations = new Map<string, symbol>()
function beginTrackedUpstreamSnapshotProbe(cacheKey: string): number {
const nextGeneration = (trackedUpstreamSnapshotGenerations.get(cacheKey) ?? 0) + 1
trackedUpstreamSnapshotGenerations.set(cacheKey, nextGeneration)
return nextGeneration
function beginTrackedUpstreamSnapshotProbe(cacheKey: string): symbol {
const generation = Symbol()
trackedUpstreamSnapshotGenerations.set(cacheKey, generation)
return generation
}
function finishTrackedUpstreamSnapshotProbe(cacheKey: string, generation: symbol): void {
// Why: generations only guard an active probe; retaining completed repo keys
// leaks worktree/runtime identities after the short-lived snapshot TTL expires.
if (trackedUpstreamSnapshotGenerations.get(cacheKey) === generation) {
trackedUpstreamSnapshotGenerations.delete(cacheKey)
}
}
function pruneTrackedUpstreamSnapshotCache(now: number): void {
for (const [cacheKey, cached] of trackedUpstreamSnapshotCache) {
if (cached.expiresAt <= now) {
trackedUpstreamSnapshotCache.delete(cacheKey)
}
}
// Why: workspace/runtime churn can create unbounded unique keys within one
// TTL window, so expiry sweeping alone is not a memory bound.
while (trackedUpstreamSnapshotCache.size > TRACKED_UPSTREAM_SNAPSHOT_CACHE_MAX_ENTRIES) {
const oldestKey = trackedUpstreamSnapshotCache.keys().next().value
if (oldestKey === undefined) {
break
}
trackedUpstreamSnapshotCache.delete(oldestKey)
}
}
export function _getTrackedUpstreamBranchCacheSizesForTests(): {
snapshots: number
inFlight: number
generations: number
} {
return {
snapshots: trackedUpstreamSnapshotCache.size,
inFlight: trackedUpstreamSnapshotInFlight.size,
generations: trackedUpstreamSnapshotGenerations.size
}
}
export function __resetTrackedUpstreamBranchCacheForTests(): void {
@@ -2510,6 +2548,7 @@ async function getTrackedUpstreamBranch(
upstreamsByBranchName: getCacheableTrackedUpstreamSnapshot(result.upstreamsByBranchName),
expiresAt: Date.now() + TRACKED_UPSTREAM_SNAPSHOT_CACHE_TTL_MS
})
pruneTrackedUpstreamSnapshotCache(Date.now())
}
if (trackedUpstreamSnapshotGenerations.get(cacheKey) !== probeGeneration) {
const fresherCached = trackedUpstreamSnapshotCache.get(cacheKey)
@@ -2522,6 +2561,7 @@ async function getTrackedUpstreamBranch(
if (trackedUpstreamSnapshotInFlight.get(cacheKey) === probe) {
trackedUpstreamSnapshotInFlight.delete(cacheKey)
}
finishTrackedUpstreamSnapshotProbe(cacheKey, probeGeneration)
}
}