Skip to content

Commit 6badce2

Browse files
jameseh96daverigby
authored andcommitted
MB-43028: [1/2] Make overhead tracking safe at VBucket destruction
Merging http://review.couchbase.org/c/kv_engine/+/136495 into master uncovered santizer issues (mad-hatter CV runs an older Clang and did not identify these issues). This patch resolves one of these issues, before the above patch may be merged to master. WARNING: ThreadSanitizer: heap-use-after-free (pid=73551) Read of size 8 at 0x7b5800000310 by main thread: #0 operator() ../kv_engine/engines/ep/src/ephemeral_bucket.cc:301 (libep.so+0x00000034705f) #1 std::_Function_handler<void (long), EphemeralBucket::makeVBucket(...>::_M_invoke(std::_Any_data const&, long&&) #3 ~Checkpoint ../kv_engine/engines/ep/src/checkpoint.cc:228 (libep.so+0x00000019dc97) #10 ~CheckpointManager ../kv_engine/engines/ep/src/checkpoint_manager.h:73 (libep.so+0x00000042f782) ... #13 ~VBucket ../kv_engine/engines/ep/src/vbucket.cc:286 (libep.so+0x00000041689a) During VBucket destruction, the CheckpointManager member, and any remaining Checkpoints it holds are destroyed. This can trigger the memOverheadChangedCallback, as destroying Checkpoints reduces the memory overhead. This was unsafe, as the state member had already been destroyed as it is declared later in VBucket. Move the CheckpointManager to be declared after state, so it is destroyed first. Change-Id: I0a34d3a11cd053f18f3168d6fbbb9dc974bbd2fc Reviewed-on: http://review.couchbase.org/c/kv_engine/+/141492 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]> Well-Formed: Build Bot <[email protected]>
1 parent ac69da7 commit 6badce2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

engines/ep/src/vbucket.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,6 @@ VBucket::VBucket(Vbid i,
186186
const nlohmann::json& replTopology,
187187
uint64_t maxVisibleSeqno)
188188
: ht(st, std::move(valFact), config.getHtSize(), config.getHtLocks()),
189-
checkpointManager(std::make_unique<CheckpointManager>(st,
190-
i,
191-
chkConfig,
192-
lastSeqno,
193-
lastSnapStart,
194-
lastSnapEnd,
195-
maxVisibleSeqno,
196-
flusherCb)),
197189
failovers(std::move(table)),
198190
opsCreate(0),
199191
opsDelete(0),
@@ -216,6 +208,14 @@ VBucket::VBucket(Vbid i,
216208
id(i),
217209
state(newState),
218210
initialState(initState),
211+
checkpointManager(std::make_unique<CheckpointManager>(st,
212+
i,
213+
chkConfig,
214+
lastSeqno,
215+
lastSnapStart,
216+
lastSnapEnd,
217+
maxVisibleSeqno,
218+
flusherCb)),
219219
purge_seqno(purgeSeqno),
220220
takeover_backed_up(false),
221221
persistedRange(lastSnapStart, lastSnapEnd),

engines/ep/src/vbucket.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,9 +907,6 @@ class VBucket : public std::enable_shared_from_this<VBucket> {
907907

908908
HashTable ht;
909909

910-
/// Manager of this vBucket's checkpoints. unique_ptr for pimpl.
911-
std::unique_ptr<CheckpointManager> checkpointManager;
912-
913910
/**
914911
* Searches for a 'valid' StoredValue in the VBucket.
915912
*
@@ -2471,6 +2468,15 @@ class VBucket : public std::enable_shared_from_this<VBucket> {
24712468

24722469
vbucket_state_t initialState;
24732470

2471+
public:
2472+
/**
2473+
* Manager of this vBucket's checkpoints. unique_ptr for pimpl.
2474+
* Declared after state as Checkpoint destruction may update stats
2475+
* based on the vbucket's current state.
2476+
*/
2477+
std::unique_ptr<CheckpointManager> checkpointManager;
2478+
2479+
private:
24742480
/**
24752481
* The replication topology, set as part of SET_VBUCKET_STATE.
24762482
* It is encoded as nlohmann::json array of (max 2) replication chains.

0 commit comments

Comments
 (0)