Skip to content

Commit 64ca6df

Browse files
committed
fix(mcp): preserve ambiguous refresh locks
1 parent 00491d9 commit 64ca6df

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

apps/sim/lib/mcp/oauth/storage.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,15 @@ describe('withMcpOauthRefreshLock', () => {
164164
expect(fn).not.toHaveBeenCalled()
165165
})
166166

167-
it('cleans up an uncertain owner token before falling open when Redis is unavailable', async () => {
167+
it('preserves an uncertain owner token when falling open after an acquire failure', async () => {
168168
mockAcquireLock.mockRejectedValueOnce(new Error('Redis connection refused'))
169169
const fn = vi.fn(async () => 'uncoordinated')
170170

171171
const result = await withMcpOauthRefreshLock('row-redis-down', fn)
172172

173173
expect(result).toBe('uncoordinated')
174174
expect(fn).toHaveBeenCalledTimes(1)
175-
expect(mockReleaseLock).toHaveBeenCalledWith(
176-
'mcp:oauth:refresh:row-redis-down',
177-
expect.any(String)
178-
)
175+
expect(mockReleaseLock).not.toHaveBeenCalled()
179176
})
180177

181178
it('cleans up an uncertain owner token before propagating cancellation', async () => {

apps/sim/lib/mcp/oauth/storage.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,15 @@ async function runWithRedisMutex<T>(
350350
try {
351351
acquired = await acquireLock(lockKey, ownerToken, REFRESH_LOCK_TTL_SEC)
352352
} catch (error) {
353-
await releaseLock(lockKey, ownerToken).catch((releaseError) => {
354-
logger.warn('Refresh lock cleanup after acquire failure failed (will expire via TTL)', {
355-
rowId,
356-
error: toError(releaseError).message,
353+
if (signal?.aborted) {
354+
await releaseLock(lockKey, ownerToken).catch((releaseError) => {
355+
logger.warn('Refresh lock cleanup after cancelled acquire failed (will expire via TTL)', {
356+
rowId,
357+
error: toError(releaseError).message,
358+
})
357359
})
358-
})
359-
signal?.throwIfAborted()
360+
signal.throwIfAborted()
361+
}
360362
logger.warn('Redis unavailable, running OAuth flow uncoordinated', {
361363
rowId,
362364
error: toError(error).message,

0 commit comments

Comments
 (0)