deepgram-python-sdk icon indicating copy to clipboard operation
deepgram-python-sdk copied to clipboard

Error upon socket connection closure

Open roerohan opened this issue 2 years ago • 10 comments

Description

I'm using v2.11.0 of your SDK and I often run into this error in live transcriptions.

future: <Task finished name='Task-311517' coro=<LiveTranscription._start() done, defined at /usr/local/lib/python3.11/dist-packages/deepgram/transcription.py:178> exception=ConnectionClosedError(Close(code=1011, reason='Deepgram did not provide a response message within the timeout window. See https://dpgr.am/net0000'), Close(code=1011, reason='Deepgram did not provide a response message within the timeout window. See https://dpgr.am/net0000'), True)>
 
Traceback (most recent call last):
 File "/usr/local/lib/python3.11/dist-packages/deepgram/transcription.py", line 222, in _start
   await self._socket.send(body)
 File "/usr/local/lib/python3.11/dist-packages/websockets/legacy/protocol.py", line 635, in send
   await self.ensure_open()
 File "/usr/local/lib/python3.11/dist-packages/websockets/legacy/protocol.py", line 939, in ensure_open
   raise self.connection_closed_exc()

After this, none of the send() method calls on an instance of the LiveTranscription object send any data over the socket.

Debugging

It seems like the issue is here: https://github.com/deepgram/deepgram-python-sdk/blob/2135dc5b352f1d7832975b8c726392f3a2b67496/deepgram/transcription.py#L198

It appears that the code only checks if the socket is closed when there is a message timeout while waiting in the queue. However, if I insert a lot of messages in the queue (by calling the send method) when self._socket.closed is False, and the socket closes later, the code will try to send the message over a closed socket at some point, resulting in the above error.

Since there is no try except block, the error also causes the while loop to exit, and therefore I'm not able to send any more messages using that object.

roerohan avatar Nov 16 '23 13:11 roerohan

Hi @roerohan The Live client is in the process of getting a major overhaul. Will ping the community to take a look when the PR becomes available.

davidvonthenen avatar Nov 19 '23 18:11 davidvonthenen

Alright!

I also wanted to bring to your notice that there's no way to handle this error currently since the async task created in the following code snippet isn't returned outside the SDK, and there's no way to catch the exception since I don't have access to this task

https://github.com/deepgram/deepgram-python-sdk/blob/2135dc5b352f1d7832975b8c726392f3a2b67496/deepgram/transcription.py#L175

For now, the only way I found to fix it was a hacky workaround, by using the _socket object directly from the LiveTranscription class.

roerohan avatar Nov 21 '23 05:11 roerohan

A new release was just posted which should address this problem, please take a look: https://github.com/deepgram/deepgram-python-sdk/releases/tag/v3.0.0-alpha.1

davidvonthenen avatar Nov 22 '23 21:11 davidvonthenen

Latest is alpha.4 with another release due out today

davidvonthenen avatar Dec 01 '23 17:12 davidvonthenen

@roerohan alpha.5 is out and that commit is most likely going to be the beta.1 release. Have your y been able to try with this latest update?

davidvonthenen avatar Dec 03 '23 04:12 davidvonthenen

Hi @dvonthenen I'm currently using a workaround where I'm sending the socket messages directly using the _socket object. I'll try out the newer version as soon as I get some time.

roerohan avatar Dec 03 '23 15:12 roerohan

cool. If you run into any problems, please let me know.

davidvonthenen avatar Dec 08 '23 17:12 davidvonthenen

Re-opened to take a look at when I have time. I am unfamiliar with v2 version, so this might take some time.

davidvonthenen avatar Feb 29 '24 14:02 davidvonthenen

@SandraRodgers said she would take a look at this issue and the other one since she has more knowledge about the v2 client.

davidvonthenen avatar Mar 05 '24 15:03 davidvonthenen

i have an issue not sure if it is a similar one: Could not open socket: type object 'LiveTranscriptionEvents' has no attribute 'Unhandled'

jz155 avatar Apr 26 '24 02:04 jz155