Skip to content

Fix double mutex unlock when shrinking IO threads - #5366

Merged
rainsupreme merged 1 commit into
valkey-io:unstablefrom
sarthakaggarwal97:fix-io-threads-shrink-double-unlock
Nov 19, 2026
Merged

rainsupreme merged 1 commit into
valkey-io:unstablefrom
sarthakaggarwal97:fix-io-threads-shrink-double-unlock

Conversation

@sarthakaggarwal97

Copy link
Copy Markdown
Contributor

When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that isn't locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread does the unlock like it does for killIOThreads.

No new test: with default mutexes the second unlock silently succeeds on Linux and macOS, so only error-checking mutexes or TSan would catch it. tests/unit/io-threads.tcl already covers shrinking the pool.

updateIOThreads locks the mutex of every active IO thread and sets
active_io_threads_num to 1 before resizing the pool. When shrinking, it
then unlocked the mutex of each removed thread and called
shutdownIOThread, which unlocks the same mutex again because the thread
id is now above active_io_threads_num. Unlocking a mutex that is not
locked is undefined behavior.

Drop the unlock in updateIOThreads and let shutdownIOThread do it, the
same way killIOThreads relies on it.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change β†’

No actionable comments were generated in the recent review.

Walkthrough

When reducing the configured I/O thread count, updateIOThreads no longer unlocks each inactive thread’s mutex before shutdown. shutdownIOThread handles that unlock.

Changes: I/O thread shutdown

Layer / File(s) Summary
Mutex unlock during shutdown
src/io_threads.c
The thread reduction path removes the explicit mutex unlock before calling shutdown. shutdownIOThread now handles the unlock.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Suggested reviewers: akashkgit, ayush933, roshkhatri

Merge Risk: Minimal Β· up to 395c8

This fixes a double unlock of a mutex when the number of IO threads is reduced, which is undefined behavior. No remaining merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: removing a duplicate mutex unlock when the IO thread pool shrinks. The changeset removes the unlock in updateIOThreads, so shutdownIOThread performs the only unlock. T…
Description check Passed The description states the bug, the cause, and the fix. It explains that updateIOThreads unlocked the mutex before shutdownIOThread unlocked it again, and that the PR removes the earlier unlock. It al…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: removing a duplicate mutex unlock when the IO thread pool shrinks. The changeset removes the unlock in updateIOThreads, so shutdownIOThread performs the only unlock. The title is specific and matches the diff summary and the PR objectives.

Full details: Description check

Explanation

The description states the bug, the cause, and the fix. It explains that updateIOThreads unlocked the mutex before shutdownIOThread unlocked it again, and that the PR removes the earlier unlock. It also explains why no new test was added. This matches the one-line change in src/io_threads.c.

  • Autofix Β· Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0c356c0f-c7bf-4ff3-b2e1-f5e3ed79c59d
Commits

Reviewing files that changed from the base of the PR and between 6f6ec61 and 395c875.

Files selected for processing: 1

  • src/io_threads.c

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@valkey-review-bot

Copy link
Copy Markdown
Contributor

Review state

Ready for human review.
The removed unlock was a real double unlock. updateIOThreads sets active_io_threads_num to 1 at src/io_threads.c:583, so shutdownIOThread unlocks every removed thread's mutex at src/io_threads.c:495-497. No open findings.

Risk: Low. A one-line removal on the CONFIG SET io-threads shrink path. If it were wrong, a removed IO thread would stay blocked and pthread_join would hang, and the existing io-threads.tcl shrink tests would catch that.

Open findings: none.

Backport: Needed for 9.2, 9.1, 9.0. upstream/9.2, 9.1 and 9.0 all have the extra pthread_mutex_unlock before shutdownIOThread in updateIOThreads, and shutdownIOThread there also unlocks inactive threads. 8.1, 8.0 and 7.2 do not have the shutdownIOThread unlock.

Updated for 395c875 at 2026-11-11 22:38 UTC. The bot rewrites this comment on every review.

@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 81.42%. Comparing base (1b03cf5) to head (395c875).
⚠️ Report is 4 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #5366      +/-   ##
============================================
- Coverage     81.44%   81.42%   -0.03%     
============================================
  Files           199      199              
  Lines        104752   104749       -3     
============================================
- Hits          85316    85290      -26     
- Misses        19436    19459      +23     
Files with missing lines Coverage Ξ”
src/io_threads.c 79.64% <ΓΈ> (-0.04%) ⬇️

... and 20 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Baraa-Hasheesh Baraa-Hasheesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@rainsupreme rainsupreme left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

I built the base and your branch with error-checking mutexes in createIOThread, and made the unlock calls in shutdownIOThread and, on the base, updateIOThreads abort on a non-zero return. On the base, unit/io-threads aborts when it shrinks the pool from 5 to 1 threads: the unlock of thread 4 in shutdownIOThread fails with EPERM, because updateIOThreads has already unlocked it. With your change the same test passes. updateIOThreads sets active_io_threads_num to 1 before the loop, so shutdownIOThread unlocks every removed thread itself.

@rainsupreme
rainsupreme merged commit 6357e3d into valkey-io:unstable Nov 19, 2026
66 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to To be backported in Valkey 9.0 Nov 19, 2026
@github-project-automation github-project-automation Bot moved this from Todo to To be backported in Valkey 9.2 Nov 19, 2026
@github-project-automation github-project-automation Bot moved this from Todo to To be backported in Valkey 9.1 Nov 19, 2026
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 19, 2026
When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that is not locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread is the single owner of the unlock, as it already is for killIOThreads.

No behavior change on Linux or macOS, where the second unlock on a default mutex returns 0; the existing io-threads.tcl shrink tests cover the path.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Backport-Source-PR: 5366
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 19, 2026
When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that is not locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread is the single owner of the unlock, as it already is for killIOThreads.

No behavior change on Linux or macOS, where the second unlock on a default mutex returns 0; the existing io-threads.tcl shrink tests cover the path.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Backport-Source-PR: 5366
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 19, 2026
When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that is not locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread is the single owner of the unlock, as it already is for killIOThreads.

No behavior change on Linux or macOS, where the second unlock on a default mutex returns 0; the existing io-threads.tcl shrink tests cover the path.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Backport-Source-PR: 5366
valkeyrie-ops Bot pushed a commit that referenced this pull request Nov 25, 2026
When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that is not locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread is the single owner of the unlock, as it already is for killIOThreads.

No behavior change on Linux or macOS, where the second unlock on a default mutex returns 0; the existing io-threads.tcl shrink tests cover the path.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Backport-Source-PR: 5366
ranshid pushed a commit that referenced this pull request Nov 25, 2026
When the IO threads pool shrinks, updateIOThreads unlocks each removed thread's mutex and then calls shutdownIOThread, which unlocks the same mutex again. Unlocking a mutex that is not locked is undefined behavior.

The unlock in shutdownIOThread was added in #2898, but the older one in updateIOThreads was never removed. This PR removes it, so shutdownIOThread is the single owner of the unlock, as it already is for killIOThreads.

No behavior change on Linux or macOS, where the second unlock on a default mutex returns 0; the existing io-threads.tcl shrink tests cover the path.

Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Backport-Source-PR: 5366
@valkeyrie-ops valkeyrie-ops Bot moved this from To be backported to Done in Valkey 9.2 Nov 25, 2026
ranshid pushed a commit that referenced this pull request Nov 26, 2026
> [!CAUTION]
> **Opened ready with 1 unresolved security item** (`force_ready` was
set): release impact may require higher urgency or security treatment.
Security classification is human-owned: settle the urgency and the
Security Fixes entries (re-run the cut with them, or edit through review
comments) before merging. All 4 flagged items: notes flagged
low-confidence; AI triaged PRs without release-notes (confirm
include/exclude); release impact may require higher urgency or security
treatment; notes credited to a backport.

Cuts **9.2.1** onto release line `9.2`.

- Promotes the release notes into a dated section, bumps
`src/version.h`, and refreshes the running contributor list.
- Release notes computed over the range below
(`9.2.0..9dc1926`):

```
mode: ga
source_ref: 9.2
target_branch: 9.2
base: 9.2.0 @ d23684e
head: 9dc1926 @ 9dc1926
```

### ⚠️ Notes to double-check

The generator flagged these notes low-confidence; confirm category and
wording before merging:

| PR | Category | Why flagged |
|----|----------|-------------|
| #5366 | Bug Fixes | explicit impact scope omitted: Linux |

### ⚠️ Release impact and urgency need review

Code detected release-impact terms (availability, memory safety, data
integrity, access control, protocol, compatibility) in these PRs; this
forces review, it is not a severity classification.

The requested urgency is **MODERATE**. Confirm whether these changes
require `HIGH`, `CRITICAL`, or `SECURITY`, and whether any need a
hand-authored **Security Fixes** entry:

<details>
<summary>7 PR(s) with impact signals</summary>

| PR | Title | Signal |
|----|-------|--------|
| [#5366](#5366) | Fix double
mutex unlock when shrinking IO threads | undefined behavior or unsafe
arithmetic |
| [#5468](#5468) | Don't resend
data to a close_asap client after an IO-thread write | server crash,
assertion, or availability failure |
| [#5112](#5112) | Pause
rehashing of path hash payloads during background iteration | server
crash, assertion, or availability failure |
| [#4712](#4712) | Fix
execGetKeys() dropping the first 256 keys past the static buffer |
server crash, assertion, or availability failure |
| [#4675](#4675) | Fix cluster
bus self-forget use-after-free | memory-safety risk |
| [#5452](#5452) | Ignore
replica claims that would close a replicaof cycle | server crash,
assertion, or availability failure |
| [#3245](#3245) | Fix assertion
crash in processIOThreadsReadDone when DONT_PARSE client has stale argc
| server crash, assertion, or availability failure |

</details>

### Triage summary

- 8 PR(s) triaged into the notes
- 3 PR(s) triaged out

Per-PR verdicts (with reasons) are in the run's `triage-verdicts`
artifact. To include a wrongly-dropped PR, label it `release-notes` and
re-cut.

### ⚠️ Notes credited to a backport (original PR not recovered)

These notes credit the backport PR, not the change's author; confirm the
original PR and correct the credit before merging:

| Backport PR | Title |
|-------------|-------|
| [#5550](#5550) | [9.2] Revert
fail-fast TLS certificate checks (#2999, #3522) |

### Wording carried from other release lines

A backported change is cut once per release line, and each cut words its
notes independently. To keep one commit from being described differently
per branch, a note whose source PR another line already published takes
that published wording verbatim (joined on the trailing `(#N)`, which
always names the original source PR). Only the text is reused; the
category, the `by @handle` credit, and the `(#N)` are this cut's own.

- Release lines consulted: `9.1`, `9.0`, `8.1`, `8.0`, `7.2`, `7.0`,
`6.2`, `6.0`, `5.0`, `4.0`, `3.2`, `3.0`, `2.8`, `2.6`, `2.4`, `2.2`
- No note needed its wording replaced.

*Generated by valkey-ci-agent. Review before merging into the release
line.*

Signed-off-by: valkeyrie-bot[bot] <3692572+valkeyrie-bot[bot]@users.noreply.github.com>
Co-authored-by: valkeyrie-bot[bot] <3692572+valkeyrie-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To be backported
Status: To be backported
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants