Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { CLI } from './cli.js'

const exitCode = await CLI.run(process.argv.slice(2))
process.exit(exitCode)
// Top-level catch: without it, any error escaping CLI.run rejects the
// top-level await and Node kills the process with a raw stack trace —
// including errors thrown while a finally block runs during shutdown.
try {
const exitCode = await CLI.run(process.argv.slice(2))
process.exit(exitCode)
} catch (e) {
const message = e instanceof Error ? e.message : String(e)
console.error(`[specbandit] Fatal: ${message}`)
process.exit(1)
}
18 changes: 16 additions & 2 deletions src/redisQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,30 @@ export class RedisQueue {
})
}

/**
* Close the connection without ever throwing. QUIT is a regular command,
* so during an outage it hits commandTimeout and rejects — and close()
* runs in exit paths (finally blocks), where a throw masks the real error
* or crashes an otherwise-green run. Fall back to a hard disconnect,
* which drops the socket and pending commands without a round-trip.
*/
async close(): Promise<void> {
await this.redis.quit()
try {
await this.redis.quit()
} catch {
this.redis.disconnect()
}
}

private async withRetries<T>(operation: string, fn: () => Promise<T>): Promise<T> {
for (let attempt = 1; attempt <= this.maxAttempts; attempt++) {
try {
return await fn()
} catch (error) {
if (attempt === this.maxAttempts) throw error
if (attempt === this.maxAttempts) {
const message = error instanceof Error ? error.message : String(error)
throw new Error(`Redis ${operation} failed after ${this.maxAttempts} attempts: ${message}`)
}
const delay = Math.min(BASE_DELAY_MS * Math.pow(2, attempt - 1), MAX_BACKOFF_MS)
console.warn(`[specbandit] Redis ${operation} failed (attempt ${attempt}/${this.maxAttempts}), retrying in ${delay}ms: ${error}`)
await new Promise(resolve => setTimeout(resolve, delay))
Expand Down
12 changes: 11 additions & 1 deletion test/redisQueue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('RedisQueue', () => {
set: ReturnType<typeof vi.fn>
exists: ReturnType<typeof vi.fn>
quit: ReturnType<typeof vi.fn>
disconnect: ReturnType<typeof vi.fn>
}

beforeEach(() => {
Expand All @@ -27,6 +28,7 @@ describe('RedisQueue', () => {
set: vi.fn(),
exists: vi.fn(),
quit: vi.fn(),
disconnect: vi.fn(),
}

// Replace the redis instance with our mock
Expand Down Expand Up @@ -224,7 +226,7 @@ describe('RedisQueue', () => {

const result = await promise
expect(result).toBeInstanceOf(Error)
expect((result as Error).message).toBe('persistent failure')
expect((result as Error).message).toBe('Redis length failed after 5 attempts: persistent failure')
expect(mockRedis.llen).toHaveBeenCalledTimes(5)
expect(warnSpy).toHaveBeenCalledTimes(4)

Expand Down Expand Up @@ -305,6 +307,14 @@ describe('RedisQueue', () => {

await queue.close()
expect(mockRedis.quit).toHaveBeenCalled()
expect(mockRedis.disconnect).not.toHaveBeenCalled()
})

it('falls back to disconnect and does not throw when QUIT fails', async () => {
mockRedis.quit.mockRejectedValue(new Error('Command timed out'))

await expect(queue.close()).resolves.toBeUndefined()
expect(mockRedis.disconnect).toHaveBeenCalled()
})
})
})
Loading