Skip to content

Commit 5cb3b14

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

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,16 @@ 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.then(
442+
(value) => {
443+
resolve(value);
444+
this.#returnClient(node);
445+
},
446+
(error) => {
447+
reject(error);
448+
this.#returnClient(node);
449+
}
450+
);
443451
} else {
444452
resolve(result);
445453
this.#returnClient(node);

0 commit comments

Comments
 (0)