Skip to content

MOD-13142 Option to resize mr thpool after creation, but not after started#89

Merged
AvivDavid23 merged 5 commits into
masterfrom
MOD-13142
Mar 31, 2026
Merged

MOD-13142 Option to resize mr thpool after creation, but not after started#89
AvivDavid23 merged 5 commits into
masterfrom
MOD-13142

Conversation

@AvivDavid23

@AvivDavid23 AvivDavid23 commented Mar 26, 2026

Copy link
Copy Markdown
Collaborator

Note

Medium Risk
Touches core threading infrastructure: mr_thpool_init now rejects non-positive sizes and new resize logic reallocates the thread table, which could affect startup/config flows and error handling if callers previously passed 0 or resize at the wrong time.

Overview
Adds a public API (MR_ResizeExecutionThreadPoolIfUnstarted) to change the execution thread-pool size after MR_Init() creates the pool but before any worker threads are started, returning an error once work has begun.

Extends the threadpool implementation with mr_thpool_workers_started() and mr_thpool_resize_unstarted() to support this guard/resize behavior, and tightens mr_thpool_init() to fail (rather than silently creating a 0-thread pool) when num_threads <= 0.

Written by Cursor Bugbot for commit 1e32f98. This will update automatically on new commits. Configure here.

Comment thread src/mr.c
return REDISMODULE_ERR;
}

if (mr_thpool_resize_unstarted(tp, numThreads) != 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Implicit size_t-to-int narrowing truncates thread count

Medium Severity

MR_ResizeExecutionThreadPoolIfUnstarted accepts size_t numThreads but passes it directly to mr_thpool_resize_unstarted, which takes int num_threads. On 64-bit systems, values exceeding INT_MAX are silently truncated. The <= 0 guard inside the callee catches some overflow cases but not all — a size_t like (1UL << 32) + 5 truncates to 5, passing validation and allocating the wrong number of thread slots.

Additional Locations (1)
Fix in Cursor Fix in Web

Comment thread src/utils/thpool.c
Comment thread src/mr.c

@galcohen-redislabs galcohen-redislabs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also: what happened to Tom's tests?

Comment thread src/utils/thpool.c

if (num_threads < 0) {
num_threads = 0;
if (num_threads <= 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why the old code allowed 0? Was it "unlimited" maybe?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I dont think so. When it was 0, its just kept allocating 0 bytes for the threads, which is 0 threads(and is there a reason we should allow it)?

Comment thread src/utils/thpool.c Outdated
@AvivDavid23

Copy link
Copy Markdown
Collaborator Author

Also: what happened to Tom's tests?

Will be in TS repo on the next PR

@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.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread src/utils/thpool.c
num_threads = 0;
if (num_threads <= 0) {
err("thpool_init(): num_threads must be greater than 0\n");
return NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

mr_thpool_init returning NULL for zero is unchecked

Medium Severity

mr_thpool_init now returns NULL when num_threads is 0, but its only caller MR_Init does not check the return value and unconditionally returns REDISMODULE_OK. Previously, passing 0 created a valid (though empty) pool object. Now, mrCtx.executionsThreadPool is left NULL, and the first call to mr_thpool_add_work will dereference it in mr_thpool_start_threads, causing a NULL pointer crash.

Additional Locations (1)
Fix in Cursor Fix in Web

@AvivDavid23 AvivDavid23 merged commit 1d2f087 into master Mar 31, 2026
5 of 8 checks passed
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