Repository navigation
Conversation
AgentSession.start() publishes the AgentActivity (and sets agent._activity) before the realtime provider connects. If bring-up then fails, _started is still False, so aclose() returned early: the realtime session stayed open, no close event was emitted, and the agent kept its activity. Retrying start() with the same agent then raised "cannot start agent: an activity is already running". Skip close only when the session never started or was already closed: a session that has an activity but no _started flag is a failed start and still owns resources to release. Add a regression test covering provider connect failure, the close event, realtime session teardown, double aclose, and reusing the agent. Co-Authored-By: Claude Code <noreply@anthropic.com>
A start() that raises during room bring-up has already created the session span and room io, but the activity is only published later. The previous guard keyed only on the activity, so aclose() still returned early and left the half-started RoomIO and session span behind. Treat the session span as the marker of a start attempt: it is created at the top of start() and cleared when the session closes. A session with no span, no activity, and no _started flag never started or is already closed. Add a regression test with a room whose on() raises: start() leaves _activity None and _room_io set, and aclose() now emits the close event, clears room io and the span, and stays safe when called twice. Co-Authored-By: Claude Code <noreply@anthropic.com>
Resolved tests/test_agent_session.py imports: keep CloseReason/RoomOptions (failed-start tests) together with main's AudioOutput/AudioOutputCapabilities and FakeLLM.
|
Hi @BlueX888, thanks for the PR! Before we can merge, could you take a look at the critical issue Devin flagged here (if |
RoomIO attaches its inputs and outputs to the AgentSession at start(), before AgentActivity is created, and RecorderIO then wraps the audio streams. If activity bring-up fails there is no activity to detach them in _teardown_activity, so a retry treats the closed streams as caller-provided I/O and disables fresh room audio. Release the I/O that RoomIO and RecorderIO attached on close, identity-checked so caller-provided I/O is preserved, and restore what the recorder wrapped. Adds regression tests for the same-session retry, caller I/O, and the recording path.
|
Hi @davidzhao — fixed in f06733c. Devin's diagnosis was right, and I reproduced it: with room audio enabled and a realtime config The teardown now releases exactly the I/O that
Regression tests cover the same-session retry with room audio (asserts fresh RoomIO input/output are attached after the retry), caller-provided audio preservation, and the recording path with a fake job context. The detach test fails on the previous head and passes with the change; the six related test modules (including Happy to adjust if you'd prefer the detach to live in |
When room audio output is disabled because the caller set its own output (an avatar, for example), the transcription synchronizer still wraps that output and RoomIO installs the wrapper. If activity bring-up fails before anything detaches it, closing the partial session dropped the caller's output, and a retry enabled the room track instead. Remember the output the synchronizer replaced and restore it when the wrapper is still attached, mirroring the recorder handling.
A start() that fails between register_session() and SessionHost.start() leaves the host subscribed to the session's events with the transport and event channel built in __init__ still open. SessionHost.aclose() returned early for a host whose start() never ran, so the callbacks stayed attached and nothing released the transport or channel. aclose() now unregisters the session and closes the event channel and transport even when start() never ran, which is the only teardown for that partial start.
|
@davidzhao both Devin findings are now covered on the current head:
The remaining avatar note is a plugin-owner contract: the core cannot observe whether a caller-owned sink still consumes after Tests: |
davidzhao
left a comment
There was a problem hiding this comment.
Upon further review, my concern is that widening the aclose() guard runs normal shutdown on a session that never finished starting. That has two concrete effects on retry: teardown can clear caller-provided audio, and a job-owned host can replay a stale session_closed event. The fixes for those effects are spreading across RoomIO, RecorderIO, and SessionHost.
Could we make start() roll back the failed attempt before re-raising the original error? A single private cleanup path under the session lock could cancel startup tasks and release the resources that attempt acquired, in reverse order. It should restore caller-owned I/O, close a session-owned host, and discard the failed attempt’s buffered events on a job-owned host. The resource classes can still provide the cleanup operations, with start() deciding when to call them.
|
Implemented the rollback in bdbeca6.
A session-owned host is closed; a job-owned host is only unregistered and its buffered events discarded, so the retry reuses it without replaying the failed attempt's events. Tests: the failed-start cases now assert the resources are gone immediately after |
bdbeca6 to
5c2d58d
Compare
|
Addressed the three Devin findings in 5c2d58d:
New tests cover all three: |
start() now releases the room I/O, recorder, host, and activity acquired by the failed attempt before re-raising. Caller-provided I/O is restored, a job-owned SessionHost stays open with its buffered events dropped, and no close event is emitted for a session that never started, so the same session can be retried.
5c2d58d to
e29855a
Compare
The 9 test_room.py errors were setup timeouts (connection refused on :7880); the other 7644 tests passed.
Problem
AgentSession.start()can fail after it has already created resources, andaclose()then does nothing:_startedis only set at the very end ofstart()(agent_session.py:1189), but before that the method starts the session span, createsroom_io/session_host/RecorderIO, publishes theAgentActivity(settingagent._activity), and connects the realtime provider. Two failure windows are left uncleaned:_started— e.g. the realtime provider fails to connect, MCP toolset setup fails, orsession_host.start()fails.aclose()returns at the guard; the realtime session stays open, nocloseevent is emitted,room_io/session_hostare never closed, andagent._activitystays set so reusing the agent raisescannot start agent: an activity is already running.RoomIO.start()._activityis stillNone, so the same guard skips cleanup androom_io(plus its room listeners and the session span) leaks.Observed on
049aea8with a provider that fails to connect:Fix
Only treat a session as nothing-to-close when it has no activity and no session span:
_session_spanis created at the top ofstart()(before room io and the activity) and cleared in_aclose_locked'sfinally, so it is the sentinel that covers both failure windows._activity is Noneand_session_span is None, so they still no-op; a successfulstart()->aclose()path is unchanged.Noneactivity:_teardown_activityskips the activity part,RoomIO.aclosecloses whatever it registered, andcloseis emitted once.Tests
Two regression tests in
tests/test_agent_session.py:test_failed_start_tears_down_partial_session— provider connect failure after the activity is published; asserts the close event, activity/realtime teardown, doubleaclosesafety, and agent reuse in a fresh session.test_failed_start_in_room_bring_up_is_still_closed— roomon()raises before the activity exists; assertsclose,_room_io is None, and the session span cleared.Receipts on
049aea8: both tests fail atassert closes == [CloseReason.USER_INITIATED](assert [] == [...]); the new pre-activity test also fails on the first commit alone. With the change:tests/test_agent_session.py112 passed; 180 adjacent voice/realtime tests pass unchanged;ruff checkandruff format --checkclean.Note
Compose checks: auto-merges cleanly with #8147, #7956 and #7930; #7931 only conflicts in
voice/agent.py(unrelated hunk) and #7940 only in this test file's import block — both trivial at rebase. Happy to rebase on request.Update — partial-start detach
Addressing the review note that a failed
start()can leave RoomIO's closed streams attached: RoomIO now records the session I/O it attaches and clears it on close (identity-checked, so caller-provided I/O is preserved), and RecorderIO restores the streams its wrappers replaced, which also coversrecord=True/ server-side recording. Regression tests cover the same-session retry, caller-provided audio, and the recording path.🤖 Generated with Claude Code
An unstarted
SessionHostis now released too:aclose()unregisters the session and closes the event channel and transport even whenstart()never ran, with a retry test that asserts a fresh host is created.