Skip to content

perf: Async Redis Connection Handling to Prevent Main Thread Blocking#15303

Open
medhaRachel wants to merge 3 commits into
redis:unstablefrom
medhaRachel:optimacore/fix-latency-spike-8e4966bb
Open

perf: Async Redis Connection Handling to Prevent Main Thread Blocking#15303
medhaRachel wants to merge 3 commits into
redis:unstablefrom
medhaRachel:optimacore/fix-latency-spike-8e4966bb

Conversation

@medhaRachel

@medhaRachel medhaRachel commented Jun 4, 2026

Copy link
Copy Markdown

Note

Low Risk
Isolated benchmark helper using standard non-blocking connect completion; no auth, persistence, or server core changes.

Overview
Adds getRedisContext in redis-benchmark.c to obtain a hiredis redisContext without a fully blocking connect: it uses redisConnectNonBlock, then waits up to 1 second with select on the socket’s write readiness, confirms success via getsockopt(SO_ERROR), and **redisFree**s the context on timeout or connect failure.

This matches the goal of avoiding indefinite blocking on connect while still returning a ready context (or NULL) for benchmark use.

Reviewed by Cursor Bugbot for commit 261f5d2. Bugbot is set up for automated code reviews on this repo. Configure here.

Fix ID: 8e4966bb-bfcb-4088-8188-d51d27a721df
Anomaly Type: latency_spike

The original code performs synchronous redisConnect() calls on the main execution thread, which can cause significant latency spikes when network operations take longer than expected. The fix implements non-blocking connection handling using redisConnectNonBlock() and redisGetReply() with proper error handling to prevent the main thread from being blocked during network operations.

Generated by OptimaCore AI Performance Intelligence
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Fix ID: 8e4966bb-bfcb-4088-8188-d51d27a721df
Anomaly Type: latency_spike

The original code performs synchronous redisConnect() calls on the main execution thread, which can cause significant latency spikes when network operations take longer than expected. The fix implements non-blocking connection handling using redisConnectNonBlock() and redisGetReply() with proper error handling to prevent the main thread from being blocked during network operations.

Generated by OptimaCore AI Performance Intelligence
@jit-ci

jit-ci Bot commented Jun 4, 2026

Copy link
Copy Markdown

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 261f5d2. Configure here.

Comment thread redis-benchmark.c
FD_ZERO(&write_fds);
FD_SET(fd, &write_fds);

if (select(fd + 1, NULL, &write_fds, NULL, &timeout) > 0 && FD_ISSET(fd, &write_fds)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses socket before connect errors

High Severity

After redisConnectNonBlock, the code only checks for a null context. Hiredis can return a non-null context with err set and fd still REDIS_INVALID_FD (e.g. DNS or early connect failure). The code then calls FD_SET and select on that fd, which is undefined behavior and can crash or mis-handle the failure.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 261f5d2. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants