Skip to content

Commit 3394981

Browse files
authored
io_context should process all posted works before shutting down (#865)
* Removed the stop statement while shutting down io_context. * Added a new test `invocation_should_not_block_indefinitely_during_client_shutdown` which can be run indefinitely and observe that it is not being blocked in any run. An example way to reproduce the problem without this PR fix is : ``` for ((;;)) { /Users/ihsan/Desktop/work/src/hazelcast-cpp-client/cmake-build-arm-debug/hazelcast/test/src/client_test --gtest_filter=IssueTest.invocation_should_not_block_indefinitely_during_client_shutdown ; } ``` This run blocks after several runs in master branch but runs indefinite time with this PR fix with no problem.
1 parent 976f6ba commit 3394981

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

hazelcast/src/hazelcast/client/network.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ namespace hazelcast {
165165

166166
spi::impl::ClientExecutionServiceImpl::shutdown_thread_pool(executor_.get());
167167

168+
// release the guard so that the io thread can stop gracefully
168169
io_guard_.reset();
169-
io_context_->stop();
170-
boost::asio::use_service<boost::asio::detail::resolver_service<boost::asio::ip::tcp>>(
171-
*io_context_).shutdown();
172170
io_thread_.join();
173171

174172
connection_listeners_.clear();

hazelcast/test/src/HazelcastTests8.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,35 @@ namespace hazelcast {
14381438
ASSERT_THROW((map->get<int, int>(1).get()), exception::hazelcast_client_not_active);
14391439
}
14401440

1441+
TEST_F(IssueTest, invocation_should_not_block_indefinitely_during_client_shutdown) {
1442+
HazelcastServer server(*g_srvFactory);
1443+
auto hz = new_client().get();
1444+
auto map = hz.get_map("my_map").get();
1445+
1446+
ASSERT_NO_THROW(map->put(1, 5).get());
1447+
1448+
std::thread t([&] () {
1449+
hz.shutdown();
1450+
});
1451+
1452+
try {
1453+
int i = 0;
1454+
while (true) {
1455+
map->put(++i, 5).get();
1456+
}
1457+
} catch (std::exception &) {
1458+
// ignore
1459+
}
1460+
1461+
try {
1462+
map->put(10, 5).get();
1463+
} catch (std::exception &) {
1464+
// ignore
1465+
}
1466+
1467+
t.join();
1468+
}
1469+
14411470
TEST_F(IssueTest, testIssue753) {
14421471
HazelcastServer server(*g_srvFactory);
14431472

0 commit comments

Comments
 (0)