Compare commits

..

2 Commits

Author SHA1 Message Date
dijunkun
f6eda34dbd [fix] fix dead lock during connecting 2025-11-28 10:02:57 +08:00
dijunkun
5d9a0a3ea5 [fix] fix dead lock during peer init 2025-11-28 09:32:21 +08:00

View File

@@ -79,24 +79,38 @@ int Render::RemoteWindow() {
enter_pressed) { enter_pressed) {
connect_button_pressed_ = true; connect_button_pressed_ = true;
bool found = false; bool found = false;
std::string target_remote_id;
std::string target_password;
bool should_connect = false;
bool already_connected = false;
for (auto& [id, props] : recent_connections_) { for (auto& [id, props] : recent_connections_) {
if (id.find(remote_id) != std::string::npos) { if (id.find(remote_id) != std::string::npos) {
found = true; found = true;
target_remote_id = props.remote_id;
target_password = props.password;
{
std::shared_lock lock(client_properties_mutex_); std::shared_lock lock(client_properties_mutex_);
if (client_properties_.find(remote_id) != if (client_properties_.find(remote_id) !=
client_properties_.end()) { client_properties_.end()) {
if (!client_properties_[remote_id]->connection_established_) { if (!client_properties_[remote_id]->connection_established_) {
ConnectTo(props.remote_id, props.password.c_str(), false); should_connect = true;
} else { } else {
// todo: show warning message already_connected = true;
LOG_INFO("Already connected to [{}]", remote_id);
} }
} else { } else {
ConnectTo(props.remote_id, props.password.c_str(), false); should_connect = true;
}
} }
} }
if (should_connect) {
ConnectTo(target_remote_id, target_password.c_str(), false);
} else if (already_connected) {
LOG_INFO("Already connected to [{}]", remote_id);
}
break;
}
}
if (!found) { if (!found) {
ConnectTo(remote_id, "", false); ConnectTo(remote_id, "", false);
} }
@@ -153,6 +167,10 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
shared_lock.unlock(); shared_lock.unlock();
if (!exists) { if (!exists) {
PeerPtr* peer_to_init = nullptr;
std::string local_id;
{
std::unique_lock unique_lock(client_properties_mutex_); std::unique_lock unique_lock(client_properties_mutex_);
if (client_properties_.find(remote_id) == client_properties_.end()) { if (client_properties_.find(remote_id) == client_properties_.end()) {
client_properties_[remote_id] = client_properties_[remote_id] =
@@ -175,17 +193,18 @@ int Render::ConnectTo(const std::string& remote_id, const char* password,
AddAudioStream(props->peer_, props->audio_label_.c_str()); AddAudioStream(props->peer_, props->audio_label_.c_str());
AddDataStream(props->peer_, props->data_label_.c_str()); AddDataStream(props->peer_, props->data_label_.c_str());
if (props->peer_) { props->connection_status_ = ConnectionStatus::Connecting;
LOG_INFO("[{}] Create peer instance successful", props->local_id_);
Init(props->peer_); peer_to_init = props->peer_;
LOG_INFO("[{}] Peer init finish", props->local_id_); local_id = props->local_id_;
} else { }
LOG_INFO("Create peer [{}] instance failed", props->local_id_);
} }
props->connection_status_ = ConnectionStatus::Connecting; if (peer_to_init) {
LOG_INFO("[{}] Create peer instance successful", local_id);
Init(peer_to_init);
LOG_INFO("[{}] Peer init finish", local_id);
} }
unique_lock.unlock();
} }
int ret = -1; int ret = -1;