[PR #2401] [MERGED] Cap TTS provider response while streaming to prevent OOM #5581

Closed
opened 2026-10-22 04:17:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2401
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/tts-stream-cap


📝 Commits (3)

  • dc585fe Cap TTS provider response while streaming to prevent OOM
  • 56073c5 Classify an oversized TTS error-status body as a provider error
  • 3ccbde4 Drop the TTS error-classification harden and keep only the streaming cap

📊 Changes

1 file changed (+29 additions, -7 deletions)

View changed files

📝 src-tauri/src/commands/storage/integrations/tts.rs (+29 -7)

📄 Description

Linked issue

Closes #2324

Why this change

  • TTS speak() fully buffered the provider response into memory via response.bytes().await and only enforced the 20MB cap afterward. A hostile or misconfigured endpoint could stream a multi-GB body (with the request timeout up to 30 min) and OOM the process before the cap ever ran.

What changed

  • src-tauri/src/commands/storage/integrations/tts.rs: replaced bytes().await with a new read_capped_audio_bytes helper that does a content_length() pre-check against MAX_TTS_AUDIO_BYTES and then streams via a .chunk() loop with a saturating running counter, aborting the moment the cap is exceeded. Mirrors the in-repo read_limited_webhook_text pattern in custom_tools.rs. The post-buffer size check is removed (now enforced during streaming); the same invalid_input "TTS audio response is too large" message is preserved on both reject paths.

Refactor impact

Primary owner: Rust storage (integrations)

Impact areas reviewed:

  • src-tauri/src/commands/storage/integrations/tts.rs (speak, new read_capped_audio_bytes)

Boundary notes:

  • None. Same-file integration change; no IPC/command-signature/return-shape change. Valid (<20MB) responses behave identically. Sibling call sites sharing the same anti-pattern (http.rs, images/providers.rs) are intentionally left out of scope (no drive-by refactor) and can be addressed separately.

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine tts — 13 passed, including local-only tests for the content_length pre-check (declared-41MB body rejected before download) and the streamed-overflow abort (no Content-Length, >20MB stream aborts); happy path unchanged.
  • cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a resource-safety bugfix.

Reason:

  • No user-facing surface change.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2401 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/tts-stream-cap` --- ### 📝 Commits (3) - [`dc585fe`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dc585fe4b716a7bb509e11bd688a93270ad6e123) Cap TTS provider response while streaming to prevent OOM - [`56073c5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/56073c54a4b5f1913a26832a09e791239177ecd3) Classify an oversized TTS error-status body as a provider error - [`3ccbde4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3ccbde441d7860aa891282fa268ae3b20333e91a) Drop the TTS error-classification harden and keep only the streaming cap ### 📊 Changes **1 file changed** (+29 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/integrations/tts.rs` (+29 -7) </details> ### 📄 Description ## Linked issue Closes #2324 ## Why this change - TTS `speak()` fully buffered the provider response into memory via `response.bytes().await` and only enforced the 20MB cap afterward. A hostile or misconfigured endpoint could stream a multi-GB body (with the request timeout up to 30 min) and OOM the process before the cap ever ran. ## What changed - `src-tauri/src/commands/storage/integrations/tts.rs`: replaced `bytes().await` with a new `read_capped_audio_bytes` helper that does a `content_length()` pre-check against `MAX_TTS_AUDIO_BYTES` and then streams via a `.chunk()` loop with a saturating running counter, aborting the moment the cap is exceeded. Mirrors the in-repo `read_limited_webhook_text` pattern in `custom_tools.rs`. The post-buffer size check is removed (now enforced during streaming); the same `invalid_input` "TTS audio response is too large" message is preserved on both reject paths. ## Refactor impact Primary owner: Rust storage (integrations) Impact areas reviewed: - `src-tauri/src/commands/storage/integrations/tts.rs` (`speak`, new `read_capped_audio_bytes`) Boundary notes: - None. Same-file integration change; no IPC/command-signature/return-shape change. Valid (<20MB) responses behave identically. Sibling call sites sharing the same anti-pattern (http.rs, images/providers.rs) are intentionally left out of scope (no drive-by refactor) and can be addressed separately. Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine tts` — 13 passed, including local-only tests for the `content_length` pre-check (declared-41MB body rejected before download) and the streamed-overflow abort (no Content-Length, >20MB stream aborts); happy path unchanged. - `cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a resource-safety bugfix. Reason: - No user-facing surface change. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:34 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5581
No description provided.