Commit d8a842f
committed
MB-54279: Pause / Resume: Unlock vb_mutexes from locking thread
As part of EPBucket::prepareForPause(), all of the vb_mutexes are
lock()ed - and left locked:
a) To ensure that any in-flight VBucket writes have completed and
b) To inhibit and new writes from occurring.
Then, when the EPBucket is resumed all the vb_mutexes are unlock()ed
which allows VBucket writes to resume.
However, EPBucket::prepareForResume() is not called on the same thread
which called prepareForPause() - prepareForPause() runs on a
background NonIO thread whereas resume runs synchronously in the
front-end thread. As such, we are incorrectly unlocking a mutex from a
different thread than the one which locked it - which is Undefined
Behaviour - from cppreference.com[1]:
void unlock();
Unlocks the mutex.
The mutex must be locked by the current thread of execution,
otherwise, the behavior is undefined.
This is helpfully reported by ThreadSanitizer:
WARNING: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) (pid=58528)
#0 pthread_mutex_unlock <null> (libtsan.so.0+0x3bf9a)
#1 __gthread_mutex_unlock(pthread_mutex_t*) c++/10.2.0/x86_64-pc-linux-gnu/bits/gthr-default.h:779 (memcached+0x5b594f)
#2 std::mutex::unlock() c++/10.2.0/bits/std_mutex.h:118 (memcached+0x602555)
#3 EPBucket::prepareForResume() kv_engine/engines/ep/src/ep_bucket.cc:2575 (memcached+0x84b94f)
#4 EventuallyPersistentEngine::resume() kv_engine/engines/ep/src/ep_engine.cc:7002 (memcached+0x7d52d3)
...
Fix by changing how we achieve inhibition of future VBucket writes:
- Introduce a EPBucket::paused flag which is set in prepareForPause
after all vb_mutexes have been acquired (and hence all in-flight
VBucket writes have finished), but then unlock all vb_mutexes before
returning from prepareForPause().
- When attempting to acquire a locked VBucket, check new paused flag
before attempting to acquire the vb_mutex - if paused is set then
block / return early (for try() variant).
This keeps the required pause behaviour but avoids keeping vb_mutexes
locked and having to later unlock (on a different thread).
[1]: https://en.cppreference.com/w/cpp/thread/mutex
Change-Id: I062583951a101a866866b79dfd6329672bb4ff42
Reviewed-on: https://review.couchbase.org/c/kv_engine/+/182099
Tested-by: Build Bot <[email protected]>
Reviewed-by: Jim Walker <[email protected]>1 parent 09969cd commit d8a842f
File tree
4 files changed
+41
-8
lines changed- engines/ep/src
4 files changed
+41
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
327 | 333 | | |
328 | 334 | | |
329 | 335 | | |
| |||
2498 | 2504 | | |
2499 | 2505 | | |
2500 | 2506 | | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
2501 | 2511 | | |
2502 | 2512 | | |
2503 | 2513 | | |
| 2514 | + | |
2504 | 2515 | | |
2505 | | - | |
| 2516 | + | |
2506 | 2517 | | |
2507 | 2518 | | |
2508 | 2519 | | |
| |||
2551 | 2562 | | |
2552 | 2563 | | |
2553 | 2564 | | |
2554 | | - | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
2555 | 2574 | | |
2556 | 2575 | | |
2557 | 2576 | | |
| |||
2567 | 2586 | | |
2568 | 2587 | | |
2569 | 2588 | | |
2570 | | - | |
| 2589 | + | |
| 2590 | + | |
2571 | 2591 | | |
2572 | | - | |
| 2592 | + | |
2573 | 2593 | | |
2574 | | - | |
2575 | | - | |
2576 | | - | |
| 2594 | + | |
2577 | 2595 | | |
2578 | 2596 | | |
2579 | 2597 | | |
2580 | | - | |
| 2598 | + | |
2581 | 2599 | | |
2582 | 2600 | | |
2583 | 2601 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
291 | 291 | | |
292 | 292 | | |
293 | 293 | | |
| 294 | + | |
294 | 295 | | |
295 | 296 | | |
296 | 297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
257 | 261 | | |
258 | 262 | | |
259 | 263 | | |
| |||
270 | 274 | | |
271 | 275 | | |
272 | 276 | | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
273 | 281 | | |
274 | 282 | | |
275 | 283 | | |
| |||
1178 | 1186 | | |
1179 | 1187 | | |
1180 | 1188 | | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
1181 | 1193 | | |
1182 | 1194 | | |
1183 | 1195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| |||
0 commit comments