Skip to content

Fix TTS memory leaks causing failure after extended usage#4615

Merged
TinyKitten merged 2 commits intodevfrom
copilot/fix-tts-functionality-issue
Nov 15, 2025
Merged

Fix TTS memory leaks causing failure after extended usage#4615
TinyKitten merged 2 commits intodevfrom
copilot/fix-tts-functionality-issue

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

TTS stops working after prolonged app usage on iOS/Android due to AudioPlayer resource leaks and unbounded cache growth.

Core Issues

AudioPlayer resource leaks:

  • Event listeners not removed in error paths
  • Existing players not cleaned up before creating new ones
  • play() failures leave resources dangling

Unbounded memory growth:

  • TTS cache grows indefinitely (one entry per station announcement)
  • No size limit causes eventual memory exhaustion

Changes

src/hooks/useTTS.ts

  • Pre-cleanup existing players before instantiation
  • Wrap all resource cleanup in try-catch (defensive against partial failures)
  • Add error handling to play() with full resource teardown on failure
  • Enhanced error logging for fetchSpeech() and HTTP failures

src/hooks/useTTSCache.ts

  • Implement FIFO cache with MAX_CACHE_SIZE = 50
  • Auto-evict oldest entries when limit exceeded

src/hooks/useTTSCache.test.ts

  • Verify cache eviction at capacity
  • Confirm FIFO ordering preserved

Example

Before:

const soundJa = createAudioPlayer({ uri: pathJa });  // Old player leaks
soundJa.play();  // Throws but leaves listeners attached

After:

// Cleanup existing
soundJaRef.current?.pause();
soundJaRef.current?.remove();

const soundJa = createAudioPlayer({ uri: pathJa });
try {
  soundJa.play();
} catch (e) {
  // Full teardown on failure
  jaRemoveListener?.remove();
  soundJa.remove();
}

Note

File system cache (MP3 files) still unbounded - follow-up issue for disk cleanup strategy.

Original prompt

This section details on the original issue you should resolve

<issue_title>長時間使用しているとTTSが動作しなくなることがある</issue_title>
<issue_description>iOS/Androidどちらでも再現。functionsの処理も考慮して調査が必要。</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 15, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

.coderabbit.yml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'auto_review'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Comment @coderabbitai help to get the list of available commands and usage tips.

- Improve AudioPlayer event listener cleanup in useTTS.ts
- Add cleanup of existing players before creating new ones
- Add error handling and logging for TTS operations
- Implement cache size limit (MAX_CACHE_SIZE=50) in useTTSCache.ts
- Add comprehensive tests for cache size limitation
- Fix linting and formatting issues

Co-authored-by: TinyKitten <32848922+TinyKitten@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TTS not functioning after extended use Fix TTS memory leaks causing failure after extended usage Nov 15, 2025
Copilot AI requested a review from TinyKitten November 15, 2025 01:03
@TinyKitten TinyKitten marked this pull request as ready for review November 15, 2025 01:13
@github-actions github-actions bot added the react label Nov 15, 2025
@TinyKitten TinyKitten merged commit 68479ae into dev Nov 15, 2025
8 checks passed
@TinyKitten TinyKitten deleted the copilot/fix-tts-functionality-issue branch November 15, 2025 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

長時間使用しているとTTSが動作しなくなることがある

2 participants