-
Notifications
You must be signed in to change notification settings - Fork 27
Room disconnect issue fix #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alan-george-lk
wants to merge
6
commits into
main
Choose a base branch
from
feature/room_disconnect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59bc2fd
Initial attempt at fixing room disconnect issue report
alan-george-lk 377c8ea
Maybe fix
alan-george-lk 12946e2
See if we can catch Rpc failure
alan-george-lk dd91cd9
Drain RPC invocations on kEos + add lifecycle tracing
alan-george-lk 6159bbc
Cleanup, shutdown local participants before
alan-george-lk e3e7e15
Additional cleanup
alan-george-lk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,32 +76,17 @@ void readyForRoomEvent(std::uint64_t room_handle) { | |
| Room::Room() : subscription_thread_dispatcher_(std::make_unique<SubscriptionThreadDispatcher>()) {} | ||
|
|
||
| Room::~Room() { | ||
| if (subscription_thread_dispatcher_) { | ||
| subscription_thread_dispatcher_->stopAll(); | ||
| } | ||
|
|
||
| int listener_to_remove = 0; | ||
| std::unique_ptr<LocalParticipant> local_participant_to_cleanup; | ||
| { | ||
| const std::scoped_lock<std::mutex> g(lock_); | ||
| listener_to_remove = listener_id_; | ||
| listener_id_ = 0; | ||
| // Move local participant out for cleanup outside the lock | ||
| local_participant_to_cleanup = std::move(local_participant_); | ||
| } | ||
|
|
||
| // Shutdown local participant (unregisters RPC handlers, etc.) before | ||
| // removing the listener. This prevents in-flight RPC responses from | ||
| // trying to use destroyed handles. | ||
| if (local_participant_to_cleanup) { | ||
| local_participant_to_cleanup->shutdown(); | ||
| } | ||
|
|
||
| if (listener_to_remove != 0) { | ||
| FfiClient::instance().removeListener(listener_to_remove); | ||
| // disconnect() is used for all tear down cases: it handles the | ||
| // already-disconnected case (returns false, no-op), the partial/Reconnecting | ||
| // case, and the FFI-failure case (local teardown still runs). Nothing else | ||
| // needs to live in the destructor. | ||
| try { | ||
| (void)disconnect(); // Don't need return value | ||
| } catch (const std::exception& e) { | ||
| LK_LOG_ERROR("Room::~Room: graceful disconnect failed: {}", e.what()); | ||
| } catch (...) { | ||
| LK_LOG_ERROR("Room::~Room: graceful disconnect failed: unknown exception"); | ||
| } | ||
|
|
||
| // local_participant_to_cleanup is destroyed here after listener is removed | ||
| } | ||
|
|
||
| void Room::setDelegate(RoomDelegate* delegate) { | ||
|
|
@@ -234,6 +219,103 @@ bool Room::Connect(const std::string& url, const std::string& token, const RoomO | |
| return connect(url, token, options); | ||
| } | ||
|
|
||
| bool Room::disconnect(DisconnectReason reason) { | ||
| TRACE_EVENT0("livekit", "Room::disconnect"); | ||
|
|
||
| // Canonical teardown path. Move all owned state out under the lock, then | ||
| // operate on it outside the lock. The destructor (and any caller) gets | ||
| // the same behavior: once this returns, the Room is fully torn down. | ||
| // | ||
| // Return value: | ||
| // true - we owned live state and tore it down (FFI disconnect succeeded) | ||
| // false - either already disconnected (no-op) or FFI disconnect failed. | ||
| // In both false cases local-side teardown still completed. | ||
|
|
||
| std::shared_ptr<FfiHandle> handle; | ||
| RoomDelegate* delegate_snapshot = nullptr; | ||
| std::unique_ptr<LocalParticipant> local_participant_to_cleanup; | ||
| std::unordered_map<std::string, std::shared_ptr<RemoteParticipant>> remote_participants_to_clear; | ||
| std::unique_ptr<E2EEManager> e2ee_manager_to_clear; | ||
| std::unordered_map<std::string, std::shared_ptr<TextStreamReader>> text_stream_readers_to_clear; | ||
| std::unordered_map<std::string, std::shared_ptr<ByteStreamReader>> byte_stream_readers_to_clear; | ||
| int listener_to_remove = 0; | ||
|
|
||
| { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussed in slack -- if the sdk is killed without calling disconnect the sfu drops the connection in 15 seconds, so this is really best effort. |
||
| const std::scoped_lock<std::mutex> g(lock_); | ||
| if (connection_state_ == ConnectionState::Disconnected) { | ||
| // Already torn down (or never connected). Nothing to do. | ||
| return false; | ||
| } | ||
| handle = room_handle_; | ||
| delegate_snapshot = delegate_; | ||
| // Take ownership of everything under the lock so the kEos handler (which | ||
| // also tries to move it out) loses any race here — only one teardown | ||
| // path operates on this state. | ||
| local_participant_to_cleanup = std::move(local_participant_); | ||
| remote_participants_to_clear = std::move(remote_participants_); | ||
| e2ee_manager_to_clear = std::move(e2ee_manager_); | ||
| text_stream_readers_to_clear = std::move(text_stream_readers_); | ||
| byte_stream_readers_to_clear = std::move(byte_stream_readers_); | ||
| listener_to_remove = listener_id_; | ||
| listener_id_ = 0; | ||
| room_handle_.reset(); | ||
| // Flip state immediately so the in-flight Disconnected room-event we'll | ||
| // get back doesn't double-fire onDisconnected. Mirrors Python's | ||
| // Room.disconnect(), which also flips state before sending the request. | ||
| connection_state_ = ConnectionState::Disconnected; | ||
| } | ||
|
|
||
| // Drain in-flight RPC handlers BEFORE telling Rust to tear down the room. | ||
| // Mirrors client-sdk-python's Room.disconnect() ordering: once the FFI | ||
| // dispatches the Disconnect, Rust starts invalidating participant handles | ||
| // in its table, and any listener-thread RPC handler still mid-flight | ||
| // would race with that invalidation and send to a dead handle → | ||
| // INVALID_HANDLE → terminate. | ||
| if (local_participant_to_cleanup) { | ||
| local_participant_to_cleanup->shutdown(); | ||
| } | ||
|
|
||
| // Tell the FFI to close the room and wait for the callback. If this fails | ||
| // we still complete local-side teardown below — releasing the listener, | ||
| // dropping handles, and notifying the delegate — so the Room is fully | ||
| // cleaned up regardless of whether the FFI round-trip succeeded. | ||
| bool ffi_ok = true; | ||
| if (handle) { | ||
| try { | ||
| FfiClient::instance().disconnectAsync(handle->get(), reason).get(); | ||
| } catch (const std::exception& e) { | ||
| LK_LOG_ERROR("Room::disconnect: FFI disconnect failed (continuing local teardown): {}", e.what()); | ||
| ffi_ok = false; | ||
| } | ||
| } | ||
|
|
||
| // Stop dispatcher so no track callbacks fire mid-teardown. | ||
| if (subscription_thread_dispatcher_) { | ||
| subscription_thread_dispatcher_->stopAll(); | ||
| } | ||
|
|
||
| if (listener_to_remove != 0) { | ||
| FfiClient::instance().removeListener(listener_to_remove); | ||
| } | ||
|
|
||
| // Fire onDisconnected exactly once, with the reason the caller passed. | ||
| if (delegate_snapshot) { | ||
| DisconnectedEvent ev; | ||
| ev.reason = reason; | ||
| try { | ||
| delegate_snapshot->onDisconnected(*this, ev); | ||
| } catch (const std::exception& e) { | ||
| LK_LOG_ERROR("Room::disconnect: onDisconnected threw: {}", e.what()); | ||
| } catch (...) { | ||
| LK_LOG_ERROR("Room::disconnect: onDisconnected threw: unknown exception"); | ||
| } | ||
| } | ||
|
|
||
| // Moved-out state (local participant, remote participants, e2ee manager, | ||
| // stream readers) destructs here, releasing FFI handles. | ||
| return ffi_ok; | ||
| } | ||
|
|
||
| RoomInfoData Room::roomInfo() const { | ||
| const std::scoped_lock<std::mutex> g(lock_); | ||
| return room_info_; | ||
|
|
@@ -1139,6 +1221,17 @@ void Room::onEvent(const FfiEvent& event) { | |
| break; | ||
| } | ||
| case proto::RoomEvent::kDisconnected: { | ||
| // If disconnect() was driven from our side, it already flipped state | ||
| // to Disconnected and fired the delegate; skip the duplicate here. | ||
| bool already_disconnected = false; | ||
| { | ||
| const std::scoped_lock<std::mutex> guard(lock_); | ||
| already_disconnected = (connection_state_ == ConnectionState::Disconnected); | ||
| connection_state_ = ConnectionState::Disconnected; | ||
| } | ||
| if (already_disconnected) { | ||
| break; | ||
| } | ||
| DisconnectedEvent ev; | ||
| ev.reason = toDisconnectReason(re.disconnected().reason()); | ||
| if (delegate_snapshot) { | ||
|
|
@@ -1193,6 +1286,14 @@ void Room::onEvent(const FfiEvent& event) { | |
| old_byte_readers = std::move(byte_stream_readers_); | ||
| } | ||
|
|
||
| // Drain in-flight RPC invocations before destroying the local | ||
| // participant's FFI handle. Mirrors the ordering in disconnect(); | ||
| // without this, a listener-thread RPC handler can race with handle | ||
| // disposal and send to a dead handle → INVALID_HANDLE → terminate. | ||
| if (old_local_participant) { | ||
| old_local_participant->shutdown(); | ||
| } | ||
|
|
||
| // Remove listener outside lock | ||
| if (listener_to_remove != 0) { | ||
| FfiClient::instance().removeListener(listener_to_remove); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LK_LOG_ERROR ?