Skip to content

TTS WebSocket session mutates flush_id/cancel state on failed sends, lacks async send lock, and allows encoding="mp3" #304

Open

Description

@Pizzylee001

Summary

Three issues found while auditing assemblyai.tts.v1 WebSocket sessions (TTSSession and AsyncTTSSession):

  1. flush() and cancel() mutate session state before _send() succeeds

    • assemblyai/tts/v1/session.py:330-350 and assemblyai/tts/v1/async_session.py:260-275 increment _next_flush_id and update _cancel_ids / _cancelled_before before calling _send(...).
    • If _send() raises (for example, RuntimeError or ConnectionClosed), the mutated counters stay corrupted. (By contrast, assemblyai-node-sdk calls this.send(...) first so a refused send changes nothing.)

    Minimal repro:

    import assemblyai as aai
    
    s = aai.TTSSession(api_key="test")
    try:
        s.cancel()
    except RuntimeError:
        pass
    assert s._next_flush_id == 0       # Fails: actually 1
    assert s._cancelled_before == -1   # Fails: actually 0
    assert s._cancel_ids == []         # Fails: actually [0]
  2. Missing asyncio.Lock in AsyncTTSSession

    • Sync TTSSession guards sends and flush_id allocation with self._send_lock (session.py:155) so flush_ids follow wire order.
    • AsyncTTSSession (async_session.py:254-275) has no asyncio.Lock, so concurrent coroutines calling generate() / flush() / cancel() can assign flush_ids out of wire order.
  3. check_session_config() accepts encoding="mp3" for /v1/ws

    • assemblyai/tts/_base_ws.py:163-170 rejects container="wav" and bit_rate with a message stating sessions stream "raw pcm_s16le, pcm_mulaw or pcm_alaw", but does not reject encoding=TTSEncoding.mp3 / "mp3".
    • build_uri(...) builds wss://tts.us.assemblyai.com/v1/ws?encoding=mp3&word_boundaries=false, which /v1/ws rejects.
  4. (Minor docs note) assemblyai/tts/v1/models.py:141,150,230 states the default voice is aai-jane, while live POST /v1/speech defaults to aai-kyla.

Suggested Fix

  • Send the WebSocket frame before mutating _next_flush_id, _cancel_ids, and _cancelled_before.
  • Add an asyncio.Lock to AsyncTTSSession mirroring TTSSession._send_lock.
  • Reject config.encoding in (TTSEncoding.mp3, "mp3") in check_session_config().

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