Improve Td closing.

GitOrigin-RevId: 9111edb11ffe14ee9fa80942a4201fec94641b56
This commit is contained in:
levlam 2019-07-24 00:15:49 +03:00
parent ae3c1208ea
commit bcacb2c31c
2 changed files with 17 additions and 14 deletions

View File

@ -3864,6 +3864,11 @@ void Td::start_up() {
LOG_IF(FATAL, symbol != c) << "TDLib requires little-endian platform"; LOG_IF(FATAL, symbol != c) << "TDLib requires little-endian platform";
} }
VLOG(td_init) << "Create Global";
set_context(std::make_shared<Global>());
inc_request_actor_refcnt(); // guard
inc_actor_refcnt(); // guard
alarm_timeout_.set_callback(on_alarm_timeout_callback); alarm_timeout_.set_callback(on_alarm_timeout_callback);
alarm_timeout_.set_callback_data(static_cast<void *>(this)); alarm_timeout_.set_callback_data(static_cast<void *>(this));
@ -3893,6 +3898,7 @@ void Td::hangup_shared() {
} }
void Td::hangup() { void Td::hangup() {
LOG(INFO) << "Receive Td::hangup";
close(); close();
dec_stop_cnt(); dec_stop_cnt();
} }
@ -4136,25 +4142,25 @@ void Td::close_impl(bool destroy_flag) {
if (close_flag_) { if (close_flag_) {
return; return;
} }
if (state_ == State::WaitParameters) {
LOG(WARNING) << (destroy_flag ? "Destroy" : "Close") << " Td in state " << static_cast<int32>(state_);
if (state_ == State::WaitParameters || state_ == State::Decrypt) {
clear_requests(); clear_requests();
state_ = State::Close; if (destroy_flag && state_ == State::Decrypt) {
return on_closed();
}
if (state_ == State::Decrypt) {
clear_requests();
if (destroy_flag) {
TdDb::destroy(parameters_).ignore(); TdDb::destroy(parameters_).ignore();
} }
state_ = State::Close; state_ = State::Close;
close_flag_ = 4; close_flag_ = 4;
return dec_actor_refcnt(); G()->set_close_flag();
request_actors_.clear();
return send_closure_later(actor_id(this), &Td::dec_request_actor_refcnt); // remove guard
} }
state_ = State::Close; state_ = State::Close;
close_flag_ = 1; close_flag_ = 1;
G()->set_close_flag(); G()->set_close_flag();
send_closure(auth_manager_actor_, &AuthManager::on_closing, destroy_flag); send_closure(auth_manager_actor_, &AuthManager::on_closing, destroy_flag);
LOG(WARNING) << "Close " << tag("destroy", destroy_flag);
// wait till all request_actors will stop. // wait till all request_actors will stop.
request_actors_.clear(); request_actors_.clear();
@ -4781,11 +4787,6 @@ Status Td::set_parameters(td_api::object_ptr<td_api::tdlibParameters> parameters
TRY_RESULT(encryption_info, TdDb::check_encryption(parameters_)); TRY_RESULT(encryption_info, TdDb::check_encryption(parameters_));
is_database_encrypted_ = encryption_info.is_encrypted; is_database_encrypted_ = encryption_info.is_encrypted;
VLOG(td_init) << "Create Global";
set_context(std::make_shared<Global>());
inc_request_actor_refcnt(); // guard
inc_actor_refcnt(); // guard
VLOG(td_init) << "Create MtprotoHeader::Options"; VLOG(td_init) << "Create MtprotoHeader::Options";
options_.api_id = parameters->api_id_; options_.api_id = parameters->api_id_;
options_.system_language_code = trim(parameters->system_language_code_); options_.system_language_code = trim(parameters->system_language_code_);

View File

@ -829,6 +829,8 @@ class CliClient final : public Actor {
bad_parameters->api_id_ = api_id_; bad_parameters->api_id_ = api_id_;
bad_parameters->api_hash_ = api_hash_; bad_parameters->api_hash_ = api_hash_;
send_request(td_api::make_object<td_api::setTdlibParameters>(std::move(bad_parameters))); send_request(td_api::make_object<td_api::setTdlibParameters>(std::move(bad_parameters)));
send_closure_later(actor_id(this), &CliClient::create_td, Slice("ClientActor3"));
} }
} }