Skip to content
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

Room._listen_task is not cancelled when server closes room #199

Closed
gigaverse-oz opened this issue May 5, 2024 · 3 comments
Closed

Room._listen_task is not cancelled when server closes room #199

gigaverse-oz opened this issue May 5, 2024 · 3 comments
Labels

Comments

@gigaverse-oz
Copy link

gigaverse-oz commented May 5, 2024

Issue Description

Overview

When the Livekit Server terminates a room and removes a Python client, the task responsible for listening (self._listen_task) does not properly conclude or cancel.

Background

We have set up a Python participant to join a specific room and listen to a particular track. If the track is no longer published, the Python participant leaves the room, completes all asynchronous tasks, and then the process exits. However, if the room is deleted, the Python participant struggles to disconnect and the _listen_task remains active.

Diagnostics

The Room.disconnect() method is designed to clear the task queue and wait for self._task to finish. But, if the Livekit Server deletes the room, the Room.disconnect() cannot complete as it fails the self.isconnected() check, resulting in self._task remaining in a running state.

if not self.isconnected():
            return

View the code segment here: Room.py Line 221

Requested Solution

We suggest introducing an aclose() function to manage disconnections and clean up any remaining asynchronous tasks. Currently, we have implemented the following in the object that utilizes the Room object:

    async def aclose(self):
        if self.is_closed():
            return
        try:
            for task in self._tasks:
                task.cancel()
            await asyncio.gather(*self._tasks, return_exceptions=True)
            await self._room.disconnect()
        except Exception as err:
            logger.error(f"Error shutting down transcript agent: {str(err)}")
        finally:
            if self._room._task:
                self._room._task.cancel()
                logger.warning("Room disconnect method failed to close _listen_task")
            self._closed = True
@theomonnom
Copy link
Member

theomonnom commented May 5, 2024

The correct implementation would be to always call self._room.disconnect like you do, the difference is that we're now awaiting the main task in #200. There is no need to cancel the task because the Rust SDK should send EOS which will break the infinite loop

@theomonnom
Copy link
Member

theomonnom commented May 5, 2024

I'm going to verify that this line is correctly ran if the disconnect comes from the server before merging the above PR

@gigaverse-oz
Copy link
Author

Thanks for the quick reply and fix. Will also verify once merged :)

@lukasIO lukasIO added the Python label May 8, 2024 — with Linear
@linear linear bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants