Skip to content

Commit 5cad84e

Browse files
test_late_joing_track_publication.cpp: reveals potential issue
1 parent 49b0077 commit 5cad84e

5 files changed

Lines changed: 974 additions & 10 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ lib/
3434
web/
3535
*trace.json
3636
compile_commands.json
37+
user_stories

‎src/room.cpp‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,24 @@ void Room::setDelegate(RoomDelegate* delegate) {
104104

105105
bool Room::Connect(const std::string& url, const std::string& token, const RoomOptions& options) {
106106
TRACE_EVENT0("livekit", "Room::Connect");
107-
108107
{
109108
const std::scoped_lock<std::mutex> g(lock_);
110109
if (connection_state_ != ConnectionState::Disconnected) {
111110
throw std::runtime_error("already connected");
112111
}
113112
connection_state_ = ConnectionState::Reconnecting;
114113
}
115-
auto fut = FfiClient::instance().connectAsync(url, token, options);
114+
115+
FfiClient::ListenerId listenerId = 0;
116116
try {
117+
// Install listener (Room is fully initialized) [2]
118+
listenerId = FfiClient::instance().AddListener([this](const proto::FfiEvent& e) { OnEvent(e); });
119+
{
120+
const std::scoped_lock<std::mutex> g(lock_);
121+
listener_id_ = listenerId;
122+
}
123+
124+
auto fut = FfiClient::instance().connectAsync(url, token, options); // [1]
117125
auto connectCb = fut.get(); // fut will throw if it fails to connect to the room
118126

119127
const auto& owned_room = connectCb.result().room();
@@ -141,6 +149,7 @@ bool Room::Connect(const std::string& url, const std::string& token, const RoomO
141149
std::make_unique<LocalParticipant>(std::move(participant_handle), pinfo.sid(), pinfo.name(), pinfo.identity(),
142150
pinfo.metadata(), std::move(attrs), kind, reason);
143151
}
152+
144153
// Setup remote participants
145154
std::unordered_map<std::string, std::shared_ptr<RemoteParticipant>> new_remote_participants;
146155
{
@@ -178,17 +187,20 @@ bool Room::Connect(const std::string& url, const std::string& token, const RoomO
178187
connection_state_ = ConnectionState::Connected;
179188
}
180189

181-
// Install listener (Room is fully initialized)
182-
auto listenerId = FfiClient::instance().AddListener([this](const proto::FfiEvent& e) { OnEvent(e); });
190+
return true;
191+
} catch (const std::exception& e) {
192+
int listener_to_remove = 0;
183193
{
184194
const std::scoped_lock<std::mutex> g(lock_);
185-
listener_id_ = listenerId;
195+
connection_state_ = ConnectionState::Disconnected;
196+
if (listener_id_ == listenerId) {
197+
listener_to_remove = listener_id_;
198+
listener_id_ = 0;
199+
}
200+
}
201+
if (listener_to_remove != 0) {
202+
FfiClient::instance().RemoveListener(listener_to_remove);
186203
}
187-
188-
return true;
189-
} catch (const std::exception& e) {
190-
// On error, set the connection_state_ to Disconnected
191-
connection_state_ = ConnectionState::Disconnected;
192204
LK_LOG_ERROR("Room::Connect failed: {}", e.what());
193205
return false;
194206
}

0 commit comments

Comments
 (0)