Skip to content

Fix deadlock in IO thread shutdown during panic - #2898

Merged
ranshid merged 1 commit into
valkey-io:unstablefrom
ouriamzn:fix-io-thread-deadlock
Dec 4, 2025
Merged

ranshid merged 1 commit into
valkey-io:unstablefrom
ouriamzn:fix-io-thread-deadlock

Conversation

@ouriamzn

@ouriamzn ouriamzn commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

Problem

IO thread shutdown can deadlock during server panic when the main thread calls pthread_cancel() while the IO thread holds its mutex, preventing the thread from observing the cancellation.

Solution

Release the IO thread mutex before cancelling to ensure clean thread termination.

Testing

Reproducer:

bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic

Before: Server hangs indefinitely
After: Server terminates cleanly

@codecov

codecov Bot commented Dec 3, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.46%. Comparing base (70196ee) to head (8ba08f8).
⚠️ Report is 2 commits behind head on unstable.

Additional details and impacted files
@@            Coverage Diff            @@
##           unstable    #2898   +/-   ##
=========================================
  Coverage     72.45%   72.46%           
=========================================
  Files           129      129           
  Lines         70526    70528    +2     
=========================================
+ Hits          51102    51109    +7     
+ Misses        19424    19419    -5     
Files with missing lines Coverage Δ
src/io_threads.c 35.57% <100.00%> (+0.41%) ⬆️

... and 13 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.

@ranshid
ranshid requested review from ranshid and uriyage December 3, 2025 16:23
Comment thread src/io_threads.c Resolved
The IO thread shutdown could deadlock when the main thread calls
pthread_cancel() while the IO thread holds its mutex. The IO thread
would not observe the cancellation request, causing pthread_join()
to block indefinitely.

Release the IO thread mutex before cancelling to ensure the thread
can process the cancellation request and exit cleanly.

Reproducer:
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic

Signed-off-by: Ouri Half <[email protected]>
@ouriamzn
ouriamzn force-pushed the fix-io-thread-deadlock branch from af34ff0 to 8ba08f8 Compare December 4, 2025 08:54
@ranshid ranshid moved this to To be backported in Valkey 8.1 Dec 4, 2025
@ranshid ranshid moved this to To be backported in Valkey 9.0 Dec 4, 2025
@ranshid ranshid removed this from Valkey 8.1 Dec 4, 2025
@ouriamzn

ouriamzn commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

This deadlock was introduced by commit 3631208 which removed makeThreadKillable() from IO threads to fix jemalloc crashes. However, removing makeThreadKillable() had an unintended side effect: IO threads no longer respond to pthread_cancel() calls while holding mutexes.

When makeThreadKillable() was present, threads could be cancelled at any cancellation point, even while holding locks. After its removal, pthread_cancel() only works when threads explicitly call pthread_testcancel(), which doesn't happen while the thread holds its mutex in the shutdown path.

This creates a deadlock scenario during panic shutdown:

  1. Main thread calls shutdownIOThread()
  2. IO thread holds io_threads_mutex[id]
  3. Main thread calls pthread_cancel() but thread doesn't observe it
  4. pthread_join() blocks indefinitely waiting for a thread that will never exit

The fix ensures the mutex is released before cancellation, allowing the thread to process the cancellation request in its main loop where pthread_testcancel() is called.

@ranshid
ranshid merged commit 3d65a4a into valkey-io:unstable Dec 4, 2025
@zuiderkwast zuiderkwast moved this from To be backported to 9.0.1 (WIP) in Valkey 9.0 Dec 4, 2025
zuiderkwast pushed a commit to zuiderkwast/valkey that referenced this pull request Dec 4, 2025
## Problem
IO thread shutdown can deadlock during server panic when the main thread
calls `pthread_cancel()` while the IO thread holds its mutex, preventing
the thread from observing the cancellation.

## Solution  
Release the IO thread mutex before cancelling to ensure clean thread
termination.

## Testing
Reproducer:
```
bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic
```

Before: Server hangs indefinitely
After: Server terminates cleanly

Signed-off-by: Ouri Half <[email protected]>
@ouriamzn
ouriamzn deleted the fix-io-thread-deadlock branch December 7, 2025 10:00
zuiderkwast pushed a commit that referenced this pull request Dec 9, 2025
## Problem
IO thread shutdown can deadlock during server panic when the main thread
calls `pthread_cancel()` while the IO thread holds its mutex, preventing
the thread from observing the cancellation.

## Solution  
Release the IO thread mutex before cancelling to ensure clean thread
termination.

## Testing
Reproducer:
```
bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic
```

Before: Server hangs indefinitely
After: Server terminates cleanly

Signed-off-by: Ouri Half <[email protected]>
aradz44 pushed a commit to aradz44/valkey that referenced this pull request Dec 23, 2025
## Problem
IO thread shutdown can deadlock during server panic when the main thread
calls `pthread_cancel()` while the IO thread holds its mutex, preventing
the thread from observing the cancellation.

## Solution  
Release the IO thread mutex before cancelling to ensure clean thread
termination.

## Testing
Reproducer:
```
bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic
```

Before: Server hangs indefinitely
After: Server terminates cleanly

Signed-off-by: Ouri Half <[email protected]>
jdheyburn pushed a commit to jdheyburn/valkey that referenced this pull request Jan 8, 2026
## Problem
IO thread shutdown can deadlock during server panic when the main thread
calls `pthread_cancel()` while the IO thread holds its mutex, preventing
the thread from observing the cancellation.

## Solution  
Release the IO thread mutex before cancelling to ensure clean thread
termination.

## Testing
Reproducer:
```
bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic
```

Before: Server hangs indefinitely
After: Server terminates cleanly

Signed-off-by: Ouri Half <[email protected]>
hpatro pushed a commit to hpatro/valkey that referenced this pull request Mar 5, 2026
## Problem
IO thread shutdown can deadlock during server panic when the main thread
calls `pthread_cancel()` while the IO thread holds its mutex, preventing
the thread from observing the cancellation.

## Solution
Release the IO thread mutex before cancelling to ensure clean thread
termination.

## Testing
Reproducer:
```
bash
./src/valkey-server --io-threads 2 --enable-debug-command yes
./src/valkey-cli debug panic
```

Before: Server hangs indefinitely
After: Server terminates cleanly

Signed-off-by: Ouri Half <[email protected]>
Signed-off-by: Harkrishn Patro <[email protected]>
rainsupreme 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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
Backport-Source-PR: 5366
valkeyrie-ops Bot pushed a commit that referenced this pull request Dec 2, 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 <[email protected]>
Backport-Source-PR: 5366
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[bot]@users.noreply.github.com>
valkeyrie-ops Bot pushed a commit that referenced this pull request Dec 2, 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 <[email protected]>
Backport-Source-PR: 5366
Signed-off-by: valkeyrie-ops[bot] <284117741+valkeyrie-ops[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: 9.0.1

Development

Successfully merging this pull request may close these issues.

3 participants