Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/bus-subscribe-unhandled-rejection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'bentocache': patch
---

Fix an unhandled promise rejection when the bus subscription fails, for example when a cache is disconnected before its Redis bus connection is ready. The error is now logged instead of crashing the process.
10 changes: 8 additions & 2 deletions packages/bentocache/src/bus/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ export class Bus {

if (name) this.#channelName += `:${name}`

this.#wrapInternalOperation(() => {
this.#bus.subscribe<CacheBusMessage>(this.#channelName, this.#onMessage.bind(this))
/**
* The subscription rejects when the connection closes before it is ready,
* for example when the cache is disconnected right after its creation
*/
this.#wrapInternalOperation(() =>
this.#bus.subscribe<CacheBusMessage>(this.#channelName, this.#onMessage.bind(this)),
).catch(err => {
this.#logger.error({ err, channel: this.#channelName }, 'failed to subscribe to bus channel')
})
this.#logger.trace({ channel: this.#channelName }, 'bus subscribed to channel')
}
Expand Down
28 changes: 27 additions & 1 deletion packages/bentocache/tests/bus/bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { sleep } from '@julr/utils/misc'
import { MemoryTransport } from '@boringnode/bus/transports/memory'
import { Redis as IoRedis, Cluster as IoRedisCluster } from 'ioredis'

import { BentoCache, bentostore } from '../../index.js'
import { ChaosBus } from '../helpers/chaos/chaos_bus.js'
import { memoryDriver } from '../../src/drivers/memory.js'
import { ChaosCache } from '../helpers/chaos/chaos_cache.js'
import { CacheBusMessageType } from '../../src/types/bus.js'
import { CacheFactory } from '../../factories/cache_factory.js'
import { RedisDriver, redisBusDriver } from '../../src/drivers/redis.js'
import { REDIS_CREDENTIALS, throwingFactory } from '../helpers/index.js'
import { RedisDriver, redisBusDriver, redisDriver } from '../../src/drivers/redis.js'

test.group('Bus synchronization', () => {
test('synchronize multiple cache', async ({ assert }) => {
Expand Down Expand Up @@ -446,6 +448,30 @@ test.group('Bus synchronization', () => {
await sleep(200)
}).waitForDone()

test('disconnecting before the bus subscription is ready does not leave an unhandled rejection', async ({
assert,
}) => {
const rejections: unknown[] = []
const onRejection = (reason: unknown) => rejections.push(reason)
process.on('unhandledRejection', onRejection)

const bento = new BentoCache({
default: 'cache',
stores: {
cache: bentostore()
.useL1Layer(memoryDriver({}))
.useL2Layer(redisDriver({ connection: REDIS_CREDENTIALS }))
.useBus(redisBusDriver({ connection: REDIS_CREDENTIALS })),
},
})
bento.use('cache')
await bento.disconnectAll()
await sleep(50)

process.off('unhandledRejection', onRejection)
assert.deepEqual(rejections, [])
})

test('works with an existing Cluster instance', async ({ assert }, done) => {
const cluster = new IoRedisCluster([{ host: '127.0.0.1', port: 7000 }])

Expand Down
2 changes: 1 addition & 1 deletion packages/bentocache/tests/cache/factory_context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ test.group('Factory Context', () => {
factory: () => 'bar',
})

await sleep(100)
await sleep(150)

const r1 = await cache.getOrSet({
key: 'foo',
Expand Down
4 changes: 2 additions & 2 deletions packages/bentocache/tests/cache/one_tier_local.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,13 @@ test.group('One tier cache', () => {
const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

// wait for expiration
await sleep(100)
await sleep(150)

// factory that will exceed soft timeout
const r2 = await cache.getOrSet({ key: 'key1', factory: slowFactory(550, { foo: 'baz' }) })

// wait til factory is done
await sleep(50)
await sleep(100)

// get the value
const r3 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bazzz' }) })
Expand Down
4 changes: 2 additions & 2 deletions packages/bentocache/tests/cache/two_tier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ test.group('Two tier cache', () => {
const r1 = await cache.getOrSet({ key: 'key1', factory: () => ({ foo: 'bar' }) })

// wait for expiration
await sleep(100)
await sleep(150)

// get the value again
const r2 = await cache.getOrSet({
Expand Down Expand Up @@ -335,7 +335,7 @@ test.group('Two tier cache', () => {
remoteDriver.alwaysThrow()

// wait till we enter the grace period
await sleep(100)
await sleep(150)

// get the value again
const r2 = cache.getOrSet({
Expand Down
Loading