-
Notifications
You must be signed in to change notification settings - Fork 104
Bugfix: process messages after websocket closes #123
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
Bugfix: process messages after websocket closes #123
Conversation
…mpleted before the message queue was cleared
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.
Added a comment
try: | ||
body = await self._socket.recv() | ||
self._queue.put_nowait((True, body)) | ||
except Exception as exc: |
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.
Should this still catch Exception after the new exception catch just in case some other error happens?
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.
I personally think we should not catch all exceptions. My guess is that the original code caught all exceptions specifically so that the ConnectionClosedOk
exception was handled correctly. Below is more info on my opinion:
This PR also makes a small change to the exception handling. Rather than catching all websocket closures (and other any exceptions!) by using except Exception as exc, I replaced the error handling with a more explicit catch: except websockets.exceptions.ConnectionClosedOK:. This change will result in errors that are not ConnectionClosedOK bubbling up to the user. I think this is appropriate because any other websocket closures are errors that the user may want to handle or be aware of. I was unable to produce any situations where a different exception was thrown, but we may get messages from customers who find edge cases.
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.
Could we catch those other errors and log them rather than throwing them instead?
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.
@DamienDeepgram what makes you want to catch and log these specific errors rather than throwing them?
The new try/except clauses catches the 1011 "error" and closes the websocket cleaning when that response code is received. Deepgram returns the 1011 "error" as a notification to the user that Deepgram closed the socket due to inactivity, which the previous version of the Python SDK considered a normal piece of the workflow (i.e. not an exceptional case).
The new code handles the 1011 "error" in the same way, but raises other errors to the user. I think that makes sense because those other errors are real errors (whereas the 1011 error is a simply closure due to inactivity). But I'm curious to hear what you're thinking.
@DamienDeepgram @jjmaldonis are you both still working on this open PR? |
Sorry I need to get better at filtering these github alerts into my inbox and missed this. I think if the desired approach here is to throw the errors then this is fine as is |
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.
LGTM
@DamienDeepgram just to confirm was this tested locally? |
I did not test this locally since it is an error handling code change no logic has changed just that we only catch these errors now not all errors websockets.exceptions.ConnectionClosedOK: |
This PR fixes a bug where the streaming websocket could be closed and marked as completed before the socket's messages were processed.
Below is the old code:
This code resulted in a race condition where the last message of the websocket would not always get processed before setting
self.done = True
. This code would put a new message in the queue for processing and then execute the next iteration of the While loop. The next iteration would raise an Exception and therefore setself.done = True
. Settingself.done = True
resulted in immediate completion of the websocket and the data was returned to the user before the previous iteration'sbody
was processed.The changes in this PR fix the race condition.
This PR also makes a small change to the exception handling. Rather than catching all websocket closures (and other any exceptions!) by using
except Exception as exc
, I replaced the error handling with a more explicit catch:except websockets.exceptions.ConnectionClosedOK:
. This change will result in errors that are notConnectionClosedOK
bubbling up to the user. I think this is appropriate because any other websocket closures are errors that the user may want to handle or be aware of. I was unable to produce any situations where a different exception was thrown, but we may get messages from customers who find edge cases.The code to reproduce the race condition (before this PR) is below:
+
OR