Skip to content

Commit bce8f2b

Browse files
committed
fix(pool): prevent race conditions in pool client return
1 parent e96db0d commit bce8f2b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

packages/client/lib/client/pool.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,35 @@ describe('RedisClientPool', () => {
88
'PONG'
99
);
1010
}, GLOBAL.SERVERS.OPEN);
11+
12+
testUtils.testWithClientPool(
13+
'proper error propagation in sequential operations',
14+
async (pool) => {
15+
let hasUnhandledRejection = false;
16+
17+
process.once('unhandledRejection', () => {
18+
hasUnhandledRejection = true;
19+
});
20+
21+
const groupName = 'test-group';
22+
const streamName = 'test-stream';
23+
24+
// First attempt - should succeed
25+
await pool.xGroupCreate(streamName, groupName, '0', {
26+
MKSTREAM: true,
27+
});
28+
29+
// Subsequent attempts - should all throw BUSYGROUP errors and be handled properly
30+
for (let i = 0; i < 3; i++) {
31+
await assert.rejects(
32+
pool.xGroupCreate(streamName, groupName, '0', {
33+
MKSTREAM: true,
34+
})
35+
);
36+
}
37+
38+
assert.equal(hasUnhandledRejection, false);
39+
},
40+
GLOBAL.SERVERS.OPEN
41+
);
1142
});

packages/client/lib/client/pool.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,9 @@ export class RedisClientPool<
438438
) {
439439
const result = fn(node.value);
440440
if (result instanceof Promise) {
441-
result.then(resolve, reject);
442-
result.finally(() => this.#returnClient(node))
441+
result
442+
.then(resolve, reject)
443+
.finally(() => this.#returnClient(node))
443444
} else {
444445
resolve(result);
445446
this.#returnClient(node);

0 commit comments

Comments
 (0)