Skip to content

perf: offload transcript download cleanup to a worker thread - #1246

Closed
groupthinking wants to merge 1 commit into
mainfrom
claude/determined-maxwell-c3g14j
Closed

perf: offload transcript download cleanup to a worker thread#1246
groupthinking wants to merge 1 commit into
mainfrom
claude/determined-maxwell-c3g14j

Conversation

@groupthinking

Copy link
Copy Markdown
Owner

Canonical issue

Closes #

Outcome

The Gemini video transcription fallback no longer stalls the asyncio event loop while it deletes downloaded artifacts. Every request that reaches _fallback_transcript_with_gemini previously ran Path.exists / Path.unlink / shutil.rmtree — all blocking syscalls — inline in a finally block, over a temp tree that can hold a merged mp4 plus unmerged .fNNN fragments. That unbounded disk work now runs in a worker thread.

Scope

  • Included:
    • src/youtube_extension/services/workflows/transcript_action_workflow.py — extract inline cleanup into a static _cleanup_download_artifacts helper that runs under asyncio.to_thread, wrapped in asyncio.shield.
    • tests/unit/test_transcript_action_workflow.py — new TestCleanupDownloadArtifactsOffEventLoop coverage.
  • Explicitly excluded: no change to request values, transcription behavior, or the format-selection chain.

Risk

  • Risk level: low
  • Failure mode: a cancellation delivered during the finally could previously skip an unshielded await and leak the temp tree; asyncio.shield preserves the original "always cleans up" property while still yielding the loop and re-raising CancelledError.
  • Rollback: revert this commit; the prior inline synchronous cleanup is restored verbatim (guards, except OSError, ignore_errors=True are unchanged).

Verification

  • Focused logic verification — the extracted helper was exercised standalone in the sandbox: it removes the video + fragments + temp root, tolerates None/missing paths, and runs rmtree on a non-event-loop thread. The committed pytest suite (TestCleanupDownloadArtifactsOffEventLoop) asserts the same via monkeypatch, including a non-vacuous guard that both primitives actually ran and a concurrency test proving the loop keeps running during deletion.
  • Required CI — could not run the full pytest suite in the sandbox (backend runtime deps such as httpx/pydantic are not installed); relying on repo CI on the current head.
  • Review threads resolved

Production evidence

Python-only change to backend workflow cleanup; not exercised by the Next.js Vercel preview. Behavior is identical to the prior synchronous path apart from where the deletion runs, so no new runtime surface is introduced.

Agent handoff

  • One canonical issue is linked — none exists yet; this is a perf follow-up in the event-loop-hygiene series.
  • No competing PR implements the same change
  • Acceptance criteria are satisfied (cleanup off the event loop, semantics preserved)
  • Required checks pass on the current head — pending CI
  • Human decision is requested for final review / provenance finalization

Agent provenance

Underlying commit is human-authored (Hayden, co-authored by Copilot App); provenance/agent-lock finalization is left to the maintainer. Kept as draft pending CI and human review.


Generated by Claude Code

The `finally` block in `_fallback_transcript_with_gemini` deleted the
downloaded video and recursively removed its temp tree directly on the
event loop. `Path.exists`, `Path.unlink` and `shutil.rmtree` are all
blocking syscalls, and the temp tree can hold a merged mp4 plus unmerged
`.fNNN` fragments, so every request that reaches the Gemini video
fallback stalls the loop for the duration of the delete.

Move the cleanup into `_cleanup_download_artifacts`, a static helper that
runs the same logic under `asyncio.to_thread`. The call is wrapped in
`asyncio.shield` because the original inline code was uncancellable: a
bare `await` in a `finally` can be interrupted by a second cancellation,
which would turn a loop stall into a disk leak. Filesystem semantics are
preserved verbatim, including the `exists()` guards, `except OSError`
and `ignore_errors=True`.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@vercel

vercel Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
v0-uvai Ready Ready Preview, v0 Aug 2, 2026 4:28pm

@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (1)
  • [‘architecture-gap’, ‘bug’, ‘ci-cd’, ‘ci/cd’, ‘copilot-rabbit’, ‘documentation’, ‘duplicate’, ‘enhancement’, ‘frontend’, ‘github_actions’, ‘good first issue’, ‘help wanted’, ‘high-priority’, ‘invalid’, ‘javascript’, ‘ml-model’, ‘needs-triage’, ‘pipeline-critical’, ‘placeholder-code’, ‘priority:high’, ‘python’, ‘python:uv’, ‘question’, ‘styling’, ‘tests’, ‘v0’]

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b58f51c5-b3e8-4f91-b221-2d53a299eca7

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

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@github-actions github-actions Bot added the python label Aug 2, 2026
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Snapshot Warnings

⚠️: No snapshots were found for the head SHA 94329ad.
Ensure that dependencies are being submitted on PR branches and consider enabling retry-on-snapshot-warnings. See the documentation for more information and troubleshooting advice.

Scanned Files

None

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

Agent Completion Truth Gate: BLOCKED

Reasons: invalid_payload

Machine-readable verdict
{
  "details": {
    "invalid_fields": [
      "issue.number",
      "policy.agent_login",
      "policy.run_id"
    ]
  },
  "reasons": [
    "invalid_payload"
  ],
  "verdict": "blocked"
}

Workflow evidence

@groupthinking

Copy link
Copy Markdown
Owner Author

Closing as a duplicate of #1245.

This PR points at the identical head commit (94329ada3e3584c9cde10b3972116c491e0375c7) as #1245, which was opened first (16:22Z vs 16:27Z) and carries the canonical issue link (#1244) plus the full governance body. This one was raised from a mirrored branch (claude/determined-maxwell-c3g14j) with an older body format and no linked issue, which is why its truth-gate returned invalid_payload.

Review is happening on #1245. No work is lost by closing this.

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.

1 participant