Skip to content

Fix stale state in user turn stop strategies between turns#4057

Merged
markbackman merged 2 commits intomainfrom
mb/fix-4053
Mar 17, 2026
Merged

Fix stale state in user turn stop strategies between turns#4057
markbackman merged 2 commits intomainfrom
mb/fix-4053

Conversation

@markbackman
Copy link
Copy Markdown
Contributor

@markbackman markbackman commented Mar 17, 2026

Context

The issue filed in #4053 highlights a case where state can bleed over between one turn to the next. This PR attempts to clean up the state between turns to ensure we don't have any state carrying over.

Summary

  • Fixed premature user turn stops caused by late transcriptions arriving between turns. When a TranscriptionFrame arrived after a turn ended but before the next one started, its text would persist as stale state into the next turn, triggering an immediate stop before the current turn's real transcript arrived.
  • Stop strategies are now reset at both turn start and turn stop, ensuring no state leaks across turn boundaries. The if self._user_turn: return guard in _trigger_user_turn_start ensures this only fires once per turn, not on mid-turn VAD restarts (speak-pause-speak).
  • Pending timeout tasks in SpeechTimeoutUserTurnStopStrategy and TurnAnalyzerUserTurnStopStrategy are now cancelled during reset() to prevent stale timeouts from firing after a turn boundary.

Testing

uv run pytest tests/test_user_turn_stop_strategy.py tests/test_user_turn_controller.py -v

Fixes

Reset stop strategies at turn start (not just turn stop) so that late
transcriptions arriving between turns do not leave stale _text that
causes premature stops on the next turn. Also cancel pending timeout
tasks in reset() for both SpeechTimeout and TurnAnalyzer strategies.
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
...ser_stop/speech_timeout_user_turn_stop_strategy.py 91.11% <100.00%> (+0.30%) ⬆️
...user_stop/turn_analyzer_user_turn_stop_strategy.py 79.27% <100.00%> (-0.36%) ⬇️
src/pipecat/turns/user_turn_controller.py 95.16% <100.00%> (+0.07%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@filipi87 filipi87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! LGTM!

@markbackman markbackman merged commit 95ee096 into main Mar 17, 2026
6 checks passed
@markbackman markbackman deleted the mb/fix-4053 branch March 17, 2026 20:19
markbackman added a commit that referenced this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stale _text in TurnAnalyzerUserTurnStopStrategy causes 'one turn behind' bug

2 participants