Repository: livekit/agents
File: livekit-agents/livekit/agents/cli/cli.py (Python)

Code before:
        c = AgentsConsole.get_instance()
        _configure_logger(c, log_level.value)

        loop = asyncio.get_event_loop()

        @server.once("worker_started")
        def _simulate_job() -> None:
            async def simulate_job() -> None:
                async with api.LiveKitAPI(url, api_key, api_secret) as lk_api:
                    room_request = api.ListRoomsRequest(names=[room])
                    active_room = await lk_api.room.list_rooms(room_request)

                    if not active_room.rooms:
                        room_info = await lk_api.room.create_room(api.CreateRoomRequest(name=room))
                    else:
                        room_info = active_room.rooms[0]

                await server.simulate_job(
                    room=room,
                    fake_job=False,
                    room_info=room_info,
                    agent_identity=participant_identity,
                )

            loop = asyncio.get_event_loop()
            asyncio.run_coroutine_threadsafe(simulate_job(), loop)

        try:
            loop.run_until_complete(server.run(devmode=True, unregistered=True))
        except _ExitCli:
            raise typer.Exit() from None
        except KeyboardInterrupt:
            logger.warning("exiting forcefully")
            os._exit(1)
        except CLIError as e:
            c.print(" ")
            c.print(f"[error]{e}")
            c.print(" ")
            raise typer.Exit(code=1) from None

    @app.command()
    def download_files() -> None:
        c = AgentsConsole.get_instance()
        c.enabled = True

        _configure_logger(c, logging.DEBUG)

        try:
            # import_data = get_import_data(path=path)
            # c.print(f"Importing from {import_data.module_data.extra_sys_path}")
            # c.print(" ")

Review comment (security):
```
     loop = asyncio.get_event_loop()
+    _task: asyncio.Task | None = None
+
     @server.once("worker_started")
     def _simulate_job() -> None:
+        nonlocal _task
+
         async def simulate_job() -> None:
             async with api.LiveKitAPI(url, api_key, api_secret) as lk_api:
                 room_request = api.ListRoomsRequest(names=[room])
                 active_room = await lk_api.room.list_rooms(room_request)
                 if not active_room.rooms:
                     room_info = await lk_api.room.create_room(api.CreateRoomRequest(name=room))
                 else:
                     room_info = active_room.rooms[0]
             await server.simulate_job(
                 room=room,
                 fake_job=False,
                 room_info=room_info,
                 agent_identity=participant_identity,
             )
-        loop = asyncio.get_event_loop()
-        asyncio.run_coroutine_threadsafe(simulate_job(), loop)
+        _task = asyncio.create_task(simulate_job())

```

Code after:
        c = AgentsConsole.get_instance()
        _configure_logger(c, log_level.value)

        loop = asyncio.get_event_loop()
        _task: asyncio.Task | None = None

        @server.once("worker_started")
        def _simulate_job() -> None:
            nonlocal _task

            async def simulate_job() -> None:
                async with api.LiveKitAPI(url, api_key, api_secret) as lk_api:
                    room_request = api.ListRoomsRequest(names=[room])
                    active_room = await lk_api.room.list_rooms(room_request)

                    if not active_room.rooms:
                        room_info = await lk_api.room.create_room(api.CreateRoomRequest(name=room))
                    else:
                        room_info = active_room.rooms[0]

                await server.simulate_job(
                    room=room,
                    fake_job=False,
                    room_info=room_info,
                    agent_identity=participant_identity,
                )

            _task = asyncio.create_task(simulate_job())

        try:
            loop.run_until_complete(server.run(devmode=True, unregistered=True))
        except _ExitCli:
            raise typer.Exit() from None
        except KeyboardInterrupt:
            logger.warning("exiting forcefully")
            os._exit(1)
        except CLIError as e:
            c.print(" ")
            c.print(f"[error]{e}")
            c.print(" ")
            raise typer.Exit(code=1) from None

    @app.command()
    def download_files() -> None:
        c = AgentsConsole.get_instance()
        c.enabled = True

        _configure_logger(c, logging.DEBUG)

        try:
            # import_data = get_import_data(path=path)