Repository navigation
Androidで長時間オートモード後にTTSが再生されなくなる不具合を修正 - #6235
Conversation
📝 WalkthroughウォークスルーTTSパイプラインに3つの強化を実施した。 変更点TTSパイプライン強化
シーケンス図sequenceDiagram
participant speechWithText
participant fetchSpeechAudio
participant armPlaybackWatchdog
participant speakFromPath
participant jaPlayer
participant enPlayer
rect rgba(100, 149, 237, 0.5)
Note over speechWithText, armPlaybackWatchdog: フェッチ段階
speechWithText->>armPlaybackWatchdog: 起動(ハング監視開始)
speechWithText->>fetchSpeechAudio: timeoutMs付きでフェッチ
alt フェッチタイムアウト
fetchSpeechAudio-->>speechWithText: null
armPlaybackWatchdog-->>speechWithText: finishPlaying(強制リセット)
else 正常取得
fetchSpeechAudio-->>speechWithText: AudioBlob
end
end
rect rgba(60, 179, 113, 0.5)
Note over speakFromPath, enPlayer: 再生段階
speechWithText->>speakFromPath: jaPath, enPath
speakFromPath->>jaPlayer: playAudio(jaPlayerRef, replace)
jaPlayer-->>speakFromPath: onFinish
speakFromPath->>jaPlayer: stopJa
speakFromPath->>enPlayer: playAudio(enPlayerRef, replace)
enPlayer-->>speakFromPath: onFinish
speakFromPath->>speakFromPath: finishPlaying
end
推定コードレビュー工数🎯 4 (Complex) | ⏱️ ~60 minutes 関連する可能性のあるPR
詩
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/utils/ttsAudioPlayer.test.ts (1)
197-202: ⚡ Quick win
pause -> replace -> playの順序も明示的に検証してください現状は各メソッドの呼び出し有無のみの検証で、順序が崩れてもテストが通る余地があります。順序も固定して回帰を防いでください。
差分案
expect(mock.player.pause).toHaveBeenCalled(); expect(mock.player.replace).toHaveBeenCalledWith({ uri: '/path/to/next.mp3', }); expect(mock.player.play).toHaveBeenCalledTimes(1); + const pauseOrder = mock.player.pause.mock.invocationCallOrder[0]; + const replaceOrder = mock.player.replace.mock.invocationCallOrder[0]; + const playOrder = mock.player.play.mock.invocationCallOrder[0]; + expect(pauseOrder).toBeLessThan(replaceOrder); + expect(replaceOrder).toBeLessThan(playOrder);As per coding guidelines,
Test agents in isolation and with integration tests to ensure correctness and compatibilityを満たすため、順序の明示検証が有効です。🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils/ttsAudioPlayer.test.ts` around lines 197 - 202, The test for the pause-replace-play sequence currently only verifies that each method on mock.player is called individually, without ensuring they are called in the correct order. To fix this, add explicit order verification to the test by checking the sequence of calls on mock.player. Verify that pause is called first, then replace, and then play last, so that any regression in the call order will be caught by the test. Use Jest's call tracking capabilities to enforce this ordering constraint alongside the existing call validation checks.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/hooks/useTTS.test.ts`:
- Around line 393-451: The test does not properly isolate the second TTS
utterance fetch from the initial render's fetch and prefetch operations. The
default useTTSText mock returns a nextText array, causing both a regular fetch
and a prefetch on the first render, so the mockFetch.toHaveBeenCalledTimes(2)
expectation may pass before the second rerender actually completes. Fix this by
either initializing the useTTSText mock to return nextText: [] from the start
(so only one fetch happens initially), or by capturing the fetch call count
before the rerender and then waiting for it to increment by 1 specifically for
the second utterance. This ensures the mockPlayer.replace verification truly
tests the reuse behavior triggered by the second speech text change.
In `@src/hooks/useTTS.ts`:
- Around line 157-168: Race condition exists where old Promise resolutions from
fetch operations can still execute and replay stale audio even after timeout or
new utterances begin. Introduce a generation or run id that is incremented for
each new utterance request. In the armPlaybackWatchdog function (lines 157-168),
store the current generation id before cleanup so that subsequent
timeout-triggered cleanup invalidates pending operations. In the fetch/Promise
handling code (lines 284-311), before executing any callbacks like
speechWithText or finishPlaying, verify that the generation id stored when the
request was initiated still matches the current generation id. If it does not
match, skip executing the callback to discard old async continuations and
prevent stale audio playback from interrupting newly started utterances or after
timeouts.
In `@src/utils/ttsSpeechFetcher.ts`:
- Around line 126-127: The timeoutMs parameter destructuring in the
ttsSpeechFetcher function does not validate that the value is a positive finite
number. When 0, negative values, NaN, or Infinity are explicitly passed as
timeoutMs, they bypass the default value assignment and are later used directly
in the setTimeout call at line 152, breaking the timeout protection mechanism.
Add validation logic after the options destructuring to normalize timeoutMs to a
positive finite value, ensuring it falls within acceptable bounds before it is
used in the setTimeout call.
---
Nitpick comments:
In `@src/utils/ttsAudioPlayer.test.ts`:
- Around line 197-202: The test for the pause-replace-play sequence currently
only verifies that each method on mock.player is called individually, without
ensuring they are called in the correct order. To fix this, add explicit order
verification to the test by checking the sequence of calls on mock.player.
Verify that pause is called first, then replace, and then play last, so that any
regression in the call order will be caught by the test. Use Jest's call
tracking capabilities to enforce this ordering constraint alongside the existing
call validation checks.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1e239d93-3f39-45dd-a931-269a796cfa80
📒 Files selected for processing (6)
src/hooks/useTTS.test.tssrc/hooks/useTTS.tssrc/utils/ttsAudioPlayer.test.tssrc/utils/ttsAudioPlayer.tssrc/utils/ttsSpeechFetcher.test.tssrc/utils/ttsSpeechFetcher.ts
概要
Android でオートモードなどを使い TTS を長時間再生し続けると、ある時点から音声がアプリ再起動まで一切再生されなくなる不具合を修正します。ネイティブログで
AudioTrack init failed/AudioFlinger could not create track, status: -12(NO_MEMORY)を確認し、発話のたびにcreateAudioPlayerで新規プレイヤー(ExoPlayer/AudioTrack)を生成していたことによるネイティブ音声トラックの枯渇が主因と特定しました。あわせて、音声フェッチのハングで再生パイプラインが固着するデッドロックも修正します。変更の種類
変更内容
replace()で使い回すよう変更し、AudioTrack 枯渇による再生停止を解消(ttsAudioPlayer.ts/useTTS.ts)。ネイティブ解放(remove())はアンマウント時のみに限定。ttsSpeechFetcher.ts)にAbortControllerベースのタイムアウト(20 秒)を追加。通信切替やドーズ状態でリクエストが応答もエラーも返さずハングしても、playingRefが固着して以降の TTS が止まるのを防止。timeoutMsは0・負値・NaN・Infinityが渡されても正の有限値に正規化。playingRefの安全タイムアウトをフェッチ段階から張るよう変更(armPlaybackWatchdog)。トークン取得・フェッチのハングでも再生パイプラインが確実に復帰する。speechRunIdRef)を導入。各awaitの後に世代が進んでいれば継続を破棄し、タイムアウト・新規発話の後に解決の遅れた古いフェッチが stale な音声を割り込ませて再生する事故を防止。EN_PLAYBACK_DELAY_MS)を削除し、日本語の再生完了後すぐ英語を再生。timeoutMs正規化 / ハング復帰 / ストール時はプレイヤーを破棄せず一時停止)。回帰リスクと対策
AudioTrack init failedがログに出ず、日本語の直後に英語が続けて再生されること)の検証を推奨します。テスト
npm run lintが通ることnpm testが通ることnpm run typecheckが通ること関連Issue
スクリーンショット(任意)