Improve error message for requests to closed client.

GitOrigin-RevId: 4870c3614ea52ecd0fd1124dfc517471b6d30b55
This commit is contained in:
levlam 2020-10-08 13:59:03 +03:00
parent b4f358de36
commit be374f38ac
2 changed files with 34 additions and 19 deletions

View File

@ -100,13 +100,13 @@ class ClientManager::Impl final {
auto guard = concurrent_scheduler_->get_main_guard();
for (size_t i = 0; i < requests_.size(); i++) {
auto &request = requests_[i];
auto it = tds_.find(request.client_id);
if (it == tds_.end()) {
if (request.client_id <= 0 || request.client_id > client_id_) {
receiver_->add_response(request.client_id, request.id,
td_api::make_object<td_api::error>(400, "Invalid TDLib instance specified"));
continue;
}
if (it->second.empty()) {
auto it = tds_.find(request.client_id);
if (it == tds_.end() || it->second.empty()) {
receiver_->add_response(request.client_id, request.id,
td_api::make_object<td_api::error>(500, "Request aborted"));
continue;
@ -334,6 +334,10 @@ class MultiImpl {
return id;
}
static bool is_valid_client_id(int32 client_id) {
return client_id > 0 && client_id < current_id_.load();
}
void send(ClientManager::ClientId client_id, ClientManager::RequestId request_id,
td_api::object_ptr<td_api::Function> &&request) {
auto guard = concurrent_scheduler_->get_send_guard();
@ -360,9 +364,10 @@ class MultiImpl {
thread scheduler_thread_;
ActorOwn<MultiTd> multi_td_;
static std::atomic<int32> current_id_;
static int32 create_id() {
static std::atomic<int32> current_id{1};
return current_id.fetch_add(1);
return current_id_.fetch_add(1);
}
void create(int32 td_id, unique_ptr<TdCallback> callback) {
@ -371,6 +376,8 @@ class MultiImpl {
}
};
std::atomic<int32> MultiImpl::current_id_{1};
class MultiImplPool {
public:
std::shared_ptr<MultiImpl> get() {
@ -410,13 +417,13 @@ class ClientManager::Impl final {
void send(ClientId client_id, RequestId request_id, td_api::object_ptr<td_api::Function> &&request) {
auto lock = impls_mutex_.lock_read().move_as_ok();
auto it = impls_.find(client_id);
if (it == impls_.end()) {
if (!MultiImpl::is_valid_client_id(client_id)) {
receiver_->add_response(client_id, request_id,
td_api::make_object<td_api::error>(400, "Invalid TDLib instance specified"));
return;
}
if (it->second.is_closed) {
auto it = impls_.find(client_id);
if (it == impls_.end() || it->second.is_closed) {
receiver_->add_response(client_id, request_id, td_api::make_object<td_api::error>(500, "Request aborted"));
return;
}

View File

@ -955,6 +955,7 @@ TEST(Client, Manager) {
auto event = client.receive(10);
if (event.client_id == 0 || event.client_id == -1) {
ASSERT_EQ(td::td_api::error::ID, event.object->get_id());
ASSERT_EQ(400, static_cast<td::td_api::error &>(*event.object).code_);
continue;
}
if (event.request_id == 3) {
@ -987,12 +988,16 @@ TEST(Client, Close) {
can_stop_receive = true;
});
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
td::thread receive_thread([&] {
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
while (true) {
auto response = client.receive(100.0);
if (stop_send && response.object == nullptr) {
return;
if (response.object == nullptr) {
if (!stop_send) {
stop_send = true;
} else {
return;
}
}
if (response.id > 0) {
if (!stop_send && response.object->get_id() == td::td_api::error::ID &&
@ -1046,19 +1051,22 @@ TEST(Client, ManagerClose) {
can_stop_receive = true;
});
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
td::thread receive_thread([&] {
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
bool can_stop_send = false;
while (true) {
auto response = client_manager.receive(100.0);
if (stop_send && response.object == nullptr) {
return;
if (response.object == nullptr) {
if (!stop_send) {
can_stop_send = true;
} else {
return;
}
}
if (can_stop_send && max_continue_send-- <= 0) {
stop_send = true;
}
if (response.request_id > 0) {
if (!stop_send && response.object->get_id() == td::td_api::error::ID &&
static_cast<td::td_api::error &>(*response.object).code_ == 400 &&
td::Random::fast(0, max_continue_send) == 0) {
stop_send = true;
}
receive_count++;
{
std::unique_lock<std::mutex> guard(request_ids_mutex);