Skip to content

Commit e316c2e

Browse files
committed
test/common: avoid leakage of CephContext
before this change, in test_util.cc, we increment the refcount of when constructing it. but at that moment, nobody really owns it. also, `CephContext` 's refcount is set to 1 in its constructor. so, we should not do this. otherwise, the created `CephContext` is leaked as LeakSanitizer rightly points out: ``` Indirect leak of 10880000 byte(s) in 1 object(s) allocated from: #0 0x5632320d27ed in operator new(unsigned long) (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_util+0x1917ed) (BuildId: ff1df1455bd07b651ad580584a17ea204afeb36e) ceph#1 0x7ff9d535b189 in __gnu_cxx::new_allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long, void const*) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/ext/new_allocator.h:127:27 ceph#2 0x7ff9d535a563 in std::allocator<ceph::logging::ConcreteEntry>::allocate(unsigned long) /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/allocator.h:185:32 ceph#3 0x7ff9d535a563 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::allocate(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2396:39 ceph#4 0x7ff9d535a2c0 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::initialize_buffer(unsigned long) /opt/ceph/include/boost/circular_buffer/base.hpp:2494:18 ceph#5 0x7ff9d5354192 in boost::circular_buffer<ceph::logging::ConcreteEntry, std::allocator<ceph::logging::ConcreteEntry> >::circular_buffer(unsigned long, std::allocator<ceph::logging::ConcreteEntry> const&) /opt/ceph/include/boost/circular_buffer/base.hpp:1039:9 ceph#6 0x7ff9d53471e4 in ceph::logging::Log::Log(ceph::logging::SubsystemMap const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/log/Log.cc:53:5 ceph#7 0x7ff9d461d96d in ceph::common::CephContext::CephContext(unsigned int, ceph::common::CephContext::create_options const&) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:729:16 ceph#8 0x7ff9d461c93b in ceph::common::CephContext::CephContext(unsigned int, code_environment_t, int) /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/ceph_context.cc:697:5 ceph#9 0x5632320d52e0 in util_collect_sys_info_Test::TestBody() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/common/test_util.cc:34:27 ceph#10 0x563232205c16 in void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2605:10 ceph#11 0x5632321c2742 in void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2641:14 ceph#12 0x5632321736dc in testing::Test::Run() /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:2680:5 ``` in this change, instead of using a raw pointer, let's use `boost::intrusive_ptr<CephContext>` to manage the lifecyle of `CephContext`, this also address the leakage reported by LeakSanitizer. the same applies to common/test_context.cc Signed-off-by: Kefu Chai <[email protected]>
1 parent dc53b76 commit e316c2e

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/test/common/test_context.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace std;
3030

3131
TEST(CephContext, do_command)
3232
{
33-
CephContext *cct = (new CephContext(CEPH_ENTITY_TYPE_CLIENT))->get();
33+
boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
3434

3535
cct->_conf->cluster = "ceph";
3636

@@ -89,12 +89,11 @@ TEST(CephContext, do_command)
8989
string s(out.c_str(), out.length());
9090
EXPECT_EQ("<config_diff_get><diff><key><default></default><override>" + value + "</override><final>value</final></key><rbd_default_features><default>61</default><final>61</final></rbd_default_features><rbd_qos_exclude_ops><default>0</default><final>0</final></rbd_qos_exclude_ops></diff></config_diff_get>", s);
9191
}
92-
cct->put();
9392
}
9493

9594
TEST(CephContext, experimental_features)
9695
{
97-
CephContext *cct = (new CephContext(CEPH_ENTITY_TYPE_CLIENT))->get();
96+
boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
9897

9998
cct->_conf->cluster = "ceph";
10099

src/test/common/test_util.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ TEST(util, collect_sys_info)
3131

3232
map<string, string> sys_info;
3333

34-
CephContext *cct = (new CephContext(CEPH_ENTITY_TYPE_CLIENT))->get();
35-
collect_sys_info(&sys_info, cct);
34+
boost::intrusive_ptr<CephContext> cct{new CephContext(CEPH_ENTITY_TYPE_CLIENT), false};
35+
36+
collect_sys_info(&sys_info, cct.get());
3637

3738
ASSERT_TRUE(sys_info.find("distro") != sys_info.end());
3839
ASSERT_TRUE(sys_info.find("distro_description") != sys_info.end());
39-
40-
cct->put();
4140
}
41+
4242
#endif

0 commit comments

Comments
 (0)