Skip to content

Keepalive thread throws errors even when the "keepalive" option is not set #344

@roerohan

Description

@roerohan

In AsyncLiveClient, the following code snippet checks if the keepalive option is set to true:

if (
counter % DEEPGRAM_INTERVAL == 0
and self.config.options.get("keepalive") == "true"
):
self.logger.verbose("Sending KeepAlive...")
await self.send(json.dumps({"type": "KeepAlive"}))

However, even if keepalive is not set in the options, or set to something other than true, the _keep_alive_thread is still created.

self._keep_alive_thread = asyncio.create_task(self._keep_alive())

Now, if the _keep_alive_thread exits for some reason, an exception is thrown that can not be handled.

This was causing a problem in v3.1.5 as the following would throw an error if the socket connection closed, even if _keep_alive was not set to true.

Even in the newer versions, if there is an exception raised from this _keep_alive_thread coroutine, there's no way to handle the exception and it will flood the user's logs with

Task exception was never retrieved
future: <Task finished name='Task-277' coro=<AsyncLiveClient._keep_alive() done, defined at /usr/local/lib/python3.12/dist-packages/deepgram/clients/live/v1/async_client.py:228> exception="...">
Traceback (most recent call last):
...

Also, if keepalive is not set to true in options, it's possible to avoid running a coroutine with while True. Is it possible for you to check the DeepgramClientOptions before creating the _keep_alive_thread coroutine, or is there something I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions