Troubleshooting playback lock leaks permanently on "media not on disk"; lock conflict = 404 (reviewer#20 F3) #233

Closed
opened 2026-07-11 00:55:25 +02:00 by timothy · 2 comments
Owner

From audit adversarial-reviewer#20, finding F3 (High). Builds on #231's ownership model (use its scope/token if adopted).

Defects

  • PrepareTroubleshootingPlaybackHandler.GetProcess locks at :171, then if (string.IsNullOrEmpty(mediaPath)) return BaseError.New("Media item does not exist on disk") at :182-186. That's a Left return, not an exception, so the unlocking catch at :142-150 never fires — nothing ever releases the lock. GET /api/troubleshoot/playback/status reports "running" forever; all future troubleshoot attempts fail until process restart. Repro: troubleshoot any media item whose file was deleted/unmounted.
  • Lock-conflict ("Troubleshooting playback is locked", :67-70 / :166-169) surfaces through TroubleshootController.cs:113 as a bare 404 — indistinguishable from a bad id. Should be 409 ProblemDetails.
  • The controller enqueues StartTroubleshootingPlayback (whose handler finally at StartTroubleshootingPlaybackHandler.cs:203 is the normal release) only after Prepare succeeds — a cancelled request in that window also leaks. Guard the enqueue (catch → unlock → rethrow).
  • The two lock sites are racy check-then-set bools — fixed by #231; this issue just needs to consume the sound primitive.

Required tests

  • GetProcess with empty media path → assert troubleshooting lock released.
  • Lock held → endpoint returns 409 (contract test case in OpenApiErrorResponseContractTests).
  • OpenAPI regen if response codes change.
From audit [adversarial-reviewer#20](https://gitea.tblindustries.be/timothy/adversarial-reviewer/issues/20#issuecomment-7588), finding **F3 (High)**. Builds on #231's ownership model (use its scope/token if adopted). ## Defects - `PrepareTroubleshootingPlaybackHandler.GetProcess` locks at `:171`, then `if (string.IsNullOrEmpty(mediaPath)) return BaseError.New("Media item does not exist on disk")` at `:182-186`. That's a `Left` return, not an exception, so the unlocking `catch` at `:142-150` never fires — **nothing ever releases the lock**. `GET /api/troubleshoot/playback/status` reports `"running"` forever; all future troubleshoot attempts fail until process restart. **Repro**: troubleshoot any media item whose file was deleted/unmounted. - Lock-conflict ("Troubleshooting playback is locked", `:67-70` / `:166-169`) surfaces through `TroubleshootController.cs:113` as a bare **404** — indistinguishable from a bad id. Should be **409 ProblemDetails**. - The controller enqueues `StartTroubleshootingPlayback` (whose handler `finally` at `StartTroubleshootingPlaybackHandler.cs:203` is the normal release) only after Prepare succeeds — a cancelled request in that window also leaks. Guard the enqueue (catch → unlock → rethrow). - The two lock sites are racy check-then-set bools — fixed by #231; this issue just needs to consume the sound primitive. ## Required tests - `GetProcess` with empty media path → assert troubleshooting lock released. - Lock held → endpoint returns 409 (contract test case in `OpenApiErrorResponseContractTests`). - OpenAPI regen if response codes change.
timothy added the bugapipriority: mediumreview labels 2026-07-11 00:55:25 +02:00
timothy added the in-progress label 2026-07-11 13:14:42 +02:00
Author
Owner

Claiming — session started 2026-07-11. Batching with #234 (both reviewer#20 lock-lifecycle Highs on the EntityLocker surface #231 just reworked, minimal SPA touch) into one PR, mirroring the #231→#232 batch (PR #241). #235 (the broader contract sweep) deferred — it's explicitly "land after the per-surface fixes" and touches web/src/App.tsx which a parallel session is actively refactoring (#244).

Claiming — session started 2026-07-11. Batching with #234 (both reviewer#20 lock-lifecycle Highs on the EntityLocker surface #231 just reworked, minimal SPA touch) into one PR, mirroring the #231→#232 batch (PR #241). #235 (the broader contract sweep) deferred — it's explicitly "land after the per-surface fixes" and touches `web/src/App.tsx` which a parallel session is actively refactoring (#244).
Author
Owner

Closed — PR #250 (merged to main, CI green)

What was done

  • PrepareTroubleshootingPlaybackHandler: both lock sites acquire atomically via if (!LockTroubleshootingPlayback()) (kills the check-then-set TOCTOU where two racers could both pass the old IsLocked check and both proceed as "owner"). Lock ownership is now centralized in Handle behind a lockAcquired flag; GetProcess is lock-free. The empty-media-path Left now releases the lock.
  • TroubleshootController.TroubleshootPlayback: lock conflict → 409 ProblemDetails (was bare 404); a startEnqueued/acquiredLock guard releases the lock if we never hand off to StartTroubleshootingPlayback.

Root cause
The lock was acquired then a non-exception Left early-return (empty media path, :182-186) skipped release, and the release paths weren't gated on this caller's ownership — so a stranded lock left GET /api/troubleshoot/playback/status reporting "running" forever, and the outer catch could cross-release another session's lock on a pre-acquisition exception (caught in adversarial review — see below).

Adversarial review (2 rounds, fork + Codex) — Codex found an additional High: the outer catch released the lock unconditionally → cross-release when an exception fired before this caller acquired (e.g. request cancellation during validation). Fixed by the lockAcquired ownership flag (8cdc9636), with a non-vacuous regression test verified against a negative control.

Files: PrepareTroubleshootingPlaybackHandler.cs, TroubleshootController.cs, + PrepareTroubleshootingPlaybackHandlerTests.cs, TroubleshootControllerTests.cs, OpenApiErrorResponseContractTests.cs, regenerated v1.json.

Deferred → #235: TOCTOU race-loser returns 404 not 409 (cosmetic); worker-dispatch-failure lock leak (pre-existing, needs the background-worker release guarantee).

Docs: OpenAPI regenerated; no convention/route change, so no doc-table edits required.

## Closed — PR #250 (merged to main, CI green) **What was done** - `PrepareTroubleshootingPlaybackHandler`: both lock sites acquire atomically via `if (!LockTroubleshootingPlayback())` (kills the check-then-set TOCTOU where two racers could both pass the old `IsLocked` check and both proceed as "owner"). Lock ownership is now centralized in `Handle` behind a `lockAcquired` flag; `GetProcess` is lock-free. The empty-media-path `Left` now releases the lock. - `TroubleshootController.TroubleshootPlayback`: lock conflict → **409 ProblemDetails** (was bare 404); a `startEnqueued`/`acquiredLock` guard releases the lock if we never hand off to `StartTroubleshootingPlayback`. **Root cause** The lock was acquired then a non-exception `Left` early-return (empty media path, `:182-186`) skipped release, and the release paths weren't gated on *this caller's* ownership — so a stranded lock left `GET /api/troubleshoot/playback/status` reporting `"running"` forever, and the outer `catch` could cross-release another session's lock on a pre-acquisition exception (caught in adversarial review — see below). **Adversarial review (2 rounds, fork + Codex)** — Codex found an additional **High**: the outer `catch` released the lock unconditionally → cross-release when an exception fired *before* this caller acquired (e.g. request cancellation during validation). Fixed by the `lockAcquired` ownership flag (`8cdc9636`), with a non-vacuous regression test verified against a negative control. **Files**: `PrepareTroubleshootingPlaybackHandler.cs`, `TroubleshootController.cs`, + `PrepareTroubleshootingPlaybackHandlerTests.cs`, `TroubleshootControllerTests.cs`, `OpenApiErrorResponseContractTests.cs`, regenerated `v1.json`. **Deferred → #235**: TOCTOU race-loser returns 404 not 409 (cosmetic); worker-dispatch-failure lock leak (pre-existing, needs the background-worker release guarantee). **Docs**: OpenAPI regenerated; no convention/route change, so no doc-table edits required.
timothy removed the in-progress label 2026-07-11 14:13:37 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: timothy/ersatztv#233