Skip to content

TTSSession raises a send TTSError a second time on leaving the with block, after the block already caught it #317

Backlog

Description

@gshost1

Steps to reproduce

The server closes the socket while the client sends (for example a 3010 "input rate exceeded" error frame, or a bare 1012 service restart). generate() raises TTSError. The block catches it. Leaving the with block raises the same TTSError again.

Test in the repo's harness (tests/unit/test_tts_session.py, uses the file's server fake and _INPUT_RATE_EXCEEDED):

@pytest.mark.parametrize(
    "code, error",
    [
        pytest.param(3010, _INPUT_RATE_EXCEEDED, id="error-frame"),
        pytest.param(1012, None, id="bare-close"),
    ],
)
def test_a_send_error_caught_inside_with_is_not_raised_twice(server, code, error):
    seen = []
    with aai.TTSSession() as session:
        server.close_on_next_send(code, error)
        try:
            session.generate("Too much, too fast.")
        except aai.TTSError as err:
            seen.append(err)
    assert len(seen) == 1
    assert seen[0] is session.error

The same test with async with aai.AsyncTTSSession() and await session.generate(...) goes in tests/unit/test_tts_session_async.py.

pytest -q tests/unit/test_tts_session.py tests/unit/test_tts_session_async.py -k "not_raised_twice or still_surfaces_on_exit"

Current vs. expected behavior

Current. All 4 cases fail. The error is raised from __exit__ after the block caught it:

>       with aai.TTSSession() as session:
tests/unit/test_tts_session.py:916:
assemblyai/tts/v1/session.py:304: in __exit__
...
assemblyai/tts/v1/session.py:321: in generate
>       raise self._closed_error(ws, closed) from closed
E       assemblyai.tts.v1.models.TTSError: Text arrived faster than it can be spoken.
FAILED tests/unit/test_tts_session.py::test_a_send_error_caught_inside_with_is_not_raised_twice[error-frame]
4 failed, 2 passed, 128 deselected

(The 2 that pass are the existing test_an_error_the_block_already_saw_is_not_raised_twice and the handler guard test below.)

Same result on assemblyai==1.10.11 from PyPI.

Expected. The block saw the error, so leaving it is quiet. That is what already happens when the error comes from audio() (test_an_error_the_block_already_saw_is_not_raised_twice), and from the pre-send closed check in _send().

Why it happens

__exit__ (assemblyai/tts/v1/session.py:303, master at 0ba094c) re-raises self.error unless self._error_raised is set. Every place that hands the error to the caller sets it through _surfaced() (line 444), except _closed_error() (line 504). That's the path a send takes when the socket closed under it. It waits for the reader, then returns self.error without marking it raised. AsyncTTSSession has the same code (async_session.py:420, checked in __aexit__ at line 251).

Suggested fix

One line in _closed_error() in each class (session.py:513, async_session.py:432):

         if self.error is not None:
-            return self.error
+            return self._surfaced()

The error_from_close(...) fallback below it stays unmarked on purpose. A session handler that sends runs on the reader thread, and _emit() (session.py:619) logs its exception, so nothing in the block sees that error. __exit__ must still raise it. The PR adds a test for that: a FlushDone handler sends after a 1012 close, and leaving the block raises the TTSError. It passes on master and with the fix.

With this, the 4 repro cases and the handler test pass, 3/3 runs. Full suite: 1039 passed (master 1034, plus 5 new). ruff format --check, ruff check and mypy are clean.

Version

master at 0ba094c and PyPI assemblyai==1.10.11 (the code is unchanged since the TTS session client landed in 1.8.2). Python 3.12, Linux.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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