Skip to content

Commit 1b857a7

Browse files
committed
fix: do not block identify on SSE client shutdown completion
1 parent 8e1af76 commit 1b857a7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

libs/client-sdk/src/client_impl.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,11 @@ std::future<bool> ClientImpl::IdentifyAsync(Context context) {
149149
}
150150

151151
void ClientImpl::RestartDataSource() {
152-
auto start_op = [this]() {
153-
data_source_ = data_source_factory_();
154-
data_source_->Start();
155-
};
156-
if (!data_source_) {
157-
return start_op();
152+
if (data_source_) {
153+
data_source_->ShutdownAsync(nullptr);
158154
}
159-
data_source_->ShutdownAsync(start_op);
155+
data_source_ = data_source_factory_();
156+
data_source_->Start();
160157
}
161158

162159
std::future<bool> ClientImpl::StartAsyncInternal(
@@ -170,7 +167,7 @@ std::future<bool> ClientImpl::StartAsyncInternal(
170167
if (auto const state = status.State(); IsInitialized(state)) {
171168
init_promise->set_value(result(status.State()));
172169
return true; /* delete this change listener since the desired
173-
state was reached */
170+
state was reached */
174171
}
175172
return false; /* keep the change listener */
176173
});

libs/server-sent-events/src/client.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ auto const kDefaultInitialReconnectDelay = std::chrono::seconds(1);
4444
// Maximum duration between backoff attempts.
4545
auto const kDefaultMaxBackoffDelay = std::chrono::seconds(30);
4646

47+
// When we shut down the SSL stream using async_shutdown operation, it
48+
// appears that the completion handler isn't invoked for about 5 minutes. Either
49+
// we or the server are misbehaving here. In any case, there is no need to wait
50+
// 5 minutes, we should give a couple seconds to receive a valid response or
51+
// else stop waiting.
52+
auto const kShutdownTimeout = std::chrono::seconds(5);
53+
4754
static boost::optional<net::ssl::context&> ToOptRef(
4855
std::optional<net::ssl::context>& maybe_val) {
4956
if (maybe_val) {
@@ -341,15 +348,17 @@ class FoxyClient : public Client,
341348
void do_shutdown(std::function<void()> completion) {
342349
shutting_down_ = true;
343350
backoff_timer_.cancel();
351+
session_->opts.timeout = kShutdownTimeout;
352+
if (session_->stream.is_ssl()) {
353+
session_->stream.ssl().next_layer().cancel();
354+
}
344355
session_->async_shutdown(beast::bind_front_handler(
345-
&FoxyClient::on_shutdown, std::move(completion)));
356+
&FoxyClient::on_shutdown, shared_from_this(),
357+
std::move(completion)));
346358
}
347359

348-
static void on_shutdown(std::function<void()> completion,
349-
boost::system::error_code ec) {
350-
// Because do_shutdown doesn't use shared_from_this() when initiating
351-
// the async_shutdown op, the client may already be destroyed - hence
352-
// this static method.
360+
void on_shutdown(std::function<void()> completion,
361+
boost::system::error_code ec) {
353362
boost::ignore_unused(ec);
354363
if (completion) {
355364
completion();

0 commit comments

Comments
 (0)