Skip to content

Commit 976e056

Browse files
authored
HazelcastClient destruction should guarantee that the lifecycle service shutdown is completed. (#316)
1 parent a3f9495 commit 976e056

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

hazelcast/include/hazelcast/client/HazelcastClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ namespace hazelcast {
713713

714714
ClientConfig clientConfig;
715715
ClientProperties clientProperties;
716+
util::CountDownLatch shutdownLatch;
716717
spi::ClientContext clientContext;
717718
spi::LifecycleService lifecycleService;
718719
serialization::pimpl::SerializationService serializationService;

hazelcast/include/hazelcast/client/spi/LifecycleService.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ namespace hazelcast {
4646
class HAZELCAST_API LifecycleService {
4747
public:
4848

49-
LifecycleService(ClientContext &clientContext, const ClientConfig &clientConfig);
49+
LifecycleService(ClientContext &clientContext, const ClientConfig &clientConfig,
50+
util::CountDownLatch &shutdownLatch);
5051

5152
virtual ~LifecycleService();
5253

@@ -68,7 +69,7 @@ namespace hazelcast {
6869
std::set<LifecycleListener *> listeners;
6970
util::Mutex listenerLock;
7071
util::AtomicBoolean active;
71-
util::CountDownLatch shutdownLatch;
72+
util::CountDownLatch &shutdownLatch;
7273
};
7374

7475
}

hazelcast/src/hazelcast/client/HazelcastClient.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ namespace hazelcast {
3535
HazelcastClient::HazelcastClient(ClientConfig &config)
3636
: clientConfig(config)
3737
, clientProperties(config)
38+
, shutdownLatch(1)
3839
, clientContext(*this)
39-
, lifecycleService(clientContext, clientConfig)
40+
, lifecycleService(clientContext, clientConfig, shutdownLatch)
4041
, serializationService(config.getSerializationConfig())
4142
, connectionManager(new connection::ConnectionManager(clientContext, clientConfig.isSmart()))
4243
, nearCacheManager(serializationService)
@@ -65,6 +66,12 @@ namespace hazelcast {
6566

6667
HazelcastClient::~HazelcastClient() {
6768
lifecycleService.shutdown();
69+
/**
70+
* We can not depend on the destruction order of the variables. lifecycleService may be destructed later
71+
* than the clientContext which is accessed by different service threads, hence we need to explicitly wait
72+
* for shutdown completion.
73+
*/
74+
shutdownLatch.await();
6875
}
6976

7077

hazelcast/src/hazelcast/client/spi/LifecycleService.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ namespace hazelcast {
3131
namespace client {
3232
namespace spi {
3333

34-
LifecycleService::LifecycleService(ClientContext &clientContext, const ClientConfig &clientConfig)
34+
LifecycleService::LifecycleService(ClientContext &clientContext, const ClientConfig &clientConfig,
35+
util::CountDownLatch &shutdownLatch)
3536
:clientContext(clientContext)
36-
, active(true), shutdownLatch(1) {
37+
, active(true), shutdownLatch(shutdownLatch) {
3738
std::set<LifecycleListener *> const &lifecycleListeners = clientConfig.getLifecycleListeners();
3839
listeners.insert(lifecycleListeners.begin(), lifecycleListeners.end());
39-
4040
}
4141

4242
bool LifecycleService::start() {

0 commit comments

Comments
 (0)