Skip to content

Error the audio stream when the connection closes before turn.end - #35

Merged
Migushthe2nd merged 1 commit into
Migushthe2nd:mainfrom
1letme:error-on-truncated-stream
Jul 9, 2026
Merged

Error the audio stream when the connection closes before turn.end#35
Migushthe2nd merged 1 commit into
Migushthe2nd:mainfrom
1letme:error-on-truncated-stream

Conversation

@1letme

@1letme 1letme commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #34.

Problem

onclose ends every in-flight audio stream with push(null) — the same thing the turn.end handler does on a normal finish. So when a connection drops mid-synthesis (before turn.end), the truncated result looks identical to a complete one: toStream() emits a clean end with a partial buffer, toFile() writes a short file, and there's no error to catch and retry on. It shows up most with rotating/residential proxies, whose connections drop mid-stream fairly often.

Change

  • Track whether turn.end was received for each request (a turnEnded flag on the stream record).
  • In onclose, streams that finished normally still end cleanly; streams closed before turn.end are now destroy()ed with an Error, so callers can catch it and retry.
  • toFile(): .pipe() doesn't forward a source-stream error to the writable, so the truncated case would otherwise hang the write promise forever. Added an audioStream error handler that tears down the file stream and rejects. The happy path is unchanged.

Tests

Added a truncated connection suite backed by a local WebSocket server standing in for Edge:

  • drops the socket before turn.end → the stream must error (partial audio has already been delivered), and
  • sends turn.end normally → the stream must end cleanly (guards against false positives).

Existing tests are unchanged and still pass.

@1letme

1letme commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Quick note on the failing check — it's the docs deploy, not the change itself. deploy_docs.yml runs on pull_request, and its last step (Deploy 🚀) force-pushes the built docs to the gh-pages branch, which 403s because a PR build only gets a read-only GITHUB_TOKEN (and from a fork it has no write access here at all):

remote: Permission to Migushthe2nd/MsEdgeTTS.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/Migushthe2nd/MsEdgeTTS.git/': The requested URL returned error: 403

The steps that actually check the code — pnpm run build and npx typedoc — both pass; only the deploy step fails. It should go green once this is on main.

@Migushthe2nd
Migushthe2nd merged commit 1eb6ac6 into Migushthe2nd:main Jul 9, 2026
1 check failed
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.

Dropped connection mid-stream ends the stream cleanly instead of erroring (silent truncation)

2 participants