Skip to content

Commit 962110e

Browse files
authored
fix(lock): keep the callback outcome when the Redis lock release fails (#2404)
1 parent 8e3e928 commit 962110e

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎packages/bun/src/redis-lock.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ describe.skipIf(!REDIS_URL)('bun redis locker integration', async () => {
179179
expect(count).toBe(5)
180180
}, { timeout: 20_000 })
181181
})
182+
183+
it('preserves the callback outcome when the release fails', async () => {
184+
const redis = {
185+
send: vi.fn((command: string) => command === 'SET' ? Promise.resolve('OK') : Promise.reject(new Error('connection lost'))),
186+
}
187+
const locker = new BunRedisLocker(redis as any, { ttl: 1000 })
188+
const error = new Error('fn failed')
189+
190+
await expect(locker.lock('key', () => 'ok')).resolves.toBe('ok')
191+
await expect(locker.lock('key', () => Promise.reject(error))).rejects.toBe(error)
192+
expect(redis.send).toHaveBeenCalledTimes(4)
193+
})

‎packages/lock/src/adapters/base-redis.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ export abstract class BaseRedisLocker implements Locker {
106106
return await fn({ waited })
107107
}
108108
finally {
109-
await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token])
109+
try {
110+
await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token])
111+
}
112+
catch {
113+
// the lock still expires through its TTL
114+
}
110115
}
111116
}
112117
}

‎packages/lock/src/adapters/redis.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,13 @@ describe.concurrent('redis locker integration', {
194194
expect(redis.isOpen).toBe(true)
195195
})
196196
})
197+
198+
it('preserves the callback outcome when the release fails', async () => {
199+
const redis = { isOpen: true, set: vi.fn(async () => 'OK'), eval: vi.fn().mockRejectedValue(new Error('connection lost')) }
200+
const locker = new RedisLocker(redis as any, { ttl: 1000 })
201+
const error = new Error('fn failed')
202+
203+
await expect(locker.lock('key', () => 'ok')).resolves.toBe('ok')
204+
await expect(locker.lock('key', () => Promise.reject(error))).rejects.toBe(error)
205+
expect(redis.eval).toHaveBeenCalledTimes(2)
206+
})

‎packages/lock/src/adapters/upstash.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,13 @@ it('prefix creates a locker that appends the prefix and shares the client', asyn
179179
await expect(locker.lock('key', () => 'ok')).resolves.toBe('ok')
180180
expect(redis.set).toHaveBeenCalledWith('app:tenant:key', expect.any(String), { nx: true, px: 1000 })
181181
})
182+
183+
it('preserves the callback outcome when the release fails', async () => {
184+
const redis = { set: vi.fn(async () => 'OK'), eval: vi.fn().mockRejectedValue(new Error('connection lost')) }
185+
const locker = new UpstashLocker(redis as any, { ttl: 1000 })
186+
const error = new Error('fn failed')
187+
188+
await expect(locker.lock('key', () => 'ok')).resolves.toBe('ok')
189+
await expect(locker.lock('key', () => Promise.reject(error))).rejects.toBe(error)
190+
expect(redis.eval).toHaveBeenCalledTimes(2)
191+
})

0 commit comments

Comments
 (0)