Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assemblyai/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.10.12"
__version__ = "1.10.15"
33 changes: 32 additions & 1 deletion assemblyai/streaming/v3/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,17 @@ def __init__(
self._stop_event: Optional[asyncio.Event] = None
self._read_task: Optional[asyncio.Task] = None
self._write_task: Optional[asyncio.Task] = None
self._connect_started = False
self._connect_task: Optional[asyncio.Task] = None

async def connect(self, params: RealTimeParameters) -> None:
# Single-use: a client whose connection went down (success or
# handshake failure) sets ``_connection_closed_reported``; reusing
# it would yield a silently dead read/write loop because
# ``_stop_event`` is already set.
already_used = (
self._websocket is not None
self._connect_started
or self._websocket is not None
or self._connection_closed_reported
or (self._read_task is not None and not self._read_task.done())
)
Expand All @@ -150,6 +153,22 @@ async def connect(self, params: RealTimeParameters) -> None:
"create a new instance for a new connection."
)

# Reserve single-use ownership before the handshake's first await.
# Disconnect must also own a pending handshake, not only its socket.
self._connect_started = True
self._connect_task = asyncio.current_task()
try:
await self._connect(params)
except asyncio.CancelledError:
if self._stop_event is not None:
self._stop_event.set()
await self._close_websocket()
await self._client.aclose()
raise
finally:
self._connect_task = None

async def _connect(self, params: RealTimeParameters) -> None:
self._write_queue = asyncio.Queue()
self._stop_event = asyncio.Event()

Expand Down Expand Up @@ -214,6 +233,18 @@ async def connect(self, params: RealTimeParameters) -> None:
logger.debug("Connected to WebSocket server")

async def disconnect(self, terminate: bool = False) -> None:
connect_task = self._connect_task
if (
connect_task is not None
and connect_task is not asyncio.current_task()
and not connect_task.done()
):
connect_task.cancel()
try:
await connect_task
except asyncio.CancelledError:
pass

if self._stop_event is None:
# Never connected — still close the HTTP client so the pool
# doesn't leak.
Expand Down
131 changes: 131 additions & 0 deletions tests/unit/test_streaming_connect_lifecycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import asyncio
from contextlib import asynccontextmanager

import pytest
from websockets.legacy.server import serve

from assemblyai.streaming.v3 import (
AsyncRealTimeTranscriber,
RealTimeParameters,
RealTimeTranscriberOptions,
SpeechModel,
)

pytestmark = pytest.mark.asyncio


def params():
return RealTimeParameters(
sample_rate=16000,
speech_model=SpeechModel.universal_streaming_english,
)


@asynccontextmanager
async def delayed_server():
"""Real sockets; only the HTTP upgrade response is held."""
entered = asyncio.Event()
release = asyncio.Event()
connections = []
requests = []

async def handshake(path, headers):
requests.append(path)
entered.set()
await release.wait()

async def handler(socket):
connections.append(socket)
await socket.wait_closed()

before = asyncio.all_tasks()
async with serve(
handler, "127.0.0.1", 0, process_request=handshake, close_timeout=0.1
) as server:
port = server.sockets[0].getsockname()[1]
client = AsyncRealTimeTranscriber(
RealTimeTranscriberOptions(
api_key="local-test", api_host=f"ws://127.0.0.1:{port}"
)
)
try:
yield client, entered, release, connections, requests
finally:
release.set()
await client.disconnect()
# Baseline can orphan both SDK tasks and sockets. Clean the test
# fixture explicitly, after measuring SDK-owned cleanup.
for task in asyncio.all_tasks() - before:
if task.get_name().startswith("AsyncRealTimeTranscriber."):
task.cancel()
await asyncio.gather(task, return_exceptions=True)
for socket in connections:
await socket.close()


@pytest.mark.parametrize("terminate", [False, True])
async def test_disconnect_interrupts_pending_handshake(terminate):
async with delayed_server() as (client, entered, release, sockets, requests):
pending = asyncio.create_task(client.connect(params()))
await asyncio.wait_for(entered.wait(), 0.5)
await client.disconnect(terminate=terminate)
stopped_on_return = pending.done()
release.set()
result = (await asyncio.gather(pending, return_exceptions=True))[0]
print(
f"terminate={terminate} stopped_on_return={stopped_on_return} "
f"result={type(result).__name__} "
f"open_sockets={sum(s.close_code is None for s in sockets)}"
)
assert stopped_on_return
assert isinstance(result, asyncio.CancelledError)
assert all(socket.close_code is not None for socket in sockets)
await client.disconnect(terminate=terminate)
with pytest.raises(RuntimeError, match="already been connected"):
await client.connect(params())
assert len(requests) == 1


async def test_concurrent_connect_reserves_single_use_before_handshake():
async with delayed_server() as (client, entered, release, sockets, requests):
first = asyncio.create_task(client.connect(params()))
await asyncio.wait_for(entered.wait(), 0.5)
second = asyncio.create_task(client.connect(params()))
# Release only after the second coroutine has had an opportunity to
# run its single-use check, without waiting for a second handshake.
await asyncio.sleep(0)
release.set()
results = await asyncio.gather(first, second, return_exceptions=True)
print(f"connect_results={[type(r).__name__ for r in results]}")
assert results[0] is None
assert isinstance(results[1], RuntimeError)
assert len(requests) == len(sockets) == 1
await client.disconnect()
await asyncio.wait_for(sockets[0].wait_closed(), 0.5)


async def test_cancelled_handshake_remains_single_use():
async with delayed_server() as (client, entered, release, sockets, requests):
pending = asyncio.create_task(client.connect(params()))
await asyncio.wait_for(entered.wait(), 0.5)
pending.cancel()
with pytest.raises(asyncio.CancelledError):
await pending
release.set()
with pytest.raises(RuntimeError, match="already been connected"):
await client.connect(params())
await client.disconnect()
assert len(requests) == 1


async def test_normal_connect_and_repeated_disconnect():
async with delayed_server() as (client, entered, release, sockets, requests):
release.set()
await client.connect(params())
with pytest.raises(RuntimeError, match="already been connected"):
await client.connect(params())
await client.stream(b"\0" * 320)
await client.disconnect()
await client.disconnect()
await asyncio.wait_for(sockets[0].wait_closed(), 0.5)
assert len(requests) == 1