From 09426ae1c7b6fa48d560d019297b96945d42c199 Mon Sep 17 00:00:00 2001 From: DorianZheng Date: Mon, 12 Nov 2018 11:50:32 -0800 Subject: [PATCH] Fix `DBImpl::GetColumnFamilyHandleUnlocked` data race (#4666) Summary: Hi, yiwu-arbug, I found that `DBImpl::GetColumnFamilyHandleUnlocked` still have data race condition, because `column_family_memtables_` has a stateful cache `current_` and `column_family_memtables_::Seek` maybe call without the protection of `mutex_` by a write thread check https://github.com/facebook/rocksdb/blob/859dbda6e3cac17416aff48f1760d01707867351/db/write_batch.cc#L1188 and https://github.com/facebook/rocksdb/blob/859dbda6e3cac17416aff48f1760d01707867351/db/write_batch.cc#L1756 and https://github.com/facebook/rocksdb/blob/859dbda6e3cac17416aff48f1760d01707867351/db/db_impl_write.cc#L318 So it's better to use `versions_->GetColumnFamilySet()->GetColumnFamily` instead. Pull Request resolved: https://github.com/facebook/rocksdb/pull/4666 Differential Revision: D13027117 Pulled By: yiwu-arbug fbshipit-source-id: 4e3778eaf8e7f7c8577bbd78129b6a5fd7ce79fb --- db/db_impl.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 7d5a87c68..27c4be8a6 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -2215,16 +2215,16 @@ ColumnFamilyHandle* DBImpl::GetColumnFamilyHandle(uint32_t column_family_id) { // REQUIRED: mutex is NOT held. std::unique_ptr DBImpl::GetColumnFamilyHandleUnlocked( uint32_t column_family_id) { - ColumnFamilyMemTables* cf_memtables = column_family_memtables_.get(); - InstrumentedMutexLock l(&mutex_); - if (!cf_memtables->Seek(column_family_id)) { + auto* cfd = + versions_->GetColumnFamilySet()->GetColumnFamily(column_family_id); + if (cfd == nullptr) { return nullptr; } return std::unique_ptr( - new ColumnFamilyHandleImpl(cf_memtables->current(), this, &mutex_)); + new ColumnFamilyHandleImpl(cfd, this, &mutex_)); } void DBImpl::GetApproximateMemTableStats(ColumnFamilyHandle* column_family,