Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp): handle redis errors in context-mill background refresh lock acquire (#91613)

Alessandro Pogliaghi602471690b92
diff --git a/services/mcp/src/hono/cache/ContextMillResourceCache.ts b/services/mcp/src/hono/cache/ContextMillResourceCache.ts
index a6911f36a211..e545f85bfbcb 100644
--- a/services/mcp/src/hono/cache/ContextMillResourceCache.ts
+++ b/services/mcp/src/hono/cache/ContextMillResourceCache.ts
@@ -150,12 +150,14 @@ export class ContextMillResourceCache extends SharedBlobCache {
private async refreshInBackground(): Promise<void> {
const token = randomUUID()
- if (!(await this.acquireLock(token))) {
- contextMillCacheEventsTotal.inc({ event: 'lock_contended' })
- return
- }
- contextMillCacheEventsTotal.inc({ event: 'lock_acquired' })
+ let acquired = false
try {
+ acquired = await this.acquireLock(token)
+ if (!acquired) {
+ contextMillCacheEventsTotal.inc({ event: 'lock_contended' })
+ return
+ }
+ contextMillCacheEventsTotal.inc({ event: 'lock_acquired' })
const bytes = await this.loadBlob()
await this.writeCache(bytes)
contextMillCacheEventsTotal.inc({ event: 'background_success' })
@@ -163,7 +165,9 @@ export class ContextMillResourceCache extends SharedBlobCache {
contextMillCacheEventsTotal.inc({ event: 'background_error' })
console.error(`[ContextMillResourceCache:${this.lockKey}] background refresh failed:`, err)
} finally {
- await this.releaseLock(token)
+ if (acquired) {
+ await this.releaseLock(token)
+ }
}
}
diff --git a/services/mcp/tests/hono/context-mill-resource-cache.test.ts b/services/mcp/tests/hono/context-mill-resource-cache.test.ts
index 8b500f8cc37f..6e93a18553e3 100644
--- a/services/mcp/tests/hono/context-mill-resource-cache.test.ts
+++ b/services/mcp/tests/hono/context-mill-resource-cache.test.ts
@@ -319,6 +319,36 @@ describe('ContextMillResourceCache', () => {
}
})
+ it('degrades to background_error when the lock acquire fails, without an unhandled rejection', async () => {
+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
+ const unhandled = vi.fn()
+ process.on('unhandledRejection', unhandled)
+ try {
+ const cache = new TestContextMillResourceCache(redis, async () => [makeEntry('a')], {
+ freshSeconds: 0,
+ waitIntervalMs: 5,
+ waitTimeoutMs: 100,
+ })
+ await cache.loadOrRefresh()
+ cache.setLoader(async () => [makeEntry('b')])
+ redis.set = vi.fn(async () => {
+ throw new Error('Command timed out')
+ })
+
+ const stale = await cache.loadOrRefresh()
+ expect(stale.result).toBe('stale_hit')
+
+ await new Promise((r) => setTimeout(r, 30))
+
+ expect(mockCacheEventsInc).toHaveBeenCalledWith({ event: 'background_error' })
+ expect(redis._store.has(MANIFEST_LOCK_KEY)).toBe(false)
+ expect(unhandled).not.toHaveBeenCalled()
+ } finally {
+ process.off('unhandledRejection', unhandled)
+ consoleError.mockRestore()
+ }
+ })
+
it('falls back to a direct load when the writer never publishes', async () => {
redis._store.set(MANIFEST_LOCK_KEY, 'someone-else')
const entries = [makeEntry('fallback')]