From 8064a3ac31ba86fff07f9db8fa04bbaf727be6c7 Mon Sep 17 00:00:00 2001
From: Peter Dillinger <peterd@fb.com>
Date: Fri, 21 Jan 2022 08:24:06 -0800
Subject: [PATCH] Fix flaky EventListenerTest.DisableBGCompaction (#9400)

Summary:
Wasn't able to easily reproduce error, but easy to see a race
condition between TestFlushListener::OnFlushCompleted and
DBTestBase::Close(), which frees CF handles before closing DB.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9400

Test Plan: CI etc.

Reviewed By: riversand963

Differential Revision: D33645134

Pulled By: pdillinger

fbshipit-source-id: d0ec914cc43c9e14f53da633876b95b61995138d
---
 db/db_impl/db_impl_debug.cc | 2 +-
 db/listener_test.cc         | 3 +++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/db/db_impl/db_impl_debug.cc b/db/db_impl/db_impl_debug.cc
index d7e0628f63..c93b55c1e9 100644
--- a/db/db_impl/db_impl_debug.cc
+++ b/db/db_impl/db_impl_debug.cc
@@ -159,7 +159,7 @@ Status DBImpl::TEST_AtomicFlushMemTables(
 Status DBImpl::TEST_WaitForBackgroundWork() {
   InstrumentedMutexLock l(&mutex_);
   WaitForBackgroundWork();
-  return Status::OK();
+  return error_handler_.GetBGError();
 }
 
 Status DBImpl::TEST_WaitForFlushMemTable(ColumnFamilyHandle* column_family) {
diff --git a/db/listener_test.cc b/db/listener_test.cc
index d087eb0778..fd4470e3b6 100644
--- a/db/listener_test.cc
+++ b/db/listener_test.cc
@@ -515,6 +515,9 @@ TEST_F(EventListenerTest, DisableBGCompaction) {
     db_->GetColumnFamilyMetaData(handles_[1], &cf_meta);
   }
   ASSERT_GE(listener->slowdown_count, kSlowdownTrigger * 9);
+  // We don't want the listener executing during DBTestBase::Close() due to
+  // race on handles_.
+  ASSERT_OK(dbfull()->TEST_WaitForBackgroundWork());
 }
 
 class TestCompactionReasonListener : public EventListener {